选择排序
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 }
道阻且长,行则将至

浙公网安备 33010602011771号