C 语言常用的排序算法

来源:互联网 发布:网络通信是什么 编辑:程序博客网 时间:2024/04/25 19:57
[c-sharp] view plain copy
  1. /******************************************************************************************** 
  2.                                 平方阶(O(n2))排序 
  3.                 一般称为简单排序,例如直接插入、直接选择和冒泡排序 
  4.      
  5.  ********************************************************************************************/  
  6.   
  7. /*插入排序*/  
  8. extern int InsertSort(int source[], int array_size)  
  9. {  
  10.     int index = 1; //插入排序  
  11.     int i, j;  
  12.     for (i = 1; i < array_size; i++)  
  13.     {  
  14.         index = source[i];  
  15.         j = i;  
  16.         while ((j > 0) && (source[j - 1] > index))  
  17.         {  
  18.             source[j] = source[j - 1];  
  19.             j--;  
  20.         }  
  21.         source[j] = index;  
  22.     }  
  23.     return 1;  
  24. }  
  25.   
  26. /*冒泡排序*/  
  27. extern int BubbleSort(int source[], int array_size)  
  28. {  
  29.     int i, j;  
  30.     int temp;  
  31.     for (i = 0; i < array_size; i++)  
  32.     {  
  33.         for (j = 0; j < array_size - i - 1; j++)  
  34.             if (source[j] > source[j + 1])  
  35.             {  
  36.                 temp = source[j];  
  37.                 source[j] = source[j + 1];  
  38.                 source[j + 1] = temp;  
  39.             }  
  40.     }  
  41.     return 1;  
  42. }  
  43.   
  44. /*选择排序*/  
  45. extern int SelectSort(int source[], int array_size)  
  46. {  
  47.     int temp, min;  
  48.     int i, j;  
  49.     for (i = 0; i < array_size; i++)  
  50.     {  
  51.         min = i;//先假设最小下标为i  
  52.         for (j = i + 1; j < array_size; j++)  
  53.             if (source[j] < source[min])  
  54.                 min = j;//把i之后的最小值附给min  
  55.             if (min != i)  
  56.             {  
  57.                 temp = source[i];  
  58.                 source[i] = source[min];  
  59.                 source[min] = temp;  
  60.             }//判断min与i是否相等,若相等则说明原假设正确,反之:交换数值  
  61.     }  
  62.     return 1;  
  63. }  

 

[cpp] view plain copy
  1. /*********************************************************************************************   
  2.                                 线性对数阶(O(nlgn))排序 
  3.                             如快速、堆和归并排序 
  4.      
  5.  ********************************************************************************************/  
  6.   
  7. /*快速排序接口*/  
  8. static int Partition(int source[], int left, int right)  
  9. {  
  10.     int x = source[left];  
  11.     while (left < right)  
  12.     {  
  13.         while (left < right && x <= source[right])  
  14.             right--;  
  15.         source[left] = source[right];  
  16.         while (left < right && x >= source[left])  
  17.             left++;  
  18.         source[right] = source[left];  
  19.     }  
  20.     source[left] = x;  
  21.     return left;  
  22. }  
  23.   
  24. extern int QuickSort(int source[], int left, int right)  
  25. {  
  26.     int iPos;  
  27.     if (left >= right)  
  28.         return 1;  
  29.     iPos = Partition(source, left, right);  
  30.     QuickSort(source, left, iPos - 1); //   左边划分  
  31.     QuickSort(source, iPos + 1, right); //   右边划分  
  32.     return 1;  
  33. }  
  34.   
  35.   
  36. /*堆排序*/  
  37. static void HeapAdjust(int source[], int root, int node)/*root根节点, node节点总数*/  
  38. {  
  39.     //已知source[root..node]中除source[root]之外均满足堆的定义,本函数调整source[root]  
  40.     //使source[root..node]成为一个大顶堆  
  41.     int j, rc;  
  42.     rc = source[root];  
  43.     for (j = 2 * root; j <= node; j *= 2) //沿关键字叫大的结点向下筛选  
  44.     {  
  45.         if (j < node && source[j] < source[j + 1])  
  46.             ++j; //j为关键字较大的记录的下标  
  47.         if (rc >= source[j])  
  48.             break//rc应插入在位置root上  
  49.         source[root] = source[j];  
  50.         root = j;  
  51.     }  
  52.     source[root] = rc; //插入  
  53. }  
  54. extern int HeapSort(int source[], int array_size)  
  55. {  
  56.     int i, t;  
  57.     for (i = array_size / 2; i > 0; --i)  
  58.         //把a[1..L.length]建成大顶堆  
  59.         HeapAdjust(source, i, array_size);  
  60.     for (i = array_size; i > 1; --i)  
  61.     {  
  62.         t = source[1]; //将堆顶记录和当前未经排序子序列a[1..i]  
  63.         source[1] = source[i]; //中的最后一个记录相互交换  
  64.         source[i] = t;  
  65.         HeapAdjust(source, 1, i - 1); //将r[1..i-1]重新调整为大顶堆  
  66.     }  
  67.     return 1;  
  68. }  

 

 

