选择排序

 1 //选择排序
 2 #include<iostream>
 3 #include <algorithm>
 4 using namespace std;
 5 void selectsort(int a[], int size)
 6 {
 7     int pos, min, j;
 8     for (pos=0;pos<size-1;pos++)
 9     {
10         min = pos;
11         for (j=pos+1;j<size;++j)
12         {
13             if (a[j]<a[min])
14             {
15                 min = j;
16             }
17         }
18         if (pos!=min)
19         {
20             swap(a[pos], a[min]);
21         }
22     }
23 }
24 int main()
25 {
26     int a[6] = { 5,2,4,3,7,0 };
27     selectsort(a, 6);
28     for (auto x : a)
29     {
30         cout << x << " ";
31     }
32     return 0;
33 }

 

posted @ 2020-12-15 23:37  丁帅帅dss  阅读(53)  评论(0)    收藏  举报