直接插入排序,折半插入,希尔排序,快速排序,堆排序,以及归并排序。
#include <iostream>
using namespace std;
#define MAXSIZE 20
typedef struct {
int key;
int *info;
}RedType;
typedef struct {
RedType r[MAXSIZE+1];
int length;
}SqList;
void print(SqList list){
cout<<"排序:";
for(int i = 1;i<=list.length;i++){
cout << list.r[i].key<<",";
}
cout <<endl;
}
void InsertSort(SqList &L){
cout <<"直接插入排序"<<endl;
for(int i=2;i<=L.length;i++){
if(L.r[i].key < L.r[i-1].key){
L.r[0] = L.r[i];
L.r[i] = L.r[i-1];
int j;
for(j = i-1;L.r[0].key < L.r[j].key;--j){
L.r[j+1] = L.r[j];
}
L.r[j+1] = L.r[0];
}
}
}
void BInsertSort(SqList &L){
cout <<"折半插入排序"<<endl;
int low,high,m;
for(int i = 2;i<=L.length;i++){
//cout << L.r[i].key<<endl;
//print(L);
L.r[0] = L.r[i];
low = 1;high = i-1;//在1到i-1之间的元素是有序的。i是待排元素
while(low <= high){
m=(low+high)/2;
if(L.r[0].key < L.r[m].key){
high = m-1;
}else{
low = m+1;
}
}
for(int j=i-1;j>=high+1;j--){//high是插入元素的位置
L.r[j+1] = L.r[j];
}
L.r[high+1] = L.r[0];
}
}
void ShellInsert(SqList &L,int dk){//希尔(缩小增量)与归并不同。它通过使元素大幅度的跳跃,减少移动的次数
cout <<"缩小增量:"<<dk<<endl;
for(int i=dk+1;i<=L.length;i++){//类似于跳跃式的归并,而且只归并一个
if(L.r[i].key < L.r[i-dk].key){
L.r[0] = L.r[i];
int j;
for(j = i-dk;j>0&&L.r[0].key < L.r[j].key;j=j-dk){
L.r[j+dk] = L.r[j];
}
L.r[j+dk] = L.r[0];//给r[j]赋0的值
}
}
print(L);
}
void ShellSort(SqList &L,int dlta[],int t){
for(int k = 0;k<t;++k){
ShellInsert(L,dlta[k]);
}
}
int Partition(SqList &L,int low,int high){
cout <<"一次快排"<<endl;
L.r[0] = L.r[low];
int privotkey = L.r[low].key;
while(low<high){//结束时high=low
while(low<high && L.r[high].key >= privotkey) --high;//右边的大什么都不做
L.r[low] = L.r[high];//右边的小,将high与枢轴交换
while(low<high && L.r[low].key <= privotkey) ++low;
L.r[high] = L.r[low];
}
L.r[low] = L.r[0];
return low;
}
void QSort(SqList &L,int low,int high){
cout <<"快速排序(递归)"<<endl;
if(low<high){
int pivotloc = Partition(L,low,high);
QSort(L,low,pivotloc-1);
QSort(L,pivotloc+1,high);
}
}
void QuickSort(SqList &L){
QSort(L,1,L.length);
}
void HeapAdjust(SqList &H,int s,int m){
RedType rc = H.r[s];
for(int j = s*2;j<=m;j=j*2){
if(j<m && H.r[j].key < H.r[j+1].key) j++;
if(!(rc.key < H.r[j].key)) break;
H.r[s] = H.r[j];
s = j;
}
H.r[s] = rc;
}
void HeapSort(SqList &H){
cout <<"堆排序"<<endl;
for(int i=H.length/2;i>0;--i){
HeapAdjust(H,i,H.length);
}
for(int i = H.length;i>1;i--){
RedType tt = H.r[i];
H.r[i] = H.r[1];
H.r[1] = tt;
HeapAdjust(H,1,i-1);
}
}
void Merge(RedType SR[],RedType TR[],int i,int m,int n){
cout<<"归并:";
int t = i;
for(int y = t;y<=n;y++){
cout<<SR[y].key<<",";
}
int k = i;
int j = m+1;
for(;i<=m&&j<=n;k++){
if(SR[i].key <= SR[j].key){
TR[k] = SR[i];
i++;
}else{
TR[k] = SR[j];
j++;
}
}
for(;i<=m;i++){
TR[k] = SR[i];
k++;
}
for(;j<=n;j++){
TR[k] = SR[j];
k++;
}
cout <<" ";
for(;t<=n;t++){
cout<<TR[t].key<<",";
}
cout <<endl;
}
void MSort(RedType SR[],RedType TR1[],int s,int t){
if(s==t) TR1[s] = SR[s];
else{
int m = (s+t)/2;
RedType TR2[11];//效率实在低
MSort(SR,TR2,s,m);
MSort(SR,TR2,m+1,t);
Merge(TR2,TR1,s,m,t);
}
}
void MergeSort(SqList &L){
MSort(L.r,L.r,1,L.length);
}
int main(){
SqList list ;
list.length=10;
int arr[] = {0,49,38,65,97,76,13,27,49,55,4};
for(int i = 1;i<=10;i++){
list.r[i].key = arr[i];
list.r[i].info = NULL;
}
list.r[0].key = NULL;
list.r[0].info = NULL;
print(list);
//InsertSort(list);
//BInsertSort(list);
//int tt[3] = {5,3,1};
//ShellSort(list,tt,3);
//QuickSort(list);
//HeapSort(list);
MergeSort(list);
print(list);
return 0;
}