堆排序和選擇排序


    選擇排序的執行過程為每次循環遍歷數組找出最小(或最大)的數,將其放在數組的有序數列的最后面,每次第i次遍歷查找要執行N-i個單位時間,然后要執行N次,故時間復雜度為O(N^2),很簡單,比較適合較小的數列的排序。

    選擇排序的代碼selection_sort.cpp會在下面的完整代碼中呈現。

    而堆排序是對於選擇排序的優化排序,它利用率了最大(最小)堆頂的數最大(最小)的性質,使得找到一個數組找到最大(最小)的元素的操作不需要像選擇排序一樣消耗N-i的時間。其時間復雜度為O(nlogn),空間復雜度為O(1)。

在介紹堆排序的執行過程前,先要了解幾個公式:

     對於一個根節點 i,其左子樹為 2*i+1,其右子樹為 2*i+2 ,而最后一個有子樹的根節點 a 的位置小於等於 N/2,N是待排序數組的長度。

 

本文中有關堆排序的介紹圖片都是轉自下面的作者的博客:

作者: dreamcatcher-cx

出處: <http://www.cnblogs.com/chengxiao/>

 

其執行過程如下:

1.先建立最大(最小)堆(build_heap)

     1.1 將數組導入一顆完全二叉樹;

 

     1.2 從倒數第一個有子樹的根節點開始建立堆(heapify)(操作就是通過比較和變換使得根節點的大小大於(小於)子樹的大小。),然后對前面一個根節點做同樣的循環操作,直到堆頂也操作結束,則完成建立整個堆。

在heapify的過程中,我們要在改變了一個子樹跟根節點位置后,再向下調整其子樹的子樹和其子樹的位置,直至最后一個子樹。

 

這部分代碼如下:

void heap_sort::heapify(int Array[],int i,int n)//最大堆
{    
    int child;
    for (; Lchild(i) < n; i = child)//這個for循環可以看出,heapify的思想是通過自頂向下的方法調整最大堆,
    {                               //並且只調整改變過位置的堆,這就要求下面的已經是最大(小)堆。
        child = Lchild(i);          //因為假如下面的樹不符合堆的話,若上面的樹已經形成堆了那么下面的就不會再作調整。
        if (child + 1 < n&&Array[child] < Array[child + 1])//最終可能無法得出整個堆。
            child++;
        if (Array[i] < Array[child])
        {
            int temp = Array[i];
            Array[i] = Array[child];
            Array[child] = temp;
        }
        else
            break;
    }

}
void heap_sort::build_heap(int Array[], int n)
{
    for (int i = n / 2; i >= 0; i--)//從最后的(往往是從沒有子樹的根節點開始,差不了多少)有子樹的根節點開始。
    {
        heap_sort::heapify(Array, i, n);
    }
}

2. 交換堆頂與堆末尾的數值,並且調整堆的長度后,重新調整成最大(最小)堆,一直循環直至堆中只剩一個堆頂元素。這個過程實際上大部分人已經能理解了。如下圖所示:

 

最終得到圖如下:

這部分代碼如下:

void heap_sort::sort_heap(int Array[], int n)
{
    heap_sort::build_heap(Array, n);
    for (int i = n - 1; i > 0; i--)
    {
        int temp = Array[i];
        Array[i] = Array[0];
        Array[0] = temp;
        heap_sort::heapify(Array, 0, i);//因為下面的已經是堆,所以上面經過變換后變成無序樹后只需要自頂向下調整即可。
    }

}

完整的工程代碼如下:

Sort.h

