1.冒泡排序

1.两两注意是相邻的两个元素的意思
2.如果有n个元素需要比较n-1次,每一轮减少1次比较
3.既然叫冒泡排序,那就是从下往上两两比较,所以看上去就跟泡泡往上冒一样。

  1. #include <stdio.h>
  2. void BubbleSort(int k[], int n)
  3. {
  4. int i, j, temp, count1=0, count2=0;
  5. for( i=0; i < n-1; i++ )
  6. {
  7. for( j=i+1; j < n; j++ )
  8. {
  9. count1++;
  10. if( k[i] > k[j] )
  11. {
  12. count2++;
  13. temp = k[j];
  14. k[j] = k[i];
  15. k[i] = temp;
  16. }
  17. }
  18. }
  19. printf("总共进行了%d次比较,进行了%d次移动!", count1, count2);
  20. }
  21. int main()
  22. {
  23. int i, a[10] = {1, 0, 2, 3, 4, 5, 6, 7, 8, 9};
  24. BubbleSort(a, 10);
  25. printf("排序后的结果是:");
  26. for( i=0; i < 10; i++ )
  27. {
  28. printf("%d", a[i]);
  29. }
  30. printf("\n\n");
  31. return 0;
  32. }
  1. #include <stdio.h>
  2. void BubbleSort(int k[], int n)
  3. {
  4. int i, j, temp, count1=0, count2=0;
  5. for( i=0; i < n-1; i++ )
  6. {
  7. for( j=n-1; j > i; j-- )
  8. {
  9. count1++;
  10. if( k[j-1] > k[j] )
  11. {
  12. count2++;
  13. temp = k[j-1];
  14. k[j-1] = k[j];
  15. k[j] = temp;
  16. }
  17. }
  18. }
  19. printf("总共进行了%d次比较,进行了%d次移动!", count1, count2);
  20. }
  21. int main()
  22. {
  23. int i, a[10] = {5, 2, 6, 0, 3, 9, 1, 7, 4, 8};
  24. BubbleSort(a, 10);
  25. printf("排序后的结果是:");
  26. for( i=0; i < 10; i++ )
  27. {
  28. printf("%d", a[i]);
  29. }
  30. printf("\n\n");
  31. return 0;
  32. }
  1. #include <stdio.h>
  2. void BubbleSort(int k[], int n)
  3. {
  4. int i, j, temp, count1=0, count2=0, flag;
  5. flag = 1;
  6. for( i=0; i < n-1 && flag; i++ )
  7. {
  8. for( j=n-1; j > i; j-- )
  9. {
  10. count1++;
  11. flag = 0;
  12. if( k[j-1] > k[j] )
  13. {
  14. count2++;
  15. temp = k[j-1];
  16. k[j-1] = k[j];
  17. k[j] = temp;
  18. flag = 1;
  19. }
  20. }
  21. }
  22. printf("总共进行了%d次比较,进行了%d次移动!", count1, count2);
  23. }
  24. int main()
  25. {
  26. int i, a[10] = {5, 2, 6, 0, 3, 9, 1, 7, 4, 8};
  27. BubbleSort(a, 10);
  28. printf("排序后的结果是:");
  29. for( i=0; i < 10; i++ )
  30. {
  31. printf("%d", a[i]);
  32. }
  33. printf("\n\n");
  34. return 0;
  35. }

2.选择排序

  1. #include <stdio.h>
  2. void SelectSort(int k[], int n)
  3. {
  4. int i, j, min, temp, count1=0, count2=0;
  5. for( i=0; i < n-1; i++ )
  6. {
  7. min = i;
  8. for( j=i+1; j < n; j++ )
  9. {
  10. count1++;
  11. if( k[j] < k[min] )
  12. {
  13. min = j;
  14. }
  15. }
  16. if( min != i )
  17. {
  18. count2++;
  19. temp = k[min];
  20. k[min] = k[i];
  21. k[i] = temp;
  22. }
  23. }
  24. printf("总共进行了%d次比较,进行了%d次移动!", count1, count2);
  25. }
  26. int main()
  27. {
  28. int i, a[10] = {5, 2, 6, 0, 3, 9, 1, 7, 4, 8};
  29. SelectSort(a, 10);
  30. printf("排序后的结果是:");
  31. for( i=0; i < 10; i++ )
  32. {
  33. printf("%d", a[i]);
  34. }
  35. printf("\n\n");
  36. return 0;
  37. }

3.直接排序

  1. #include <stdio.h>
  2. void InsertSort(int k[], int n)
  3. {
  4. int i, j, temp;
  5. for( i=1; i < n; i++ )
  6. {
  7. if( k[i] < k[i-1] )
  8. {
  9. temp = k[i];
  10. for( j=i-1; k[j] > temp; j-- )
  11. {
  12. k[j+1] = k[j];
  13. }
  14. k[j+1] = temp;
  15. }
  16. }
  17. }
  18. int main()
  19. {
  20. int i, a[10] = {5, 2, 6, 0, 3, 9, 1, 7, 4, 8};
  21. InsertSort(a, 10);
  22. printf("排序后的结果是:");
  23. for( i=0; i < 10; i++ )
  24. {
  25. printf("%d", a[i]);
  26. }
  27. printf("\n\n");
  28. return 0;
  29. }

4.希尔排序

  1. #include <stdio.h>
  2. void InsertSort(int k[], int n)
  3. {
  4. int i, j, temp;
  5. int gap = n;
  6. do
  7. {
  8. gap = gap/3 + 1;
  9. for( i=gap; i < n; i++ )
  10. {
  11. if( k[i] < k[i-gap] )
  12. {
  13. temp = k[i];
  14. for( j=i-gap; k[j] > temp; j-=gap )
  15. {
  16. k[j+gap] = k[j];
  17. }
  18. k[j+gap] = temp;
  19. }
  20. }
  21. }while(gap > 1);
  22. }
  23. int main()
  24. {
  25. int i, a[10] = {5, 2, 6, 0, 3, 9, 1, 7, 4, 8};
  26. InsertSort(a, 10);
  27. printf("排序后的结果是:");
  28. for( i=0; i < 10; i++ )
  29. {
  30. printf("%d", a[i]);
  31. }
  32. printf("\n\n");
  33. return 0;
  34. }

5.快速排序

  1. #include <stdio.h>
  2. void swap(int k[], int low, int high)
  3. {
  4. int temp;
  5. temp = k[low];
  6. k[low] = k[high];
  7. k[high] = temp;
  8. }
  9. int Partition(int k[], int low, int high)
  10. {
  11. int point;
  12. point = k[low];
  13. while( low < high )
  14. {
  15. while( low < high && k[high] >= point )
  16. {
  17. high--;
  18. }
  19. swap(k, low, high);
  20. while( low < high && k[low] <= point )
  21. {
  22. low++;
  23. }
  24. swap(k, low, high);
  25. }
  26. return low;
  27. }
  28. void QSort(int k[], int low, int high)
  29. {
  30. int point;
  31. if( low < high )
  32. {
  33. point = Partition(k, low, high);
  34. QSort(k, low, point-1);
  35. QSort(k, point+1, high);
  36. }
  37. }
  38. void QuickSort(int k[], int n)
  39. {
  40. QSort(k, 0, n-1);
  41. }
  42. int main()
  43. {
  44. int i, a[10] = {4, 2, 5, 0, 3, 9, 1, 7, 6, 8};
  45. QuickSort(a, 10);
  46. printf("排序后的结果是:");
  47. for( i=0; i < 10; i++ )
  48. {
  49. printf("%d", a[i]);
  50. }
  51. printf("\n\n");
  52. return 0;
  53. }