[cpp] view plain copy
  1. /********************************************************************************************** 
  2.                                     O(n1+£)阶排序 
  3.                         £是介于0和1之间的常数,即0<£<1,如希尔排序 
  4.  
  5.  ********************************************************************************************/  
  6.   
  7. /*希儿排序*/  
  8. extern int ShellSort(int source[], int array_size)  
  9. {  
  10.     int increament;  
  11.     int e, i, j;  
  12.       
  13.     /*初始步长设为n/2*/  
  14.     for (increament = array_size / 2; increament > 0; increament = increament / 2)  
  15.         for (j = increament; j < array_size; j++)  
  16.         {  
  17.             if (source[j] < source[j - increament])  
  18.             {  
  19.                 e = source[j];  
  20.                 for (i = j - increament; i >= 0 && source[i] > e; i = i - increament)  
  21.                     source[i + increament] = source[i];  
  22.                 source[i + increament] = e;  
  23.                   
  24.             }  
  25.         }  
  26.         return 1;  
  27. }  

  九大数据结构排序算法的实现

targetver.h

[cpp] view plain copy
  1. #pragma once  
  2.   
  3. // 包括 SDKDDKVer.h 将定义可用的最高版本的 Windows 平台。  
  4.   
  5. // 如果要为以前的 Windows 平台生成应用程序,请包括 WinSDKVer.h,并将  
  6. // WIN32_WINNT 宏设置为要支持的平台,然后再包括 SDKDDKVer.h。  
  7.   
  8. #include <SDKDDKVer.h>  


stdafx.h

