0001 /****************************************************************************************** 0002 * Data Structures in C++ 0003 * ISBN: 7-302-33064-6 & 7-302-33065-3 & 7-302-29652-2 & 7-302-26883-3 0004 * Junhui DENG, deng@tsinghua.edu.cn 0005 * Computer Science & Technology, Tsinghua University 0006 * Copyright (c) 2006-2013. All rights reserved. 0007 ******************************************************************************************/ 0008 0009 template <typename T> //向量的起泡排序(提前终止版) 0010 void Vector<T>::bubbleSort( Rank lo, Rank hi ) { //assert: 0 <= lo < hi <= size 0011 for( bool sorted = false; sorted = !sorted; ) 0012 for( Rank i = lo; i < hi - 1; i++ ) 0013 if( _elem[i] > _elem[i + 1] ) //若逆序,则 0014 swap( _elem[i], _elem[i + 1] ), sorted = false; 0015 //经交换使局部有序——当然,至此还不能确定已整体有序 0016 }