• 冒泡排序给cpu干懵了 哈哈 还有希尔排序 算法补充(学习笔记)


    直接给出代码 

    1. #include
    2. #include
    3. #include "Student.h"
    4. #include "sorttesthelper.h"
    5. #include "BubbleSort.h"
    6. using namespace std;
    7. template<typename T>
    8. void shellSort(T arr[], int n){
    9. // 计算 increment sequence: 1, 4, 13, 40, 121, 364, 1093...
    10. int h = 1;
    11. while( h < n/3 )
    12. h = 3 * h + 1;
    13. while( h >= 1 ){
    14. // h-sort the array
    15. for( int i = h ; i < n ; i ++ ){
    16. // 对 arr[i], arr[i-h], arr[i-2*h], arr[i-3*h]... 使用插入排序
    17. T e = arr[i];
    18. int j;
    19. for( j = i ; j >= h && e < arr[j-h] ; j -= h )
    20. arr[j] = arr[j-h];
    21. arr[j] = e;
    22. }
    23. h /= 3;
    24. }
    25. }
    26. template<typename T >
    27. void selectionSort( T arr[], int n){
    28. for(int i = 0 ; i < n ; i++){
    29. //寻找【i,n之间的最小值】
    30. int minIndex = i;
    31. for( int j = i + 1 ; j < n ; j++)
    32. if(arr[j] < arr[minIndex] )
    33. minIndex = j;
    34. swap( arr[i] , arr[minIndex]);
    35. }
    36. }
    37. //对插入排序来说,直接从第二个元素开始
    38. template<typename T >
    39. void InsertSort( T arr[], int n){
    40. for(int i = 1 ; i < n ; i++){
    41. T e = arr[i];
    42. // j 需要保存元素e应该插入的位置
    43. int j;
    44. //寻找【i应该插入的位置】,但是注意我们是从后面往前找所以j 要从后往前
    45. // for( int j = i ; j > 0 ; j --)
    46. // if(arr[j] < arr[j - 1] )
    47. // swap(arr[j], arr[j-1]);
    48. // else
    49. // break;
    50. //插入排序的优点是 可以提前 终止循环 所以对于几乎有序的序列 插入排序的性能非常强
    51. for( j = i ; j > 0 && arr[j-1] > arr[e]; j --)
    52. arr[j] = arr[j-1];
    53. arr[j] = arr[e];
    54. }
    55. }
    56. int main()
    57. {
    58. int a[5] = {5,62,3,58,44};
    59. selectionSort( a, 5 );
    60. for( int i = 0 ; i < 5 ; i++)
    61. cout<" ";
    62. cout<
    63. float b[4] = {4.4,2.3,5.63};
    64. selectionSort( b , 3);
    65. for( int i = 0 ; i < 3 ; i++)
    66. cout<" ";
    67. cout<
    68. string c[2] = {"z","b"};
    69. selectionSort( c , 2);
    70. for( int i = 0 ; i < 2 ; i++)
    71. cout<" ";
    72. cout<
    73. Student d[3] = {{"D",90} , {"C",89} , { "B", 114}};
    74. selectionSort( d , 3);
    75. for( int i = 0 ; i < 3 ; i++)
    76. cout<
    77. cout<
    78. int n = 100000;
    79. int *arr1 = SortTestHelper :: generateRandomArr(n, 0, n) ;
    80. int *arr2 = SortTestHelper :: copyIntArray(arr1, n);
    81. int *arr3 = SortTestHelper :: generateNearlyorderedArr(n, 100);
    82. int *arr4 = SortTestHelper::copyIntArray(arr1, n);
    83. int *arr5 = SortTestHelper::copyIntArray(arr1, n);
    84. // InsertSort(arr2, n);
    85. // SortTestHelper :: printarr(arr2, n);
    86. // selectionSort( arr, n );
    87. // SortTestHelper :: printarr(arr, n);
    88. // SortTestHelper::test_sort("selection Sort", selectionSort, arr,n);
    89. SortTestHelper::test_sort("Insertion Sort", InsertSort, arr2,n);
    90. SortTestHelper::test_sort("Insertion Sort a nearly ordered arr", InsertSort, arr3,n);
    91. SortTestHelper::test_sort("Bubble Sort", bubbleSort, arr4, n);
    92. SortTestHelper::test_sort("Shell Sort", shellSort, arr5, n);
    93. delete[] arr1;
    94. delete[] arr2;
    95. delete[] arr3;
    96. delete[] arr4;
    97. delete[] arr5;
    98. return 0;
    99. }

    下面是单独的冒泡排序 

    https://github.com/liuyubobobo/Play-with-Algorithms/blob/master/02-Sorting-Basic/Course%20Code%20(C%2B%2B)/Optional-03-Shell-Sort/main.cpp

    给出大佬的链接

    1. //
    2. // Created by liuyubobobo on 7/15/16.
    3. //
    4. #ifndef OPTIONAL_02_SHELL_SORT_BUBBLESORT_H
    5. #define OPTIONAL_02_SHELL_SORT_BUBBLESORT_H
    6. #include
    7. #include
    8. using namespace std;
    9. template<typename T>
    10. void bubbleSort( T arr[] , int n){
    11. int newn; // 使用newn进行优化
    12. do{
    13. newn = 0;
    14. for( int i = 1 ; i < n ; i ++ )
    15. if( arr[i-1] > arr[i] ){
    16. swap( arr[i-1] , arr[i] );
    17. // 记录最后一次的交换位置,在此之后的元素在下一轮扫描中均不考虑
    18. newn = i;
    19. }
    20. n = newn;
    21. }while(newn > 0);
    22. }
    23. #endif //OPTIONAL_02_SHELL_SORT_BUBBLESORT_H

  • 相关阅读:
    【一、灵犀考试系统项目设计、框架搭建】
    用AI绘画-Stable Diffusion稳定生成指定人物的2-3人场景图,制作小说配图从未如此轻松!
    【java】【SpringBoot】【二】运维实用篇 SpringBoot工程
    selenium环境搭建
    动环监控系统的主要功能,动环监控系统的监控对象有哪些
    前端Vue 结合xlxs库实现解析excel文件,并动态组装表头!
    Android内存泄漏
    Python基础学习笔记【最好拥有一定Java基础】
    C# 通过自定义控件实现炫酷的时间显示
    WPF 控件专题 ProgressBar控件详解
  • 原文地址:https://blog.csdn.net/qq_68308828/article/details/133930452