[cpp] view plain copy
  1. // stdafx.h : 标准系统包含文件的包含文件,  
  2. // 或是经常使用但不常更改的  
  3. // 特定于项目的包含文件  
  4. //  
  5. #define _AFXDLL  
  6. #pragma once  
  7.   
  8. #include "targetver.h"  
  9.   
  10. #include <stdio.h>  
  11. #include <tchar.h>  
  12.   
  13.   
  14.   
  15. // TODO:  在此处引用程序需要的其他头文件  
  16. #include<iostream>  
  17. #include<fstream>  
  18. #include<string>  
  19. #include<afxwin.h>  
  20.   
  21. using namespace std;  
  22.   
  23. static const int CMAX_N = 100;  
  24.   
  25. //剩余排序  
  26. class CData  
  27. {  
  28. public:  
  29.     int CKey_1;//使用该关键字  
  30.     int CKey_2;//未使用  
  31.     int CKey_3;//未使用  
  32. };  
  33.   
  34. //基数排序数据节点  
  35. class _CList  
  36. {  
  37. public:  
  38.     char CData[2];//各个位置  
  39.     int CDataNum;//数据位数  
  40.     class _CList *next;  
  41. };  
  42.   
  43. //排序  
  44. class CSortFunction  
  45. {  
  46. public:  
  47.     //构造  
  48.     CSortFunction(string CReadName, string CWriteName);  
  49.     //析构  
  50.     ~CSortFunction();  
  51.     //警告信息  
  52.     inline void CWarnings();  
  53.     //获取文件数据  
  54.     void CGetArray();  
  55.     //获取文件数据  
  56.     void CGetFileData(string CRadixFileName,string CWriteRadixFileName);  
  57.     //基数排序写入  
  58.     bool CWriteRadix();   
  59.     //写入排序后数据  
  60.     bool CWriteArray();  
  61.     //直接插入排序  
  62.     void CInsertSort();  
  63.     //折半插入排序  
  64.     void CHalfInsertSort();  
  65.     //希尔排序  
  66.     void CShellSort();  
  67.     //冒泡排序  
  68.     void CBubbleSort();  
  69.     //快速排序  
  70.     void CQuickSort();  
  71.     //选择排序  
  72.     void CSelectSort();  
  73.     //堆排序  
  74.     void CHeapSort();  
  75.     //归并排序  
  76.     void CMergeSort();  
  77.     //基数排序  
  78.     void CRadixSort();  
  79. private:  
  80.     CData *CArray;  
  81.     int CLength;  
  82.     string CReadName;  
  83.     string CWriteName;  
  84.     ifstream CReadFile;  
  85.     ofstream CWriteFile;  
  86.     _CList *list;  
  87.     _CList *CTmp = nullptr;  
  88.   
  89.     //快速排序  
  90.     void PriCQuickSort(CData* CArray, int CStart, int CEnd);  
  91.   
  92.     //堆重构  
  93.     void CSift(CData* CArray, int low, int high);  
  94.   
  95.     //归并子表  
  96.     void CMerge(CData* CArray, int low, int mid, int high);  
  97.   
  98.     //归并整张表  
  99.     void CMergePass(CData* CArray, int CChildLength, int CLength);  
  100. };  


stdafx.cpp

