基於數組的堆排序演算法的C語言實現

來源:互聯網
上載者:User

實現如下:

int getParent(int c);int getLeft(int p);int getRight(int p);void swap(int *p1, int *p2);void heap_sort(int *source, int length);void max_heapify(int *source, int length, int loc);void build_max_heap(int *source, int length);void heap_sort_iteration(int *source, int length);void max_heapify_iteration(int *source, int length, int loc);void build_max_heap_iteration(int *source, int length);int getParent(int c) {    return (c - 1) / 2;}int getLeft(int p) {    return p * 2 + 1;}int getRight(int p) {    return p * 2 + 2;}void swap(int *p1, int *p2) {    int temp = *p1;    *p1 = *p2;    *p2 = temp;}void max_heapify(int *source, int length, int loc) {    int l = getLeft(loc);    int r = getRight(loc);    int max = loc;    if (l < length && source[l] > source[max])        max = l;    if (r < length && source[r] > source[max])        max = r;    if (max == loc)        return;    swap(source + max, source + loc);    max_heapify(source, length, max);}void build_max_heap(int *source, int length) {    for (int i = getParent(length - 1); i >= 0; --i) {        max_heapify(source, length, i);    }}void heap_sort(int *source, int length) {    build_max_heap(source, length);    for (int i = length - 1; i > 0; --i) {        swap(source, source + i);        max_heapify(source, i, 0);    }}void max_heapify_iteration(int *source, int length, int loc) {    int l, r;    int max = loc;    while (loc < length) {        l = getLeft(loc);        r = getRight(loc);        if (l < length && source[l] > source[max])            max = l;        if (r < length && source[r] > source[max])            max = r;        if (max == loc)            return;        swap(source + max, source + loc);        loc = max;    }}void build_max_heap_iteration(int *source, int length) {    for (int i = getParent(length - 1); i >= 0; --i) {        max_heapify_iteration(source, length, i);    }}void heap_sort_iteration(int *source, int length) {    build_max_heap(source, length);    for (int i = length - 1; i > 0; --i) {        swap(source, source + i);        max_heapify_iteration(source, i, 0);    }}

堆排序是不穩定原地的排序。
該程式中使用的數組作為表示堆的資料結構,其計算左子女,右子女以及父節點的座標的方式如parent,left_child,right_child所示。
max_heapify使其保持最大堆的性質,假設p節點左右兩顆子樹都是最大堆,max_heapify使p節點保持最大堆的性質,選取p節點的左右子女中最大的與p節點交換,然後遞迴的對交換後的節點繼續進行max_heapify操作,直到到達葉子結點。max_heapify操作已耗用時間的漸近上界為 Ο(h) Ο(h)h為待動作節點的高度。
build_max_heap將一個數組建立為最大堆,從最後一個非葉子節點到根節點依次分別調用max_heapify函數即可。其操作時間的漸近確界為 Ο(n) Ο(n)。
heap_sort函數對n個數進行堆排序,每次將最大堆的第一個元素和最後一個元素交換,得到最大的元素,將堆的元素個數減一後再對堆的根節點進行max_heapify操作,得到當前堆中最大元素,迴圈n-1次即可排序數組。其操作時間的漸近確界為 Ο(nlogn) Ο(nlogn)。
其上所有演算法複雜度計算方法見演算法導論第六章,涉及主定理不再贅述。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.