1 #include <iostream> 2 #include <ctime> 3 using namespace std; 4 #define kARRAYCOUNT 20 5 6 // 冒泡排序(次方法会修改外部的内容)升序 7 void bubbleSort(int **array, int count) 8 { 9 if (!array || count <= 0 || count > kARRAYCOUNT) 10 return; 11 12 for (int i = 1; i < count; i++) 13 { 14 for (int j = 0; j < count - i; j++) 15 { 16 // 从小到大排序 17 if ((*array)[j] > (*array)[j + 1]) 18 { 19 int temp = (*array)[j]; 20 (*array)[j] = (*array)[j + 1]; 21 (*array)[j + 1] = temp; 22 } 23 } 24 } 25 } 26 27 void printfArray(int *array, int count) 28 { 29 if (!array || count <= 0 || count > kARRAYCOUNT) 30 return; 31 32 for (int index = 0; index < kARRAYCOUNT; index++) 33 { 34 cout << array[index] << " "; 35 } 36 cout << endl << "---End---" << endl; 37 } 38 39 void main() 40 { 41 int *array = new int[kARRAYCOUNT]; 42 43 srand(unsigned(time(0))); 44 45 for (int index = 0; index < kARRAYCOUNT; index++) 46 { 47 array[index] = rand() % 50 + 20; 48 } 49 50 printfArray(array, kARRAYCOUNT); 51 52 bubbleSort(&array, kARRAYCOUNT); 53 54 printfArray(array, kARRAYCOUNT); 55 56 getchar(); 57 }
时间: 2024-11-08 20:35:44