[cpp] view plain copy
  1. // stdafx.cpp : 只包括标准包含文件的源文件  
  2. // AllSortFunction.pch 将作为预编译头  
  3. // stdafx.obj 将包含预编译类型信息  
  4.   
  5. #include "stdafx.h"  
  6.   
  7. // TODO:  在 STDAFX.H 中  
  8. // 引用任何所需的附加头文件,而不是在此文件中引用  
  9.   
  10. //构造  
  11. CSortFunction::CSortFunction(string CReadName, string CWriteName)  
  12. {  
  13.     this->CReadName = CReadName;//读取文件名  
  14.     this->CWriteName = CWriteName;//写入文件名  
  15.     CArray = (CData *)malloc(sizeof(CData)*CMAX_N);//申请空间  
  16.     CLength = 0;//数组长度  
  17. }  
  18.   
  19. //析构  
  20. CSortFunction::~CSortFunction()  
  21. {  
  22.     //释放基数排序所用空间  
  23.     _CList *CTmpList_r = list, *CTmpList_p = CTmpList_r->next;  
  24.     while (CTmpList_p != NULL)  
  25.     {  
  26.         free(CTmpList_r);  
  27.         CTmpList_r = CTmpList_p;  
  28.         CTmpList_p = CTmpList_p->next;  
  29.     }  
  30.     free(CTmpList_p);  
  31.     //释放其他排序所用空间  
  32.     free(CArray);  
  33. }  
  34.   
  35.   
  36. //警告  
  37. inline void CSortFunction::CWarnings()  
  38. {  
  39.     MessageBox(NULL, _T("No Data Found!"), _T("Warning"), 1);//无原始数据文件  
  40.     exit(0);  
  41. }  
  42.   
  43.   
  44. //获取数据  
  45. void CSortFunction::CGetArray()  
  46. {  
  47.     CReadFile.open(CReadName, ios::in);  
  48.     if (CReadFile.is_open())  
  49.     {  
  50.         int CfData, i = 0;  
  51.         while (!CReadFile.eof() && (CReadFile >> CfData).good())  
  52.         {  
  53.             CArray[i].CKey_1 = CfData;//读入数据  
  54.             i++;  
  55.         }  
  56.         CLength = i;//数组长度  
  57.     }  
  58.     else  
  59.     {  
  60.         CSortFunction::CWarnings();//警告,无原始数据文件  
  61.     }  
  62.     CReadFile.close();  
  63. }  
  64.   
  65. //写入排序数据  
  66.   
  67. bool CSortFunction::CWriteArray()  
  68. {  
  69.     CWriteFile.open(CWriteName, ios::out|ios::app);  
  70.     CWriteFile << "----------" << endl;  
  71.     for (int i = 0; i < CLength; i++)  
  72.     {  
  73.         CWriteFile << CArray[i].CKey_1 << endl;  
  74.     }  
  75.     CWriteFile.close();  
  76.     return true;  
  77. }  
  78.   
  79.   
  80. //直接插入排序  
  81. void CSortFunction::CInsertSort()  
  82. {  
  83.     for (int i = 1; i < CLength; i++)  
  84.     {  
  85.         CData tmp = CArray[i];  
  86.         int j = i - 1;  
  87.         //从有序序列末尾开始,若小于末尾,则后移覆盖  
  88.         while (j >= 0 && tmp.CKey_1 < CArray[j].CKey_1)  
  89.         {  
  90.             CArray[j + 1] = CArray[j];  
  91.             j--;  
  92.         }  
  93.         //正确位置  
  94.         CArray[j + 1] = tmp;  
  95.     }  
  96. }  
  97.   
  98. //折半插入排序  
  99. void CSortFunction::CHalfInsertSort()  
  100. {  
  101.     int low, high, mid;  
  102.     for (int i = 1; i < CLength; i++)  
  103.     {  
  104.         CData tmp = CArray[i];  
  105.         low = 0; high = i - 1;  
  106.         //折半查找  
  107.         while (low <= high)  
  108.         {  
  109.             mid = (low + high) / 2;  
  110.             if (tmp.CKey_1 < CArray[mid].CKey_1)  
  111.             {  
  112.                 high = mid - 1;  
  113.             }  
  114.             else  
  115.             {  
  116.                 low = mid + 1;  
  117.             }  
  118.         }  
  119.         //后移插入正确位置  
  120.         for (int j = i - 1; j>high; j--)  
  121.         {  
  122.             CArray[j + 1] = CArray[j];  
  123.         }  
  124.         CArray[high + 1] = tmp;  
  125.     }  
  126. }  
  127.   
  128. //希尔排序  
  129. void CSortFunction::CShellSort()  
  130. {  
  131.     //直接插入排序间隔为1,希尔排序间隔增大  
  132.     int gap = CLength / 2;//分割标志  
  133.     while (gap > 0)  
  134.     {  
  135.         for (int i = gap; i < CLength; i++)  
  136.         {  
  137.             CData tmp = CArray[i];  
  138.             int j = i - gap;  
  139.             while (j >= 0 && tmp.CKey_1 < CArray[j].CKey_1)  
  140.             {  
  141.                 CArray[j + gap] = CArray[j];  
  142.                 j -= gap;  
  143.             }  
  144.             CArray[j + gap] = tmp;  
  145.         }  
  146.         gap /= 2;  
  147.     }  
  148. }  
  149.   
  150. //冒泡排序  
  151. void CSortFunction::CBubbleSort()  
  152. {  
  153.     bool CExchange;//优化判断是否有数据交换  
  154.     for (int i = 0; i < CLength - 1; i++)  
  155.     {  
  156.         CExchange = false;  
  157.         for (int j = CLength - 1; j>i; j--)  
  158.         {  
  159.             //后一位小于前一位,交换位置  
  160.             if (CArray[j].CKey_1 < CArray[j - 1].CKey_1)  
  161.             {  
  162.                 CData tmp = CArray[j];  
  163.                 CArray[j] = CArray[j - 1];  
  164.                 CArray[j - 1] = tmp;  
  165.                 CExchange = true;  
  166.             }  
  167.         }  
  168.         //若某一趟没有发生交换,排序结束  
  169.         if (!CExchange)  
  170.         {  
  171.             return;  
  172.         }  
  173.     }  
  174. }  
  175.   
  176. //快速排序  
  177. void CSortFunction::PriCQuickSort(CData* CArray,int CStart,int CEnd)  
  178. {  
  179.     int i = CStart, j = CEnd;  
  180.     CData tmp;  
  181.     if (CStart < CEnd)  
  182.     {  
  183.         tmp = CArray[CStart];  
  184.         while (i != j)  
  185.         {  
  186.             //从右到左  
  187.             while (j>i&&CArray[j].CKey_1 >= tmp.CKey_1)  
  188.             {  
  189.                 j--;  
  190.             }  
  191.             CArray[i] = CArray[j];  
  192.             //从左到右  
  193.             while (i<j&&CArray[i].CKey_1 <= tmp.CKey_1)  
  194.             {  
  195.                 i++;  
  196.             }  
  197.             CArray[j] = CArray[i];  
  198.         }  
  199.         CArray[i] = tmp;  
  200.         //递归左右两侧  
  201.         PriCQuickSort(CArray, CStart, i - 1);  
  202.         PriCQuickSort(CArray, i + 1, CEnd);  
  203.     }  
  204. }  
  205.   
  206. //快排  
  207. void CSortFunction::CQuickSort()  
  208. {  
  209.     PriCQuickSort(CArray, 0, CLength-1);  
  210. }  
  211.   
  212. //选择排序  
  213. void CSortFunction::CSelectSort()  
  214. {  
  215.     for (int i = 0; i < CLength - 1; i++)  
  216.     {  
  217.         int k = i;  
  218.         //后i后存在更小值,则改变k值为该值下标  
  219.         for (int j = i + 1; j<CLength; j++)  
  220.         {  
  221.             if (CArray[j].CKey_1 < CArray[k].CKey_1)  
  222.             {  
  223.                 k = j;  
  224.             }  
  225.         }  
  226.         //若发生改变,则交换  
  227.         if (k != i)  
  228.         {  
  229.             CData tmp = CArray[i];  
  230.             CArray[i] = CArray[k];  
  231.             CArray[k] = tmp;  
  232.         }  
  233.     }  
  234. }  
  235.   
  236. //堆重构  
  237. void CSortFunction::CSift(CData* CArray, int low, int high)  
  238. {  
  239.     //位置i所在为双亲节点,j为左节点,j+1为右节点  
  240.     int i = low, j = 2 * i;  
  241.     CData tmp = CArray[i];  
  242.     while (j <= high)  
  243.     {  
  244.         //若右节点大于左节点,j指向右节点  
  245.         if (j < high&&CArray[j].CKey_1 < CArray[j + 1].CKey_1)  
  246.         {  
  247.             j++;  
  248.         }  
  249.         //若左右节点中较大值大于双钱节点,交换值和下标  
  250.         if (tmp.CKey_1 < CArray[j].CKey_1)  
  251.         {  
  252.             CArray[i] = CArray[j];  
  253.             i = j;  
  254.             j = 2 * i;  
  255.         }  
  256.         else  
  257.         {  
  258.             break;  
  259.         }  
  260.     }  
  261.     CArray[i] = tmp;  
  262. }  
  263.   
  264. //堆排序  
  265. void CSortFunction::CHeapSort()  
  266. {  
  267.     //初始化堆  
  268.     for (int i = (CLength) / 2; i >= 0; i--)  
  269.     {  
  270.         CSift(CArray, i, CLength-1);  
  271.     }  
  272.     //排序  
  273.     for (int i = CLength-1; i >= 1; i--)  
  274.     {  
  275.         CData tmp = CArray[0];  
  276.         CArray[0] = CArray[i];  
  277.         CArray[i] = tmp;  
  278.         CSift(CArray, 0, i-1);//重构堆  
  279.     }  
  280. }  
  281.   
  282. //归并子表  
  283. void CSortFunction::CMerge(CData* CArray, int low, int mid, int high)  
  284. {  
  285.     CData *CTmpArray;  
  286.     int i = low, j = mid + 1, k = 0;  
  287.     CTmpArray = (CData *)malloc(sizeof(CData)*(high - low + 1));  
  288.     while (i <= mid&&j <= high)  
  289.     {  
  290.         //向CTmpArray中存入第一段  
  291.         if (CArray[i].CKey_1 <= CArray[j].CKey_1)  
  292.         {  
  293.             CTmpArray[k] = CArray[i];  
  294.             i++; k++;  
  295.         }  
  296.         else//存第二段  
  297.         {  
  298.             CTmpArray[k] = CArray[j];  
  299.             j++; k++;  
  300.         }  
  301.     }  
  302.     //复制第一段余下部分  
  303.     while (i <= mid)  
  304.     {  
  305.         CTmpArray[k] = CArray[i];  
  306.         i++; k++;  
  307.     }  
  308.     //复制第二段余下部分  
  309.     while (j <= high)  
  310.     {  
  311.         CTmpArray[k] = CArray[j];  
  312.         j++; k++;  
  313.     }  
  314.     //拷贝回原数组  
  315.     for (k = 0, i = low; i <= high; k++, i++)  
  316.     {  
  317.         CArray[i] = CTmpArray[k];  
  318.     }  
  319.     free(CTmpArray);  
  320. }  
  321.   
  322. //归并整张表  
  323. void CSortFunction::CMergePass(CData* CArray, int CChildLength, int CLength)  
  324. {  
  325.     int i;  
  326.     //归并CChildLength长的相邻子表  
  327.     for (i = 0; i + 2 * CChildLength - 1 < CLength; i = i + 2 * CChildLength)  
  328.     {  
  329.         CMerge(CArray, i, i + CChildLength - 1, i + 2 * CChildLength - 1);  
  330.     }  
  331.     //归并子表  
  332.     if (i + CChildLength - 1 < CLength)  
  333.     {  
  334.         CMerge(CArray, i, i + CChildLength - 1, CLength - 1);  
  335.     }  
  336. }  
  337.   
  338. //归并排序  
  339. void CSortFunction::CMergeSort()  
  340. {  
  341.     for (int length = 1; length < CLength; length = length * 2)  
  342.     {  
  343.         CMergePass(CArray, length, CLength);  
  344.     }  
  345. }  
  346.   
  347. //获取文件数据  
  348. void CSortFunction::CGetFileData(string CRadixFileName, string CWriteRadixFileName)  
  349. {  
  350.     CReadName = CRadixFileName;//读入数据文件名  
  351.     CWriteName = CWriteRadixFileName;//写入数据文件名  
  352.   
  353.     //尾插法构造链表  
  354.     _CList *CTmpList_r, *CTmpList_p;  
  355.     list = (_CList *)malloc(sizeof(_CList));  
  356.     CTmpList_r = list;  
  357.   
  358.     string CfData;  
  359.     CReadFile.open(CReadName, ios::in);  
  360.     if (CReadFile.is_open())  
  361.     {  
  362.         while (!CReadFile.eof() && (CReadFile >> CfData).good())  
  363.         {  
  364.             int j = 0;  
  365.             CTmpList_p = (_CList *)malloc(sizeof(_CList));  
  366.             CTmpList_p->CDataNum = CfData.length();//获取数据位数  
  367.             while (CfData.length())  
  368.             {  
  369.                 CTmpList_p->CData[j] = CfData[CfData.length()-1];//倒叙写入数据  
  370.                 CfData.assign(CfData.substr(0, CfData.length() - 1));//重构数据串  
  371.                 j++;  
  372.             }  
  373.             CTmpList_r->next = CTmpList_p;  
  374.             CTmpList_r = CTmpList_p;  
  375.         }  
  376.         CTmpList_r->next = nullptr;  
  377.         CReadFile.close();  
  378.     }  
  379.     else  
  380.     {  
  381.         CWarnings();  
  382.     }  
  383. }  
  384.   
  385.   
  386. //基数排序  
  387. void CSortFunction::CRadixSort()  
  388. {  
  389.     _CList *CHead[10], *CTail[10], *CTmp_r = nullptr;  
  390.     CTmp = list->next;  
  391.     //分散遍历排序  
  392.     for (int i = 0; i < CTmp->CDataNum; i++)  
  393.     {  
  394.         //初始化为空  
  395.         for (int j = 0; j < 10; j++)  
  396.         {  
  397.             CHead[j] = CTail[j] = nullptr;  
  398.         }  
  399.         //按个位/十位/百位...依次排序,循环次数定位数据位数次  
  400.         while (CTmp!=NULL)  
  401.         {  
  402.             int k = CTmp->CData[i] - '0';  
  403.             if (CHead[k] == NULL)  
  404.             {  
  405.                 CHead[k] = CTail[k] = CTmp;  
  406.             }  
  407.             else  
  408.             {  
  409.                 CTail[k]->next = CTmp;  
  410.                 CTail[k] = CTmp;  
  411.             }  
  412.             CTmp = CTmp->next;  
  413.         }  
  414.         //收集排序后序列  
  415.         CTmp = NULL;  
  416.         for (int j = 0; j < 10; j++)  
  417.         {  
  418.             if (CHead[j] != NULL)  
  419.             {  
  420.                 if (CTmp == NULL)  
  421.                 {  
  422.                     CTmp = CHead[j];  
  423.                     CTmp_r = CTail[j];  
  424.                       
  425.                 }  
  426.                 else  
  427.                 {  
  428.                     CTmp_r->next = CHead[j];  
  429.                     CTmp_r = CTail[j];  
  430.                       
  431.                 }  
  432.             }  
  433.         }  
  434.         CTmp_r->next = NULL;  
  435.     }  
  436. }  
  437.   
  438. //基数排序写入  
  439. bool CSortFunction::CWriteRadix()  
  440. {  
  441.     CWriteFile.open(CWriteName, ios::out);  
  442.     _CList *CTmpList_r = CTmp;  
  443.     while (CTmpList_r != NULL)  
  444.     {  
  445.         //倒叙写入,获取正序数  
  446.         CWriteFile << CTmpList_r->CData[1] << CTmpList_r->CData[0] << endl;  
  447.         CTmpList_r = CTmpList_r->next;  
  448.     }  
  449.     CWriteFile.close();  
  450.     return true;  
  451. }  

