| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- #include<stdio.h>
- #include<stdlib.h>
- #define N 8
- void select_sort(int a[],int n);
- //选择排序实现
- void select_sort(int a[],int n)//n为数组a的元素个数
- {
- int i,j;
- int min_index;
- int temp;
- //进行N-1轮选择
- for(i=0; i<n-1; i++)
- {
- min_index = i;
- //找出第i小的数所在的位置
- for(j=i+1; j<n; j++)
- {
- if(a[j] < a[min_index])
- {
- min_index = j;
- }
- }
- //将第i小的数,放在第i个位置;如果刚好,就不用交换
- if( i != min_index)
- {
- temp = a[i];
- a[i] = a[min_index];
- a[min_index] = temp;
- }
- }
- }
- int main()
- {
- int i;
- int num[N] = {89, 38, 11, 78, 96, 44, 19, 25};
- select_sort(num, N);
- for(i=0; i<N; i++)
- printf("%d ", num[i]);
- printf("\n");
- system("pause");
- return 0;
- }
|