七大排序之希爾排序

2022-08-17 07:15:14 字數 1749 閱讀 1379

希爾排序可以說是對插入排序的優化,所以又可以稱希爾排序為分組插入排序;

插入排序速度快是在資料量較小且基本有序倆個條件下有效,如果資料過長且基本無序,採用希爾排序將其分解成若干組,分別做插入排序。

核心**:

1

void shellsort(int* arr, int

length)

23//

不能改成arr[i]因為不一定到最前面

24 arr[k+increasement] =temp;25}

26}27}

2829 } while (increasement>1

);30 }

與插入排序做對比:

1 #include2 #include3 #include4 #include5

using

namespace

std;

6const

int max = 10000;7

8void swap(int& a, int&b)

1314

long

getsystemtime()

19void print(const

int* arr, int

length) 23}

24void insertsort(int* arr, int

length) 36}

37 arr[k + 1] =temp;38}

39}40}

41void shellsort(int* arr, int

length)

63//

不能改成arr[i]因為不一定到最前面

64 arr[k+increasement] =temp;65}

66}67}

6869 } while (increasement>1

);70}71

intmain()

79//

cout << "排序前:\n";

80//

print(arr, max);

81long pt =getsystemtime();

82shellsort(arr, max);

83long at =getsystemtime();

84//

cout << "\n排序後:\n";

85//

print(arr, max);

86 cout << "

\ntime of shellsort:

"<< at - pt << "

ms\n";

87 pt =getsystemtime();

88insertsort(arr2, max);

89 at =getsystemtime();

90 cout << "

\ntime of insertsort:

"<< at - pt << "

ms\n";

91return0;

92 }

max=100

max=1000

max=10000

資料一大,差距立馬就出來了。

七大排序之希爾排序

希爾排序的實質就是分組插入排序,該方法又稱 縮小增量排序 因dl shell於1959年提出而得名。該方法的基本思想是 先將整個待排元素序列分割成若干個子串行 由相隔某個 增量 的元素 組成的 分別進行直接插入排序,然後依次縮減增量再進行排序,待整個序列中的元素基本有序 增量足夠小 時,再對全體元素...

七大排序演算法之希爾排序

這個排序演算法,其實就是乙個插入排序的改進,可以作為知識的擴充套件來學習,由於平時用的不多,所以先記錄在這裡,以後要用不至於沒有頭緒。希爾排序在陣列中採用跳躍式分組的策略,通過某個增量將陣列元素劃分為若干組,然後分組進行插入排序,隨後逐步縮小增量,繼續按組進行插入排序操作,直至增量為1 平均情況 介...

七大排序之堆排序

堆排序與快速排序,歸併排序一樣都是時間複雜度為o n logn 的幾種常見排序方法。學習堆排序前,先講解下什麼是資料結構中的二叉堆。二叉堆是完全二叉樹或者是近似完全二叉樹。二叉堆滿足二個特性 1 父結點的鍵值總是大於或等於 小於或等於 任何乙個子節點的鍵值。2 每個結點的左子樹和右子樹都是乙個二叉堆...