数据结构C语言——实现各种排序算法

功能要求:实现直接插入算法、希尔排序算法、冒泡法排序算法、简单选择排序和快速排序算法。存储结构自己选择。

要整个一套程序,这些算法都包含在那一套程序里,可以在主函数里随意调用任何一个。 要调试后的代码,拿来直接能运行的。 最好附带一些简单的讲解。。。对了,要数据结构那种的编写!~~

各路神仙,各位大侠们,快帮帮小弟我吧!!!谢谢了!!!

刚做完的
#include <iostream>
using namespace std;

void BiInsertsort(int r[], int n) //插入排序(折半)
{
for(int i=2;i<=n;i++)
{
if (r[i]<r[i-1])
{
r[0] = r[i]; //设置哨兵
int low=1,high=i-1; //折半查找
while (low<=high)
{
int mid=(low+high)/2;
if (r[0]<r[mid]) high=mid-1;
else low = mid+1;
}
int j;
for (j=i-1;j>high;j--) r[j+1] = r[j]; //后移
r[j+1] = r[0];
}
}
for(int k=1;k<=n;k++) cout<<r[k]<<" ";
cout<<"\n";
}

void ShellSort ( int r[], int n) //希尔排序
{
for(int d=n/2;d>=1;d=d/2) //以d为增量进行直接插入排序
{
for (int i=d+1;i<=n;i++)
{
r[0] = r[i]; //暂存被插入记录
int j;
for( j=i-d; j>0 && r[0]<r[j]; j=j-d) r[j+d] = r[j]; //记录后移d个位置
r[j+d] = r[0];

}
}
for(int i=1;i<=n;i++) cout<<r[i]<<" ";
cout<<"\n";
}

void BubbleSort(int r[], int n) //起泡排序
{
int temp,exchange,bound;
exchange=n; //第一趟起泡排序的范围是r[0]到r[n-1]
while (exchange) //仅当上一趟排序有记录交换才进行本趟排序
{
bound=exchange;
exchange=0;
for (int j=1; j<bound; j++) //一趟起泡排序
if (r[j]>r[j+1])
{
temp=r[j];
r[j]=r[j+1];
r[j+1]=temp;
exchange=j; //记录每一次发生记录交换的位置
}
}
for(int i=1;i<=n;i++) cout<<r[i]<<" ";
cout<<"\n";
}

int Partition(int r[], int first, int end) //快速排序一次划分
{
int i=first; //初始化
int j=end;
r[0]=r[first];
while (i<j)
{
while (i<j && r[0]<= r[j]) j--; //右侧扫描
r[i]=r[j];
while (i<j && r[i]<= r[0]) i++; //左侧扫描
r[j]=r[i];
}
r[i]=r[0];
return i; //i为轴值记录的最终位置
}
void QuickSort(int r[], int first, int end) //快速排序
{
if (first<end)
{ //递归结束
int pivot=Partition(r, first, end); //一次划分
QuickSort(r, first, pivot-1);//递归地对左侧子序列进行快速排序
QuickSort(r, pivot+1, end); //递归地对右侧子序列进行快速排序
}
}

void SelectSort(int r[ ], int n) //简单选择排序
{
int i,j,index,temp;
for (i=1; i<n; i++) //对n个记录进行n-1趟简单选择排序
{
index=i;
for (j=i+1; j<=n; j++) //在无序区中选取最小记录
if (r[j]<r[index]) index=j;
if (index!=i)
{
temp=r[i];
r[i]=r[index];
r[index]=temp;
}
}
for(i=1;i<=n;i++) cout<<r[i]<<" ";
cout<<"\n";
}

void main()
{
const int numv=12;
int a[3][numv]={{0,6,13,19,23,37,39,41,45,48,58,86},{0,86,58,48,45,41,39,37,23,19,13,6},{0,23,13,48,86,19,6,41,58,37,45,39}};
int z1[numv],z2[numv];
int m,n;
cout<<"请选择测试数据类型:⑴正序 ⑵逆序 ⑶随机 [ 若跳出,请按⑷ ]" <<endl;
cin>>m;
while(m>0&&m<4)
{
cout<<"请选择排序算法:⑴直接插入排序 ⑵希尔排序 ⑶冒泡排序 ⑷快速排序 \n ⑸简单选择排序"<<endl;
cin>>n;
switch(n)
{
case 1:
cout << "直接插入排序前:" << "\n";
for(int j=1;j<numv;j++) cout<<a[m-1][j]<<" ";
cout << "\n直接插入排序结果为:" << "\n";
BiInsertsort(a[m-1],numv-1);
break;
case 2:
cout << "\n希尔排序前:" << "\n";
for(int j=1;j<numv;j++) cout<<a[m-1][j]<<" ";
cout << "\n希尔排序结果为:" << "\n";
ShellSort(a[m-1], numv-1);
break;
case 3:
cout << "\n冒泡排序前:" << "\n";
for(int k=1;k<numv;k++) cout<<a[m-1][k]<<" ";
cout << "\n冒泡排序结果为:" << "\n";
BubbleSort(a[m-1], numv-1);
break;
case 4:
cout << "\n快速排序前:" << "\n";
for(int j=1;j<numv;j++) cout<<a[m-1][j]<<" ";
cout << "\n快速排序结果为:" << "\n";
QuickSort(a[m-1],0,numv-1);
for(int i=1;i<numv;i++)
cout<<a[m-1][i]<<" ";
cout<<"\n";
break;
case 5:
cout << "\n简单选择排序前:" << "\n";
for(int j=1;j<numv;j++) cout<<a[m-1][j]<<" ";
cout << "\n简单选择排序结果为:" << "\n";
SelectSort(a[m-1],numv-1);
break;

default:
cout<<"输入错误!"<<endl;
}
m=0;
cout<<"请选择测试数据类型:⑴正序 ⑵逆序 ⑶随机 [ 若跳出,请按⑷ ]" <<endl;
cin>>m;
}
if(m==4) cout<<"(*^__^*) 再见!"<<endl;
else cout<<"输入错误!"<<endl;
}
温馨提示:答案为网友推荐,仅供参考
相似回答