[关闭]
@chawuciren 2018-10-15T04:10:18.000000Z 字数 1129 阅读 508

排序

未分类


  1. #include<stdio.h>
  2. void BubbleSort(int a[], int len);
  3. void InsertionSort(int a[], int len);
  4. void SelectionSort(int a[], int len);
  5. int main(void){
  6. int a[16] = {123,1221,12546,7658,47589,3457435,4578565,57575,3534,634,7547432,235,67576,22,347645,131};
  7. int b[16] = {123,1221,12546,7658,47589,3457435,4578565,57575,3534,634,7547432,235,67576,22,347645,131};
  8. int c[16] = {123,1221,12546,7658,47589,3457435,4578565,57575,3534,634,7547432,235,67576,22,347645,131};
  9. BubbleSort(a, 16);
  10. InsertionSort(b, 16);
  11. SelectionSort(c, 16);
  12. for(int i = 0; i < 16; i++){
  13. printf("%d ", a[i]);
  14. }
  15. printf("\n");
  16. for(int i = 0; i < 16; i++){
  17. printf("%d ", b[i]);
  18. }
  19. printf("\n");
  20. for(int i = 0; i < 16; i++){
  21. printf("%d ", c[i]);
  22. }
  23. return 0;
  24. }
  25. void BubbleSort(int a[], int len){
  26. int i,j;
  27. for(i = 0 ; i < len - 1 ; i++){
  28. for(j = 0 ; j < len - i - 1 ; j++)
  29. if(a[j] > a[j+1]){
  30. int t = a[j];
  31. a[j] = a[j+1];
  32. a[j+1] = t;
  33. }
  34. }
  35. return;
  36. }
  37. void SelectionSort(int a[],int len){
  38. int i,j;
  39. for(i = 0 ; i < len-1 ; i++){
  40. int min_index = i;
  41. for(j = i + 1 ; j < len; j++)
  42. if(a[ min_index ] > a[j])
  43. min_index = j;
  44. if(min_index != i){
  45. int temp = a[i];
  46. a[i] = a[min_index];
  47. a[min_index] = temp;
  48. }
  49. }
  50. }
  51. void InsertionSort(int a[], int len){
  52. int i, j, key;
  53. for(i = 1; i < len; i++){
  54. key = a[i];
  55. for(j = i - 1; j >= 0; j--){
  56. if(a[j] > key){
  57. a[j + 1] = a[j];
  58. }
  59. else{
  60. break;
  61. }
  62. }
  63. a[j + 1] = key;
  64. }
  65. }

在此输入正文

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注