溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C++ 數(shù)據(jù)結(jié)構(gòu) 堆排序的實(shí)現(xiàn)

發(fā)布時(shí)間:2020-09-06 00:57:23 來源:腳本之家 閱讀:134 作者:lqh 欄目:編程語言

堆排序(heapsort)是一種比較快速的排序方式,它的時(shí)間復(fù)雜度為O(nlgn),并且堆排序具有空間原址性,任何時(shí)候只需要有限的空間來存儲(chǔ)臨時(shí)數(shù)據(jù)。我將用c++實(shí)現(xiàn)一個(gè)堆來簡單分析一下。

堆排序的基本思想為:

1、升序排列,保持大堆;降序排列,保持小堆;

2、建立堆之后,將堆頂數(shù)據(jù)與堆中最后一個(gè)數(shù)據(jù)交換,堆大小減一,然后向下調(diào)整;直到堆中只剩下一個(gè)有效值;

下面我將簡單分析一下:

第一步建立堆:

1、我用vector順序表表示數(shù)組;

2、用仿函數(shù)實(shí)現(xiàn)大小堆隨時(shí)切換,實(shí)現(xiàn)代碼復(fù)用;

3、實(shí)現(xiàn)向下調(diào)整算法,時(shí)間復(fù)雜度為O(lgn);

下面是我用某教材中的一個(gè)建最小堆的過程圖,希望能更直觀一些:

C++ 數(shù)據(jù)結(jié)構(gòu) 堆排序的實(shí)現(xiàn)

為了保證復(fù)用性,用仿函數(shù)重載了(),下面是復(fù)用的向下調(diào)整算法:

void _AdjustDown(int root,int size) 
{ 
  Camper camper;    //仿函數(shù) 
  int parent = root; 
  int child = parent * 2 + 1; 
  while (child <= size) //保證訪問不越界 
  { 
    if (child < size && camper(_vec[child+1] , _vec[child])) //保證存在右子樹、同時(shí)判斷右子樹是否大于或小于左子樹 
    { 
      child++; 
    } 
    if (camper(_vec[child], _vec[parent])) 
    { 
      swap(_vec[parent], _vec[child]); 
      parent = child; 
      child = parent * 2 + 1; 
    } 
    else 
    { 
      break; 
    } 
  } 
 
} 

排序算法思想:

1、將堆頂數(shù)據(jù)與堆中最后一個(gè)數(shù)據(jù)交換;

2、堆大小減一,然后調(diào)用向下調(diào)整算法;

3、結(jié)束條件:堆中剩下一個(gè)有效值;

排序算法實(shí)現(xiàn):

void Sort() 
{ 
  size_t size = _vec.size(); //數(shù)據(jù)數(shù)量 
  while (size > 1)   
  { 
    swap(_vec[0], _vec[size - 1]); 
    size--; 
    _AdjustDown(size); 
  } 
} 

仿函數(shù)的實(shí)現(xiàn):

template<class T> 
struct Greater //大于 
{ 
  bool operator ()(const T& l, const T& p) 
  { 
    return l > p; 
  } 
}; 
 
template<class T> 
struct Less //小于 
{ 
  bool operator () (const T&l, const T& p) 
  { 
    return l < p; 
  } 
}; 

完整的代碼實(shí)現(xiàn):

#include<iostream> 
using namespace std; 
 
#include<vector> 
 
 
template<class T> 
struct Greater //大于 
{ 
  bool operator ()(const T& l, const T& p) 
  { 
    return l > p; 
  } 
}; 
 
template<class T> 
struct Less //小于 
{ 
  bool operator () (const T&l, const T& p) 
  { 
    return l < p; 
  } 
}; 
 
template<class T,class Camper> 
class HeapSort //建大堆 
{ 
public: 
  HeapSort() 
  {} 
  HeapSort(T* arr, size_t n) 
  { 
    _vec.reserve(n); 
    if (arr != NULL) 
    { 
      for (size_t i = 0; i < n; i++) 
      { 
        _vec.push_back(arr[i]); 
      } 
    } 
     
    _AdjustDown(_vec.size()); 
  } 
 
  void Sort() 
  { 
    size_t size = _vec.size(); //數(shù)據(jù)數(shù)量 
    while (size > 1)   
    { 
      swap(_vec[0], _vec[size - 1]); 
      size--; 
      _AdjustDown(size); 
    } 
  } 
  void Print() 
  { 
    for (size_t i = 0; i < _vec.size(); i++) 
    { 
      cout << _vec[i] <<" "; 
    } 
    cout << endl; 
  } 
protected: 
  void _AdjustDown(int size) 
  { 
    int parent = (size - 2) / 2; 
    while (parent >= 0) 
    { 
      _AdjustDown(parent, size - 1); 
      parent--; 
    } 
  } 
  void _AdjustDown(int root,int size) 
  { 
    Camper camper;    //仿函數(shù) 
    int parent = root; 
    int child = parent * 2 + 1; 
    while (child <= size) //保證訪問不越界 
    { 
      if (child < size && camper(_vec[child+1] , _vec[child])) //保證存在右子樹、同時(shí)判斷右子樹是否大于或小于左子樹 
      { 
        child++; 
      } 
      if (camper(_vec[child], _vec[parent])) 
      { 
        swap(_vec[parent], _vec[child]); 
        parent = child; 
        child = parent * 2 + 1; 
      } 
      else 
      { 
        break; 
      } 
    } 
 
  } 
private: 
  vector<T> _vec; 
}; 

測(cè)試用例代碼:

void TextSort() 
{ 
  int a[] = { 10, 11, 13, 12, 16, 18, 15, 17, 14, 19 }; 
  HeapSort<int,Greater<int>> h(a, sizeof(a) / sizeof(a[0])); 
  h.Print(); 
  h.Sort(); 
  h.Print(); 
} 

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI