1 #include "stdafx.h" 2 #include <iostream> 3 #include <exception> 4 using namespace std; 5 6 /*排序用到的结构*/ 7 const int maxSize = 10; 8 typedef struct 9 { 10 int r[maxSize+1]; 11 int length; 12 }SqList; 13 14 /*数组元素交换*/ 15 void swap(SqList *L,int i ,int j) 16 { 17 int temp = L->r[i]; 18 L->r[i] = L->r[j]; 19 L->r[j] = temp; 20 } 21 22 /*冒泡排序 方法一 该法有缺点,算法效率低下*/ 23 void BubbleSort0(SqList *L) 24 { 25 for(int i = 1;i!=L->length;++i) 26 { 27 for(int j = i;j != L->length;++j) 28 { 29 if(L->r[i] > L->r[j]) 30 { 31 swap(L,i,j); 32 } 33 } 34 } 35 } 36 /*改进的冒泡排序*/ 37 void BubbleSort(SqList *L) 38 { 39 for(int i = 1;i!=L->length;++i) 40 { 41 for(int j=L->length-1;j>=i;j--) 42 { 43 if(L->r[j]>L->r[j+1]) 44 { 45 swap(L,j,j+1); 46 } 47 } 48 } 49 } 50 /*继续改进的冒泡排序*/ 51 void BubbleSort2(SqList *L) 52 { 53 int i,j; 54 int flag = true; 55 for(i =1;i!=L->length&& flag;i++) 56 { 57 flag = false; 58 for(j=L->length-1;j>=i;j--) 59 { 60 if(L->r[j]>L->r[j+1]) 61 { 62 swap(L,j,j+1); 63 flag = true; 64 } 65 } 66 } 67 } 68 int _tmain(int argc, _TCHAR* argv[]) 69 { 70 71 72 return 0 ; 73 }
原文:http://www.cnblogs.com/crazycodehzp/p/3551538.html