//自己編寫的各種排序算法的頭文件。
#ifndef _SORT_H
#define _SORT_H
//冒泡排序
class bubble_sort{
private:
    int *Array,Length;
public:
    bubble_sort(int *Array1,int Length);
    int *sort_bubble();

};
//歸並排序
class merge_sort{
public:
     static void sort_merge(int Array1[], int Array2[], int Left, int Rightend, int Right,int recursion);
     static void Merge(int Array1[], int Array2[], int Left, int Rightend);//遞歸方法
     static void Merge_pass(int Array1[], int Array2[], int ordered_len,int Length);
     static void Merge1(int Array1[], int Array2[], int Length);//非遞歸方法
};
//快速排序
class quick_sort{
public:
    static void sort_quick(int Array1[],int Left,int right);
    
};
//插入排序
class insertion_sort{
public:
    static void sort_insertion(int Array[], int n);
};
//堆排序
class heap_sort{
public:
    static void sort_heap(int Array[], int n);
    static void build_heap(int Array[], int n);
    static void heapify(int Array[], int i, int n);
};
//選擇排序
class selection_sort{
public:
    static void sort_selection(int Array[], int n);

};
#endif

heap_sort.cpp

#include "Sort.h"
#define Lchild(i) (2*i+1)//定義對於傳入的根節點算出相應的左子節點
void heap_sort::heapify(int Array[],int i,int n)//最大堆
{    
    int child;
    for (; Lchild(i) < n; i = child)//這個for循環可以看出,heapify的思想是通過自頂向下的方法調整最大堆,
    {                               //並且只調整改變過位置的堆,這就要求下面的已經是最大(小)堆。
        child = Lchild(i);          //因為假如下面的樹不符合堆的話,若上面的樹已經形成堆了那么下面的就不會再作調整。
        if (child + 1 < n&&Array[child] < Array[child + 1])//最終可能無法得出整個堆。
            child++;
        if (Array[i] < Array[child])
        {
            int temp = Array[i];
            Array[i] = Array[child];
            Array[child] = temp;
        }
        else
            break;
    }

}
void heap_sort::build_heap(int Array[], int n)
{
    for (int i = n / 2; i >= 0; i--)//從最后的(往往是從沒有子樹的根節點開始,差不了多少)有子樹的根節點開始。
    {
        heap_sort::heapify(Array, i, n);
    }
}
void heap_sort::sort_heap(int Array[], int n)
{
    heap_sort::build_heap(Array, n);
    for (int i = n - 1; i > 0; i--)
    {
        int temp = Array[i];
        Array[i] = Array[0];
        Array[0] = temp;
        heap_sort::heapify(Array, 0, i);//因為下面的已經是堆,所以上面經過變換后變成無序樹后只需要自頂向下調整即可。
    }

}

selection_sort.cpp

#include "Sort.h"
void selection_sort::sort_selection(int Array[], int n)
{
    for (int i = 0; i < n; i++)
    {
        int min_location = i;
        
        for (int j = n - 1; j >i; j--)
        {
            if (Array[j] < Array[min_location])
                min_location = j;
        }
        int temp;
        temp = Array[min_location];
        Array[min_location] = Array[i];
        Array[i] = temp;
    }
}

 

main.cpp

#include <iostream>
#include "Sort.h"
using namespace std;
void main()
{
    int Array[] = { 7, 5, 2, 1, 3, 7,93, 6,5, 10, 100, 98, 44, 66, 23 };
    int Array2[] = { 7, 5, 2, 1, 3, 7, 93, 6, 5, 10, 100, 98, 44, 66, 23 };
    int n = sizeof(Array) / sizeof(int);
    int n2 = sizeof(Array2) / sizeof(int);
    heap_sort::sort_heap(Array, n);
    selection_sort::sort_selection(Array2, n2);
    cout << "堆排序:" << endl;
    for (int i = 0; i < n; i++)
    {
        cout << Array[i] << endl;
    }
    cout << "選擇排序:" << endl;
    for (int i = 0; i < n; i++)
    {
        cout << Array2[i] << endl;
    }
    system("pause");
}

 運行結果如下:

到此介紹結束,歡迎交流。

作者: 十面埋伏但莫慌

出處: <https://www.cnblogs.com/leo-lv/>

轉載請注明出處及原文鏈接,謝謝!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM