1.选择排序.c 867 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define N 8
  4. void select_sort(int a[],int n);
  5. //选择排序实现
  6. void select_sort(int a[],int n)//n为数组a的元素个数
  7. {
  8. int i,j;
  9. int min_index;
  10. int temp;
  11. //进行N-1轮选择
  12. for(i=0; i<n-1; i++)
  13. {
  14. min_index = i;
  15. //找出第i小的数所在的位置
  16. for(j=i+1; j<n; j++)
  17. {
  18. if(a[j] < a[min_index])
  19. {
  20. min_index = j;
  21. }
  22. }
  23. //将第i小的数,放在第i个位置;如果刚好,就不用交换
  24. if( i != min_index)
  25. {
  26. temp = a[i];
  27. a[i] = a[min_index];
  28. a[min_index] = temp;
  29. }
  30. }
  31. }
  32. int main()
  33. {
  34. int i;
  35. int num[N] = {89, 38, 11, 78, 96, 44, 19, 25};
  36. select_sort(num, N);
  37. for(i=0; i<N; i++)
  38. printf("%d ", num[i]);
  39. printf("\n");
  40. system("pause");
  41. return 0;
  42. }