typedef int ElementType;void printArray(ElementType num[], int count){ for(int i = 0; i < count; i++) cout << num[i]; cout << endl;}void swap(ElementType &a, ElementType &b){ ElementType tmp = a; a = b; b = tmp;}void bubble(ElementType num[], int count){ for(int i = 0; i < count; i++) { bool flag = false; for(int j = 0; j < count-i-1; j++) { if(num[j] > num[j+1]) { swap(num[j], num[j+1]); flag = true; } } if(!flag) break; printArray(num, count); } cout << endl;}void bid_bubble(ElementType num[], int count){ int low, high; bool flag; low = 0, high = count -1; while(low < high) { flag = false; for(int i = low; i < high; i++) { if(num[i] > num[i+1]) { swap(num[i], num[i+1]); flag = true; } } if(!flag) break; high--; for(i = high; i > low; i--) { if(num[i] < num[i-1]) { swap(num[i], num[i-1]); } } low++; }}void bid_bubble2(ElementType num[], int count){ bool flag = true; int i = 0; while(flag) { flag = false; for(int j = i; j < count - i - 1; j++) { if(num[j] > num[j+1]) { swap(num[j], num[j+1]); flag = true; } } for(j = count - i - 1; j > i; j--) { if(num[j] < num[j-1]) { swap(num[j], num[j-1]); flag = true; } } i++; } }void select(ElementType num[], int count){ for(int i = 0; i < count-1; i++) { int minIndex = i; for(int j = i+1; j < count; j++) { if(num[minIndex] > num[j]) minIndex = j; } if(minIndex != i) { swap(num[minIndex], num[i]); } } cout << endl;}void insert(ElementType num[], int count){ for(int i = 1; i < count; i++) { int tmp = num[i]; for(int j = i; j > 0; j--) { if(num[j-1] > tmp) num[j] = num[j-1]; else break; } num[j] = tmp; } cout << endl; }void shell(ElementType num[], int count){ int step = count/2; for(step; step > 0; step /= 2) { for(int i = step; i < count; i++) { int tmp = num[i]; for(int j = i; j >= step; j -= step) { if(num[j-step] > tmp) num[j] = num[j-step]; else break; } num[j] = tmp; } }}void quick(ElementType num[], int l, int r){ if(l < r) { int i = l, j = r, x = num[l]; while(i < j) { while(i < j && num[j] >= x) j--; if(i < j) num[i++] = num[j]; while(i < j && num[i] < x) i++; if(i < j) num[j++] = num[i]; } num[j] = x; quick(num, l, j-1); quick(num, j+1, r); }}void quick2(ElementType num[], int l, int r){ if(l < r) { x = num[(l+r)/2]; int i = l-1; int j = r+1; while(1) { while(num[++i] < x); while(num[--j] > x); if(i >= j) break; swap(num[i], num[j]); } quick(num, l, j-1); quick(num, j+1, r); }}void main(){ int alpha[] = {2, 5, 4, 1, 3}; int count = sizeof(alpha)/sizeof(alpha[0]); cout << "sort before:"; printArray(alpha, count); bubble(alpha, count); cout << "sort after:"; printArray(alpha, count);}