#include <iostream>
//直接插入排序
void DirectInsertSort(int a[],int count)
{
int i,j;
int key=a[0];
for (i=1;i<count;i++)
{
if (a[i]<a[i-1])
{
key=a[i];
a[i]=a[i-1];
for (j=i-2;a[j]>key;j--)
{
a[j+1]=a[j];
}
a[j+1]=key;
}
}
}
//二分插入排序或折半插入排序
void BinaryInsertSort(int a[],int count)
{
int i,low,high,mid,j;
int key=a[0];
for (i=1;i<count;i++)
{
key=a[i];
low=0;
high=i-1;
while(low<=high)
{
mid=(low+high)/2;
if (a[mid]>key)
{
high=mid-1;
}
else
{
low=mid+1;
}
}
for (j=i-1;j>=high+1;j--)
{
a[j+1]=a[j];
}
a[j+1]=key;
}
}
//希尔排序
void ShellSort(int a[],int dk,int count)
{
int i;
while (dk>0)
{
for (i=dk;i<count;i++)
{
if (a[i]<a[i-dk])
{
std::swap(a[i],a[i-dk]);
}
}
dk=dk-2==0?1:dk-2;
}
}
//冒泡排序
void BubbleSort(int a[],int count)
{
int i;
///*方法1:判断较大数,移至最后*/
//for (i=0;i<count-1;i++)
//{
// for (int j=0;j<count-i-1;j++)
// {
// if (a[j]>a[j+1])
// {
// std::swap(a[j],a[j+1]);
// }
// }
//}
/*方法2:判断较小数,移至最前*/
for (i=0;i<count;i++)
{
for (int j=count-1;j>i;j--)
{
if (a[j]<a[j-1])
{
std::swap(a[j],a[j-1]);
}
}
}
}
//快速排序
void QuickSort(int a[],int lowIndex,int highIndex)
{
if (lowIndex>highIndex)
{
return;
}
int first=lowIndex;
int last=highIndex;
int key=a[first];
while (first<last)
{
while (first<last && a[last]>=key)
{
--last;
}
a[first]=a[last];
while (first<last && a[first]<=key)
{
++first;
}
a[last]=a[first];
}
a[first]=key;
QuickSort(a,lowIndex,first-1);
QuickSort(a,last+1,highIndex);
}
//选择排序
void SelectionSort(int a[],int count)
{
int i,j;
for (i=0;i<count;i++)
{
int min=9999;
int index=-1;
for (j=i;j<count;j++)
{
if (min>a[j])
{
min=a[j];
index=j;
}
}
if (index!=i)
{
a[index]=a[i];
a[i]=min;
}
}
}
//堆排序之小顶堆
void heapAdjustMin(int a[],int count)
{
int i,j;
for (i=count/2-1;i>-1;i--)
{
for (j=2*i+1;j<count;j=2*j+1)
{
if (j+1<count && a[j]>a[j+1])
{
j++;
}
if (a[i]>a[j])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
i=j;
}
else
{
break;
}
}
}
}
//堆排序之大顶堆
void heapAdjustMax(int a[],int count)
{
int i,j;
for (i=count/2-1;i>-1;i--)
{
for (j=2*i+1;j<count;j=2*j+1)
{
if (j+1<count && a[j]<a[j+1])
{
j++;
}
if (a[i]<a[j])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
i=j;
}
else
{
break;
}
}
}
}
//堆排序
void HeapSort(int a[],int count,bool isBig)
{
if (isBig)
{
heapAdjustMax(a,count);
for (int i=0;i<count-1;i++)
{
std::swap(a[0],a[count-i-1]);
heapAdjustMax(a,count-i-1);
}
}
else
{
heapAdjustMin(a,count);
for (int i=0;i<count-1;i++)
{
std::swap(a[0],a[count-i-1]);
heapAdjustMin(a,count-i-1);
}
}
}
int main()
{
std::cout<<"****************************"<<std::endl;
std::cout<<"本程序包含以下几种排序方法:"<<std::endl;
std::cout<<"1、直接插入排序"<<std::endl;
std::cout<<"2、折半插入排序"<<std::endl;
std::cout<<"3、希尔排序"<<std::endl;
std::cout<<"4、冒泡排序"<<std::endl;
std::cout<<"5、快速排序"<<std::endl;
std::cout<<"6、简单选择排序"<<std::endl;
std::cout<<"7、堆排序之大顶堆"<<std::endl;
std::cout<<"8、堆排序之小顶堆"<<std::endl;
std::cout<<"****************************"<<std::endl;
int num;
do
{
std::cout<<"请输入你所选择的排序方法的序号:";
std::cin>>num;
if (num<1 || num>8)
{
std::cout<<"错误!"<<std::endl;
}
else
{
int a[]={49,38,65,97,76,13,27,49};
int count=sizeof(a)/sizeof(int);
int i;
std::cout<<"初始数字序列为: ";
for (i=0;i<count;i++)
{
std::cout<<a[i]<<" ";
}
std::cout<<std::endl;
switch(num)
{
case 1:
DirectInsertSort(a,count);
std::cout<<"直接插排后序列为: ";
break;
case 2:
BinaryInsertSort(a,count);
std::cout<<"折半插排后序列为: ";
break;
case 3:
ShellSort(a,(count+1)/2,count);
std::cout<<"希尔排序后序列为: ";
break;
case 4:
BubbleSort(a,count);
std::cout<<"冒泡排序后序列为: ";
break;
case 5:
QuickSort(a,0,count-1);
std::cout<<"快速排序后序列为: ";
break;
case 6:
SelectionSort(a,count);
std::cout<<"简单选择排序后序列为: ";
break;
case 7:
HeapSort(a,count,true);
std::cout<<"大顶堆堆排序后序列为: ";
break;
case 8:
HeapSort(a,count,false);
std::cout<<"小顶堆堆排序后序列为: ";
break;
default:
break;
}
for (i=0;i<count;i++)
{
std::cout<<a[i]<<" ";
}
std::cout<<std::endl;
}
} while (1);
return 0;
}
11-16