| while (leftPass<=rightPass) { //从左边及右边开始扫描子表 smallIndex=leftPass; largeIndex=rightPass; //j和i遍历整个子表arr[LeftPass]~arr[rightPass] for (i=leftPass+1;i<rightPass;i++) //如果找到更小的元素,则将该位置赋值给smallIndex if (arr[i]<arr[smallIndex]) smallIndex=i; //如果smallIndex和leftPass不在相同的位置 //则将子表中的最小项与arr[pass]交换 if (smallIndex!=leftPass) { temp=arr[leftPass]; arr[leftPass]=arr[smallIndex]; arr[smallIndex]=temp; } for (j=rightPass-1;j>leftPass;j--) if(arr[j]>arr[largeIndex]) largeIndex=j; if(largeIndex!=rightPass) { temp=arr[rightPass]; arr[rightPass]=arr[largeIndex]; arr[largeIndex]=temp; } //从两头收缩子表 leftPass++; rightPass--; } } |
//自编冒泡法排序算法函数bubbleSort()的实现
| template <typename T> int bubbleSort(T arr[],int n) { bool exchanged=false; //是否发生交换 int i,j; //用于遍历子表的下标 T temp; //用于交换元素的临时变量 |
|