AllSortFunction.cpp(main文件)

[cpp] view plain copy
  1. // AllSortFunction.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5.   
  6.   
  7. int _tmain(int argc, _TCHAR* argv[])  
  8. {  
  9.     CSortFunction sort("read.txt""sort.txt");  
  10.     sort.CGetArray();//读数据  
  11.     sort.CInsertSort();//直接插入排序  
  12.     if (sort.CWriteArray())//写数据  
  13.     {  
  14.         cout << "直接插入排序 OK." << endl;  
  15.     }  
  16.     sort.CHalfInsertSort();//折半插入排序  
  17.     if (sort.CWriteArray())//写数据  
  18.     {  
  19.         cout << "折半插入排序 OK." << endl;  
  20.     }  
  21.     sort.CShellSort();//希尔排序  
  22.     if (sort.CWriteArray())//写数据  
  23.     {  
  24.         cout << "希尔排序 OK." << endl;  
  25.     }  
  26.     sort.CBubbleSort();//冒泡排序  
  27.     if (sort.CWriteArray())//写数据  
  28.     {  
  29.         cout << "冒泡排序 OK." << endl;  
  30.     }  
  31.     sort.CQuickSort();//快速排序  
  32.     if (sort.CWriteArray())//写数据  
  33.     {  
  34.         cout << "快速排序 OK." << endl;  
  35.     }  
  36.     sort.CSelectSort();//选择排序  
  37.     if (sort.CWriteArray())//写数据  
  38.     {  
  39.         cout << "选择排序 OK." << endl;  
  40.     }  
  41.     sort.CHeapSort();//堆排序  
  42.     if (sort.CWriteArray())//写数据  
  43.     {  
  44.         cout << "堆排序 OK." << endl;  
  45.     }  
  46.     sort.CMergeSort();//归并排序  
  47.     if (sort.CWriteArray())//写数据  
  48.     {  
  49.         cout << "归并排序 OK." << endl;  
  50.     }  
  51.     sort.CGetFileData("radix.txt""solveRadix.txt");//获取基数排序数据  
  52.     sort.CRadixSort();//基数排序  
  53.     //写入文件  
  54.     if (sort.CWriteRadix())  
  55.     {  
  56.         cout << "基数排序 OK." << endl;  
  57.     }  
  58.     return 0;  
  59. }  

1 0