数据结构

有关数据结构,简述各种排序算法思想

数据结构中包含希尔排序,快速排序,堆排序,还有比如常见的起泡排序

希尔排序又称"缩小增量排序",它的基本思想是,先对待排序列进行"宏观调整",待序列中的记录"基本有序"时再进行直接插入排序例如一个含11个关键字的序列 (16,25,12,30,47,11,23,36,9,18,31),先对它进行"增量为5"的插入排序,即分别使、、、 和 为有序序列,然后将增量"缩小到3",排序结果使 、 和 分别成为有序序列,此时序列中在关键字18,23,25,31和47之前的关键字均比它们小,即在进行最后一趟排序时这几个关键字都不需要"往前进行"插入,之后经过最后一趟插入排序即得到有序序列

快速排序:例子如下,关键字序列 ( 52, 49, 80, 36, 14, 75, 58, 97, 23, 61 )
经第1趟快速排序之后为 ( 23, 49, 14, 36) 52 (75, 58, 97, 80, 61 )
经第2趟快速排序之后为 ( 14) 23 (49, 36) 52 (61, 58) 75 (80, 97 )
经第3趟快速排序之后为 ( 14, 23, 36, 49, 52, 58, 61, 75, 80, 97 )

堆分为大顶堆和小顶堆他们满足下列条件:若含n个元素的序列 {,,…,} 满足下列关系则称作"小顶堆" 或"大顶堆" 。"堆顶" 元素为序列中的"最小值"或"最大值"。

例如,{12, 39, 20, 65, 47, 34, 98, 81, 73, 56}为"小顶堆";
{98, 81, 34, 73, 56, 12, 20, 39, 65, 47}为"大顶堆"。

堆其实可以看成是一个完全二叉树。

二路归并算法:

归并排序的基本操作是将两个或两个以上的记录有序序列归并为一个有序序列。最简单的情况是,只含一个记录的序列显然是个有序序列,经过"逐趟归并"使整个序列中的有序子序列的长度逐趟增大,直至整个记录序列为有序序列止。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-07-03
//LS的顶个,但是还有错误
#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)
{
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]; 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; [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";
}

int 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 m,n,i,j,k;
cout<<"请选择测试数据类型:⑴正序 ⑵逆序 ⑶随机 [ 若跳出,请按⑷ ]" <<endl;
cin>>m;
while(m>0&&m<4)
{
cout<<"请选择排序算法:⑴直接插入排序 ⑵希尔排序 ⑶冒泡排序 ⑷快速排序 \n ⑸简单选择排序"<<endl;
cin>>n;
switch(n)
{
case 1:
cout << "直接插入排序前:" << "\n";
for(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(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(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(j=1;j<numv;j++) cout<<a[m-1][j]<<" ";
cout << "\n快速排序结果为:" << "\n";
QuickSort(a[m-1],0,numv-1);
for(i=1;i<numv;i++)
cout<<a[m-1][i]<<" ";
cout<<"\n";
break;
case 5:
cout << "\n简单选择排序前:" << "\n";
for(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;
return 0;
}本回答被提问者采纳
第2个回答  2010-07-02
//二楼的顶个,但是全是错误啊,改了下!!测试了没问题了!!
#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";
}

int 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 m,n,i,j,k;
cout<<"请选择测试数据类型:⑴正序 ⑵逆序 ⑶随机 [ 若跳出,请按⑷ ]" <<endl;
cin>>m;
while(m>0&&m<4)
{
cout<<"请选择排序算法:⑴直接插入排序 ⑵希尔排序 ⑶冒泡排序 ⑷快速排序 \n ⑸简单选择排序"<<endl;
cin>>n;
switch(n)
{
case 1:
cout << "直接插入排序前:" << "\n";
for(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(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(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(j=1;j<numv;j++) cout<<a[m-1][j]<<" ";
cout << "\n快速排序结果为:" << "\n";
QuickSort(a[m-1],0,numv-1);
for(i=1;i<numv;i++)
cout<<a[m-1][i]<<" ";
cout<<"\n";
break;
case 5:
cout << "\n简单选择排序前:" << "\n";
for(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;
return 0;
}
第3个回答  2010-07-02
刚做完的
#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;
第4个回答  2010-07-02
我看完一个网址。你也可以看看
http://blog.csdn.net/wuhongze/archive/2004/11/26/194293.aspx