1 /* 选择排序 */
2
3 #include <iostream>
4 #include <cstdlib>
5
6 #define ARR_SIZE 10
7
8 using namespace std;
9
10 void chosesort(int a[]);
11 void CreateRandArr(int a[]);
12
13 int main()
14 {
15 int a[ARR_SIZE];
16 int i;
17 CreateRandArr(a);
18 chosesort(a);
19 cout << "after sort: " << endl;
20 for(i=0;i<ARR_SIZE;i++)
21 {
22 cout << a[i] << ' ' ;
23 }
24
25 return 0;
26 }
27
28 /* 从余下的序列中选择最小的作为下一个排序 */
29 void chosesort(int a[])
30 {
31 int i, j, temp;
32 for(i=0; i<ARR_SIZE; i++)
33 {
34 for(j=i+1; j<ARR_SIZE; j++)
35 {
36 if(a[j] < a[i])
37 {
38 temp = a[i];
39 a[i] = a[j];
40 a[j] = temp;
41 }
42 }
43 }
44 }
45
46 void CreateRandArr(int a[])
47 {
48 int i;
49 for(i=0;i<ARR_SIZE;i++)
50 {
51 a[i] = rand() % 100;
52 cout <<a[i] << ' ' ;
53 }
54 cout << endl;
55 }