1、直接插入排序2、希尔排序3、2-路归并排序4、折半插入排序5、冒泡排序6、快速排序7、堆排序/*----------------------------------------* 07_排序.cpp -- 排序的相关操作* 对排序的每个基本操作都用单独的函数来实现* 水上飘2011年写----------------------------------------*/// ds07.cpp : 定义控制台应用程序的入口点。
//#include "stdafx.h"#include "stdio.h"#include <iostream>#include <iomanip>using namespace std;#define MAXSIZE 20typedefintKeyType;typedefstruct{KeyType key; //关键字项KeyType data; //数据项}RedType; //记录类型typedefstruct{RedTypearr[MAXSIZE+1]; //arr[0]闲置或用作哨兵单元int length; //顺序表长度}SqList; //顺序表类型typedefSqListHeapType;//对顺序表L做一趟希尔插入排序//前后记录位置的增量是dk//r[0]只是暂存单元//当j<=0时,插入位置已找到voidshellInsert(SqList&L, intdk){int i, j;for (i = dk + 1; i <= L.length; i++){if (L.arr[i].key <L.arr[i - dk].key){L.arr[0] = L.arr[i]; //暂存for (j = i - dk; j > 0 &&L.arr[0].key <L.arr[j].key; j -= dk){L.arr[j + dk] = L.arr[j]; //记录后移,查找插入位置}//end for jL.arr[j + dk] = L.arr[0]; //插入}//end if}//end for i}//shellInsert//按增量序列dlta[0...t-1]对顺序表做希尔排序voidshellSort(SqList&L, intdlta[], int t){for (int k = 0;k < t; k++){shellInsert(L, dlta[k]); //一趟增量为dlta[k]的插入序列}}//折半插入排序voidbInsertSort(SqList&L){for (int i = 2; i <= L.length; i++){L.arr[0] = L.arr[i];//将其暂存到arr[0]int low = 1;int high = i - 1;while(low <= high){//在arr[low...high]中折半查找有序插入的位置int m = (low + high) / 2;//折半if(L.arr[0].key <L.arr[m].key)high = m - 1;//插入点在低半区else low = m + 1;//插入点在高半区}//whilefor(int j = i - 1; j >= high + 1; j--)L.arr[j + 1] = L.arr[j];//记录后移L.arr[high + 1] = L.arr[0];//插入}//for}//BInsertSort//直接插入排序voidinsertSort(SqList&L){for(int i = 2; i <= L.length; i++){if(L.arr[i].key <L.arr[i-1].key){//if"<",需将L.arr[i]插入有序子表L.arr[0] = L.arr[i];//复制为哨兵L.arr[i] = L.arr[i-1];int j;for(j = i - 2; L.arr[0].key <L.arr[j].key; j--)L.arr[j+1] = L.arr[j];//记录后移L.arr[j+1] = L.arr[0];//插入到正确位置}//if}//for}//InsertSort//冒泡排序voidbubbleSort(SqList&L){RedType* temp = NULL;for(int i = 1; i <L.length; i++){//第i趟排序for(int j = 1; j <= L.length-i; j++){//第j次排序if(L.arr[j].key >L.arr[j+1].key){//交换前后的位置L.arr[0] = L.arr[j];L.arr[j] = L.arr[j+1];L.arr[j+1] = L.arr[0];}//if}//for j}//for i}//bubbleSort//交换顺序表中子表L.arr[low...high]的记录,//枢轴记录到位,并返回其所在位置int partition(SqList&L, int low, int high){L.arr[0] = L.arr[low];//子表的第一个记录做枢轴记录KeyTypepivotKey = L.arr[low].key;//枢轴记录关键字while(low < high){//从表的两端交替向中间扫描while(low < high &&L.arr[high].key >= pivotKey){high--;}//whileL.arr[low] = L.arr[high];//将比枢轴小的记录移到低端while(low < high &&L.arr[low].key <= pivotKey){low++;}//whileL.arr[high] = L.arr[low];//将比枢轴大的记录移到高端}//whileL.arr[low] = L.arr[0];//枢轴记录到位return low;//返回枢轴位置}//paitition//对顺序表L中的子序列L.arr[low...high]做快速排序voidqSort(SqList&L, int low, int high){if(low < high){//长度大于1KeyTypepivotLoc = partition(L, low, high);//将L.arr[low...high]一分为二qSort(L, low, pivotLoc-1);//对低子表递归排序,pivotLoc是枢轴位置qSort(L, pivotLoc+1, high);//对高子表递归排序}//if}//qSort//对顺序表L做快速排序voidquickSort(SqList&L){qSort(L, 1, L.length);}//quickSort//调整H.arr[s]的关键字,使H.arr[s...m]成为一个大顶堆//(对其中记录的关键字而言)voidheapAdjust(HeapType&H, int s, int m){RedTyperc = H.arr[s];for(int j = 2 * s; j <= m; j = j * 2){//沿key较大的孩子结点向下筛选if(j < m &&H.arr[j].key <H.arr[j+1].key){j++;//j为key叫大记录的下标}//ifif(!(rc.key<H.arr[j].key)){break;//rc应插入在位置s上}//ifH.arr[s] = H.arr[j];s = j;}//forH.arr[s] = rc;//插入}//heapAdjust//对顺序表H进行堆排序voidheapSort(HeapType&H){for(int i = H.length/2; i > 0; i--){//把H.arr[1...H.length]建成大顶堆heapAdjust(H, i, H.length);}//forfor(int i = H.length; i > 1; i--){//将堆记录当前未经排序的子序列H.arr[1...i]中的最后一个记录交换H.arr[0] = H.arr[1];H.arr[1] = H.arr[i];H.arr[i] = H.arr[0];heapAdjust(H, 1, i-1);//将H.arr[1...i-1]重新调整为大顶堆}//for}//heapSort//将有序的SR[i..m]和SR[m+1..n]归并为有序的TR[i..n]void Merge(RedType SR[], RedType *TR, int i, int m, int n){int j, k;for (j = m + 1, k = i; i <= m && j <= n; k++)//将SR中记录由小到大地并入TR {if (SR[i].key <= SR[j].key){TR[k] = SR[i++];}else{TR[k] = SR[j++];}}//end for j,kif (i <= m) //将剩余的SR[i..m]复制到TR {for (; k <= n && i <= m; k++, i++){TR[k] = SR[i];}}//end if iif (j <= n) //将剩余的SR[j..n]复制到TR {for (; k <= n && j <= n; k++, j++){TR[k] = SR[j];}}//end if j}//Merge//将SR[s...t]归并排序为TR1[s...t]voidMSort(RedType SR[], RedType TR1[], int s, int t){int m;RedTypeTR2[MAXSIZE];if (s == t){TR1[s] = SR[s];}else{m = (s + t) /2; //将SR[s..t]平分为SR[s..m]和SR[m+1..t]MSort(SR, TR2, s, m); //递归地将SR[s..m]归并为有序的TR2[s..m]MSort(SR, TR2, m+1, t); //递归地将SR[m+1..t]归并为有序的TR2[m+1..t]Merge(TR2, TR1, s, m, t); //将TR2[s..m]和TR2[m+1..t]归并到TR1[s..t] }}//MSort//对顺序表L做归并排序voidmergeSort(SqList&L){MSort(L.arr, L.arr, 1, L.length);}//mergeSort//初始化顺序表,给其赋值voidsetValue(SqList&L){KeyTypedata[8] = {49,38,65,97,76,13,27,49};int n = 8;L.arr[0].data = 0;L.length = n;for(int i = 1; i <= L.length; i++){L.arr[i].data = data[i-1];L.arr[i].key = L.arr[i].data;}//end for i}//setValue//输出顺序表void show(SqList&L){for(int i = 1; i <= L.length; i++){if(i % 5 == 0)cout<<endl;cout<<setw(5) <<L.arr[i].data;}cout<<endl;}//show//调用排序函数进行排序void performance(SqList&L){cout<< "原始序列:" <<endl;show(L);SqList LI = L;insertSort(LI);cout<< "直接插入排序结果:" <<endl;show(LI);SqList LS = L;intdlta[3] = {4,3,1};int t = 3;shellSort(LS, dlta, t);cout<< "希尔排序结果:" <<endl;show(LS);SqList LM = L;mergeSort(LM);cout<< "2-路归并排序结果:" <<endl;show(LM);SqList LB = L;bInsertSort(LB);cout<< "折半插入排序结果:" <<endl;show(LB);SqList LBB = L;bubbleSort(LBB);cout<< "冒泡排序结果:" <<endl;show(LBB);SqList LQ = L;cout<< "快速排序结果:" <<endl;quickSort(LQ);show(LQ);HeapType H = L;cout<< "堆排序结果:" <<endl;heapSort(H);show(H);}//performanceint _tmain(intargc, _TCHAR* argv[]){SqList L;setValue(L);performance(L);system("pause");return 0; }。