排序之选择排序
1、直接选择排序(Straight Select Sorting) 也是一种简单的排序方法,它的基本思想是:第一次从R[0]~R[n-1]中选取最小值,与R[0]交换,第二次从R[1]~R[n-1]中选取最小值,与R[1]交换,....,第i次从R[i-1]~R[n-1]中选取最小值,与R[i-1]交换,.....,第n-1次从R[n-2]~R[n-1]中选取最小值,与R[n-2]交换,总共通过n-1次,得到一个按排序码从小到大排列的有序序列。
1 #include<iostream> 2 using namespace std; 3 4 void Selectsort(int a[], int n) 5 { 6 int i, j, temp; 7 for (i = 0; i < n; i++) 8 { 9 temp = a[i]; 10 for (j = i+1; j < n; j++) 11 { 12 if (a[j] < temp) 13 { 14 temp = a[j]; 15 a[j] = a[i]; 16 a[i] = temp; 17 } 18 } 19 20 } 21 } 22 int main() 23 { 24 int a[]={9, 6, 3, 4, 5, 7}; 25 Selectsort(a,6); 26 for (int i = 0; i < 6; i++) 27 cout << a[i] << endl; 28 return 0; 29 }