1 /*
2 选择排序
3 在冒泡排序的基础上改进
4 在未排序的里面找出最小的
5
6
7 选择排序和冒泡排序的区别,选择排序每次只交换一次,冒泡排序可能要交换多次
8 所以,选择排序比冒泡排序效率高些
9
10 */
11
12 #include "Select.h"
13 #include <iostream>
14
15 using namespace std;
16 void SelectSort(int *a,const int n);
17
18
19 int main()
20 {
21 const int nLen = 10;
22 int x[nLen]={1,3,5,7,9,2,4,6,8,10};
23
24 SelectSort(x,nLen);
25
26 for (int i=0;i<nLen;i++)
27 {
28 cout<<x[i]<<" ";
29 }
30
31
32 cout<<endl;
33 system("pause");
34 return 0;
35 }
36
37
38 void SelectSort(int *list,const int n)
39 {
40
41 for (int i=0;i<n;i++)
42 {
43
44 int nMinFlag = i;//毛巾,下标
45
46 for (int j=i+1;j<n;j++)//每次扫描,要从已扫描过的地方开始
47 {
48 if (list[j] < list[nMinFlag])
49 {
50 nMinFlag = j;//记住下标
51 }
52
53 }
54
55 swap(list[i],list[nMinFlag]);
56 }
57
58 return;
59 }