当前位置:   article > 正文

【海贼王的数据航海】排序——直接选择排序|堆排序

【海贼王的数据航海】排序——直接选择排序|堆排序

目录

1 -> 选择排序

1.1 -> 基本思想

1.2 -> 直接选择排序

1.2.1 -> 代码实现

1.3 -> 堆排序

1.3.1 -> 代码实现


1 -> 选择排序

1.1 -> 基本思想

每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,直到全部待排序的数据元素排完。

1.2 -> 直接选择排序

  • 在元素集合arr[i] -- arr[n - 1]中选择关键码最大(或最小)的数据元素
  • 若它不是这组元素中的最后一个(或第一个)元素,则将它与这组元素中的最后一个(或第一个)元素交换
  • 在剩余的arr[i] -- arr[n - 2] (arr[i + 1] -- arr[n - 1]) 集合中,重复上述步骤,直到集合剩余1个元素

直接选择排序的特性总结:

  1. 好理解,但效率不是很好,实际中很少使用
  2. 时间复杂度:O(N^{2})
  3. 空间复杂度:O(1)
  4. 稳定性:不稳定

1.2.1 -> 代码实现

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<stdbool.h>
  5. // 交换
  6. void Swap(int* x, int* y)
  7. {
  8. int tmp = *x;
  9. *x = *y;
  10. *y = tmp;
  11. }
  12. // 打印
  13. void PrintArray(int* a, int n)
  14. {
  15. for (int i = 0; i < n; i++)
  16. printf("%d ", a[i]);
  17. printf("\n");
  18. }
  19. // 选择排序
  20. void SelectSort(int* a, int n)
  21. {
  22. int begin = 0, end = n - 1;
  23. while (begin < end)
  24. {
  25. int maxi = begin, mini = begin;
  26. for (int i = begin; i <= end; i++)
  27. {
  28. if (a[i] > a[maxi])
  29. {
  30. maxi = i;
  31. }
  32. if (a[i] < a[mini])
  33. {
  34. mini = i;
  35. }
  36. }
  37. Swap(&a[begin], &a[mini]);
  38. // 如果maxi和begin重叠,修正一下即可
  39. if (begin == maxi)
  40. {
  41. maxi = mini;
  42. }
  43. Swap(&a[end], &a[maxi]);
  44. ++begin;
  45. --end;
  46. }
  47. }
  48. void TestSelectSort()
  49. {
  50. int a[] = { 9, 2, 6, 1, 7, 3 ,0, 5, 8, 4 };
  51. PrintArray(a, sizeof(a) / sizeof(int));
  52. SelectSort(a, sizeof(a) / sizeof(int));
  53. PrintArray(a, sizeof(a) / sizeof(int));
  54. }
  55. int main()
  56. {
  57. TestSelectSort();
  58. return 0;
  59. }

1.3 -> 堆排序

堆排序(Heapsort)是指利用堆积树(堆)这种数据结构所设计的一种排序算法,它是选择排序的一种。它是通过堆来进行选择数据。需要注意的是排升序要建大堆,排降序建小堆。

堆排序特性总结:

  1. 堆排序用堆来选数,效率较高
  2. 时间复杂度:O(N\cdot logN)
  3. 空间复杂度:O(1)
  4. 稳定性:不稳定

1.3.1 -> 代码实现

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<stdbool.h>
  5. // 交换
  6. void Swap(int* x, int* y)
  7. {
  8. int tmp = *x;
  9. *x = *y;
  10. *y = tmp;
  11. }
  12. // 打印
  13. void PrintArray(int* a, int n)
  14. {
  15. for (int i = 0; i < n; i++)
  16. printf("%d ", a[i]);
  17. printf("\n");
  18. }
  19. // 堆排序
  20. void AdjustUp(int* a, int child)
  21. {
  22. int father = (child - 1) / 2;
  23. while (child > 0)
  24. {
  25. if (a[child] > a[father])
  26. {
  27. Swap(&a[child], &a[father]);
  28. //更新下标
  29. child = father;
  30. father = (father - 1) / 2;
  31. }
  32. else
  33. {
  34. break;//一旦符合小堆了,就直接退出
  35. }
  36. }
  37. }
  38. void AdjustDown(int* a, int n, int parent)
  39. {
  40. int child = parent * 2 + 1;
  41. while (child < n)
  42. {
  43. // 找出小的那个孩子
  44. if (child + 1 < n && a[child + 1] > a[child])
  45. {
  46. ++child;
  47. }
  48. if (a[child] > a[parent])
  49. {
  50. Swap(&a[child], &a[parent]);
  51. parent = child;
  52. child = parent * 2 + 1;
  53. }
  54. else
  55. {
  56. break;
  57. }
  58. }
  59. }
  60. // 排升序
  61. void HeapSort(int* a, int n)
  62. {
  63. // 建大堆
  64. for (int i = (n - 1 - 1) / 2; i >= 0; --i)
  65. {
  66. AdjustDown(a, n, i);
  67. }
  68. int end = n - 1;
  69. while (end > 0)
  70. {
  71. Swap(&a[0], &a[end]);
  72. AdjustDown(a, end, 0);
  73. --end;
  74. }
  75. }
  76. void TestHeapSort()
  77. {
  78. int a[] = { 9, 2, 6, 1, 7, 3 ,0, 5, 8, 4 };
  79. PrintArray(a, sizeof(a) / sizeof(int));
  80. HeapSort(a, sizeof(a) / sizeof(int));
  81. PrintArray(a, sizeof(a) / sizeof(int));
  82. }
  83. int main()
  84. {
  85. TestHeapSort();
  86. return 0;
  87. }


感谢大佬们的支持!!!

互三啦!!!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/431143
推荐阅读
相关标签
  

闽ICP备14008679号