#include<iostream>
#include<cstdlib>
using namespace std;
#define MAXSIZE 20
typedef int ElemType;
typedef struct{
ElemType Array[MAXSIZE+1];//Array[0]置哨
int length;
}SqList;
void InsertSort(SqList &L)//直接插入
{
int i, j;
for (i = 2; i <= L.length; i++)//Array[i]为待插入元素
{
if (L.Array[i] < L.Array[i-1])//从Array[i-1]起向前进行顺序查找
{
L.Array[0] = L.Array[i];//入哨
for (j = i-1; L.Array[0] < L.Array[j]; j--)//从后往前找
L.Array[j+1] = L.Array[j];//将Array[j]后移一个单位
L.Array[j+1] = L.Array[0];//将Array[i]放入待插入位置(j+1)
}//if不满足则不动
}
return;
}
void BInsertSort(SqList &L)//折半插入
{
int i, j; int low, high, mid;
for (i = 2; i <= L.length; ++i)
{
L.Array[0] = L.Array[i];//先入哨
low = 1; high = i-1;//high取有序序列中的最后一个元素的下标
while (low <= high){//循环结束条件
mid = (low + high) / 2;//取中间位置的元素的下标
if (L.Array[0] < L.Array[mid])
high = mid-1;//插入点在有序序列底半区
else
low = mid+1;//插入点在有序序列高半区
}
for (j = i-1; j>=high+1; --j)//high位置的元素比待插入元素的关键字小
L.Array[j+1] = L.Array[j];//比待插入元素的关键字大的记录后移
L.Array[high+1] = L.Array[0];//插入
}
return;
}
void ShellSort(SqList &L, int Gap)//希尔排序(隶属插入排序)又名缩小增量排序
{//和直接插入排序很相似
int i, j;
while (Gap > 0)
{
for (i = Gap+1; i <= L.length; i++)
if (L.Array[i] < L.Array[i-Gap])
{
L.Array[0] = L.Array[i];//入哨
for (j=i-Gap; j>0&&(L.Array[0]<L.Array[j]); j -=Gap)
L.Array[j+Gap] = L.Array[j];//记录后移
L.Array[j+Gap] = L.Array[0];//插入
}
Gap /=2;//增量缩小直至1
}
return;
}
void BubbleSort(SqList &L)//起泡排序(隶属交换排序)
{
int i, j;
ElemType tem;
int change=1;//测试是否进行交换
for (i = L.length; i>1&&change; i--)
{
change=0;
for (j = 1; j<i; j++)
if (L.Array[j]>L.Array[j+1])//比较与交换是1:3
{
tem = L.Array[j];
L.Array[j] = L.Array[j+1];
L.Array[j+1] = tem;
change=1;
}
}
return;
}
/************************快速排序******************************/
int Partition(SqList &L, int low, int high)//一趟快速排序
{
ElemType LimtKey;
LimtKey = L.Array[low];//元素为枢轴
L.Array[0] = L.Array[low];//入哨
while (low < high)
{
while (low < high&&L.Array[high] >= LimtKey)
--high;
L.Array[low] = L.Array[high];
while (low < high&&L.Array[low] <= LimtKey)
++low;
L.Array[high] = L.Array[low];
}//low==high,退出循环.
L.Array[low] = L.Array[0];//或者L.Array[high]=L.Array[0]
return low;//or return high
}
void QSort(SqList &L, int low, int high)//对分割的两部分进行递归的快速排序
{
int LimtLoc;
if (low < high){
LimtLoc = Partition(L, low, high);
QSort(L, low, LimtLoc-1);
QSort(L, LimtLoc+1, high);
}
return;
}
void QuickSort(SqList &L, int n)//快速排序
{
QSort(L, 1, n);
}
/***********************************************************/
void SelectSort(SqList &L)
{
int i, j; ElemType tem;
for (i = 1; i < L.length; i++)
{
for (j = i + 1; j <= L.length; j++)
{
if (L.Array[j] < L.Array[i])
{
tem = L.Array[i];
L.Array[i] = L.Array[j];
L.Array[j] = tem;
}
}
}
return;
}
/*****************堆排序********************/
void HeapAdjust(SqList &L, int s, int m)
{
int j;
ElemType rc = L.Array[s];
for (j = 2 * s; j <= m; j *= 2)
{
if (j<m&&L.Array[j]<L.Array[j+1]) j++;
if (rc >= L.Array[j])
break;
L.Array[s] = L.Array[j];
s = j;
}
L.Array[s] = rc;
return;
}
void HeapSort(SqList &L)//隶属于选择排序
{
int i; ElemType tem;
for (i = L.length / 2; i > 0; i--)//最大堆的初始化
HeapAdjust(L, i, L.length);
for (i = L.length; i > 1; i--)
{
{
tem = L.Array[1];
L.Array[1] = L.Array[i];
L.Array[i] = tem;
}
HeapAdjust(L, 1, i-1);
}
return;
}
/*******************************************/
//归并基程序
void Merge(ElemType S[], ElemType T[], int i, int m, int n)
{
int j, k;
j = m + 1; k = i;
while (i <= m&&j <= n)
{
if (S[i] <= S[j]) T[k++] = S[i++];
else T[k++] = S[j++];
}
if (i <= m)
while (k <= n&&i <= m) T[k++] = S[i++];
if (j <= n)
while (k <= n&&j <= n) T[k++] = S[j++];
return;
}
/*********************归并排序*************************/
void MSort(ElemType S[], ElemType T[],int s,int t)
{
int m; ElemType R[20];
if (s == t) T[s] = S[s];
else{
m = (s + t) / 2;
MSort(S,R,s,m);
MSort(S,R,m+1,t);
Merge(R, T, s, m, t);
}
return;
}
void MergeSort(SqList &L)
{
MSort(L.Array, L.Array, 1, L.length);
return;
}
/**************************************************/
int main(void)
{
SqList L; int i;
cout << "The length of LIST :"; cin >> L.length;
cout << "enter nodes:" << endl;
for (i = 1; i <= L.length; i++)
cin >> L.Array[i];
for (i = 1; i <= L.length; i++)
cout << L.Array[i] << " ";
cout << endl;
//HeapSort(L);
//SelectSort(L);
//QuickSort(L, L.length);
//BubbleSort(L);
//InsertSort(L);
//BInsertSort(L);
//ShellSort(L,4);
MergeSort(L);
for (i = 1; i <= L.length; i++)
cout << L.Array[i] << " ";
cout << endl;
return(0);
}