各种排序算法C++模版类实现

来源:互联网 发布:mac ppt 设置动画顺序 编辑:程序博客网 时间:2024/05/21 09:12

闲来无事,于是把常用的排序算法自己写了一遍,也当做是复习一下。

[cpp] view plaincopyprint?
  1. /***************************************************************  
  2.  *                                                             *  
  3.  *                                                             *  
  4.  *              Date   : 2012. 05. 03                          *  
  5.  *              Author : 397090770                             *  
  6.  *              Email  : wyphao.2007@163.com                   *  
  7.  *                                                             *  
  8.  *                                                             * 
  9.  ***************************************************************/   
  10.   
  11. #include <iostream>  
  12. #include <iomanip.h>   
  13.   
  14. #define LEN 100  //排序数的个数   
  15. #define NUM 10   //每行输出的字数个数   
  16.    
  17. using namespace std;  
  18.   
  19. ////////////////////////////////////////////////////////////////////   
  20. //树节点   
  21. template <class T>   
  22. class Node{   
  23. public:  
  24.     Node *left;  
  25.     Node *right;  
  26.     T data;  
  27.       
  28.     Node() : left(NULL), right(NULL), data(NULL){}  
  29.     ~Node(){}   
  30. };   
  31.   
  32. ////////////////////////////////////////////////////////////////////   
  33. class Sort{  
  34. public:  
  35.     Sort(){};     
  36.     ~Sort(){};  
  37.     //快速排序   
  38.     template <class T>  
  39.     void QuickSort(T arr[], int low, int hight);  
  40.       
  41.     //选择排序   
  42.     template <class T>  
  43.     void SelectSort(T arr[], int len);  
  44.       
  45.     //冒泡排序   
  46.     template <class T>  
  47.     void BubbleSort(T arr[], int len);  
  48.       
  49.     //插入排序   
  50.     template <class T>  
  51.     void InsertSort(T arr[], int len);  
  52.       
  53.     //堆排序   
  54.     template <class T>  
  55.     void HeapSort(T arr[], int len);  
  56.       
  57.     //二叉排序树排序   
  58.     template <class T>  
  59.     void TreeSort(T arr[], int len);   
  60.       
  61. private:  
  62.     //快速排序中选择中心点   
  63.     template <class T>  
  64.     int Quick(T arr[], int left, int right);  
  65.       
  66.     //建立堆   
  67.     template <class T>  
  68.     void CreateHeap(T arr[], int root, int len);  
  69.       
  70.     //建立二叉排序树   
  71.     template <class T>  
  72.     Node<T>* BuildTree(Node<T>*root, T data);   
  73.       
  74.     //中序遍历二叉排序树   
  75.     template <class T>  
  76.     void InTree(Node<T> *root, T arr[]);     
  77. };  
  78.   
  79. ////////////////////////////////////////////////////////////////////   
  80. int main(){  
  81.     Sort sort;  
  82.     int *arr;                               //需要排序的数组   
  83.     int width = 0;                          //最大数的位数,用于排列输出结果    
  84.     int len = LEN;                          //用来求最大数的位数  
  85.     arr = (int *)malloc(LEN * sizeof(int)); //分配空间   
  86.     if(arr == NULL){                        //空间分配失败   
  87.         cout << "Malloc failed!" << endl;  
  88.         exit(1);   
  89.     }   
  90.       
  91.     srand(time(NULL));                      //设置种子   
  92.     for(int i = 0; i < LEN ;i++){            //随机生成数字   
  93.         arr[i] = (rand() % (LEN * 10)) + 1;    
  94.     }   
  95.       
  96.     //sort.SelectSort(arr, LEN);  
  97.     sort.TreeSort(arr, LEN);  
  98.       
  99.     //求得最大数的位数,用于排列输出结果   
  100.     while(len){  
  101.         width++;  
  102.         len /= 10;   
  103.     }  
  104.        
  105.     for(int i = 0; i < LEN; i++){            //输出排序后的数字   
  106.         cout << setw(width) << arr[i] << " ";  
  107.         cout << fixed;  
  108.         if((i + 1) % NUM == 0){             //每行输出的数字个数   
  109.             cout << endl;   
  110.         }   
  111.     }  
  112.       
  113.     cout << endl;  
  114.     return 0;  
  115. }  
  116.   
  117. ////////////////////////////////////////////////////////////////////   
  118. //快速排序   
  119. template <class T>  
  120. void Sort::QuickSort(T arr[], int low, int hight){  
  121.     int pivot = -1;  
  122.     if(low <= hight){  
  123.         pivot = Quick(arr, low, hight);  
  124.         QuickSort(arr, low, pivot - 1);  
  125.         QuickSort(arr, pivot + 1, hight);  
  126.     }  
  127. }  
  128.   
  129. //返回中轴点的下标   
  130. template <class T>  
  131. int Sort::Quick(T arr[], int left, int right){  
  132.     int i = left + 1, j = right;  
  133.     int flag = left;  
  134.     int temp;  
  135.       
  136.     while(i <= j){  
  137.         while(i <= j && arr[i] < arr[flag]){  
  138.             ++i;  
  139.         }  
  140.         while(i <= j && arr[j] > arr[flag]){  
  141.             --j;  
  142.         }  
  143.         if(i < j){  
  144.             temp = arr[i];  
  145.             arr[i] = arr[j];  
  146.             arr[j] = temp;  
  147.             ++i;  
  148.             --j;          
  149.         }  
  150.     }  
  151.       
  152.     temp = arr[flag];  
  153.     arr[flag] = arr[j];  
  154.     arr[j] = temp;  
  155.     return j;  
  156. }  
  157.   
  158. ////////////////////////////////////////////////////////////////////   
  159. //选择排序   
  160. template <class T>  
  161. void Sort::SelectSort(T arr[], int len){  
  162.     int index;  
  163.     T temp;  
  164.     for(int i = 0; i < len - 1; i++){  
  165.         index = i;  
  166.         for(int j = i + 1; j < len; j++){  
  167.             if(arr[index] > arr[j]){  
  168.                 index = j;  
  169.             }  
  170.         }  
  171.         if(index != i){  
  172.             temp = arr[index];  
  173.             arr[index] = arr[i];  
  174.             arr[i] = temp;  
  175.         }  
  176.     }         
  177. }  
  178.   
  179. ////////////////////////////////////////////////////////////////////   
  180. //冒泡排序  
  181. template <class T>  
  182. void Sort::BubbleSort(T arr[], int len){  
  183.     T temp;  
  184.     bool flags = true;   
  185.     for(int i = len - 1; i > 0; i--){  
  186.         if(flags){   
  187.             flags = false;   
  188.             for(int j = 0; j < i; j++){  
  189.                 if(arr[j] > arr[j + 1]){  
  190.                     flags = true;  
  191.                     temp = arr[j];  
  192.                     arr[j] = arr[j + 1];  
  193.                     arr[j + 1] = temp;  
  194.                     for(int k = 0; k < LEN; k++){  
  195.                         cout << arr[k] << " ";   
  196.                     }   
  197.                     cout << endl;  
  198.                 }  
  199.             }  
  200.         }else{  
  201.             break;   
  202.         }   
  203.     }   
  204. }   
  205.   
  206. ////////////////////////////////////////////////////////////////////   
  207. //插入排序  
  208. template <class T>  
  209. void Sort::InsertSort(T arr[], int len){  
  210.     T temp;   
  211.     int i, j;   
  212.     for(i = 1; i < len; i++){  
  213.         temp = arr[i];   
  214.         for(j = i - 1; j >= 0; j--){  
  215.             if(temp < arr[j]) {  
  216.                 arr[j + 1] = arr[j];   
  217.             }else{  
  218.                 break;  
  219.             }   
  220.         }  
  221.         arr[j + 1] = temp;    
  222.     }   
  223. }  
  224.   
  225. ////////////////////////////////////////////////////////////////////   
  226. //堆排序  
  227. template <class T>  
  228. void Sort::HeapSort(T arr[], int len){  
  229.     int i;   
  230.     T buff;   
  231.     T *temp = (T *)malloc(sizeof(T) * (len + 1));   
  232.     if(temp == NULL){  
  233.         cout << "Malloc Error!" << endl;  
  234.         exit(1);   
  235.     }   
  236.     for(i = 1; i < len + 1; i++){ //复制数组,使得偏移从1开始,这样好计算左孩子和右孩子坐标   
  237.         temp[i] = arr[i - 1];   
  238.     }  
  239.       
  240.     //建立子堆   
  241.     for(i = len / 2; i >= 1; i--){  
  242.         CreateHeap(temp, i, len);  
  243.     }  
  244.       
  245.     for(i = len - 1; i >= 1; i--){  
  246.         buff = temp[1];  
  247.         temp[1] = temp[i + 1];  
  248.         temp[i + 1] = buff;   
  249.           
  250.         CreateHeap(temp, 1, i);   
  251.     }  
  252.       
  253.     for(i = 1; i < len + 1; i++){  
  254.         arr[i - 1] = temp[i];   
  255.     }   
  256. }   
  257.   
  258. //建立堆   
  259. template <class T>  
  260. void Sort::CreateHeap(T arr[], int root, int len){  
  261.     int j = 2 * root;                   //root's left child, right (2 * root + 1)   
  262.     T temp = arr[root];  
  263.     bool flags = false;   
  264.       
  265.     while(j <= len && !flags){  
  266.         if(j < len){  
  267.             if(arr[j] < arr[j + 1]){     // Left child is less then right child   
  268.                 ++j;                        // Move the index to the right child   
  269.             }     
  270.         }  
  271.           
  272.         if(temp < arr[j]){  
  273.             arr[j / 2] = arr[j];  
  274.             j *= 2;   
  275.         }else{  
  276.             flags = true;   
  277.         }   
  278.     }   
  279.     arr[j / 2]  = temp;   
  280. }   
  281.   
  282. ////////////////////////////////////////////////////////////////////   
  283. //二叉排序树排序   
  284. template <class T>  
  285. void Sort::TreeSort(T arr[], int len){  
  286.     Node <T>*root = NULL;   
  287.     for(int i = 0; i < len; i++){   
  288.         root = BuildTree(root, arr[i]);  
  289.     }  
  290.       
  291.     InTree(root, arr);   
  292. }  
  293.    
  294. //建立二叉排序树   
  295. template <class T>  
  296. Node<T>* Sort::BuildTree(Node<T>*root, T data){  
  297.     Node<T> *tempNode = root;  
  298.     Node<T> *parentNode = NULL;  
  299.       
  300.     Node<T> *newNode = new Node<T>;   
  301.     newNode->data = data;  
  302.     newNode->left = NULL;  
  303.     newNode->right = NULL;   
  304.        
  305.     if(root == NULL){//空树的时候   
  306.         return newNode;   
  307.     }else{  
  308.         while(tempNode != NULL){  
  309.             parentNode = tempNode;  
  310.             if(tempNode->data >= data){  
  311.                 tempNode = tempNode->left;   
  312.             }else{  
  313.                 tempNode = tempNode->right;   
  314.             }   
  315.         }  
  316.           
  317.         if(parentNode->data >= data){  
  318.             parentNode->left = newNode;   
  319.         }else{  
  320.             parentNode->right = newNode;   
  321.         }   
  322.     }  
  323.       
  324.     return root;   
  325. }   
  326.   
  327. //中序遍历二叉排序树,将二叉树的节点存储在数组中   
  328. template <class T>  
  329. void Sort::InTree(Node<T> *root, T arr[]){  
  330.     static int index = 0;   
  331.     if(root != NULL){  
  332.         InTree(root->left, arr);  
  333.         arr[index++] = root->data;    
  334.         InTree(root->right, arr);  
  335.     }   
  336. }   
  337. ////////////////////////////////////////////////////////////////////  

原文地址:点击打开链接