然後還有壹個跟程序有關的錯誤,這個只有寫這個程序的人才知道這是這個錯誤怎麽解決,雖然我也可以解決,但是帶麻煩了,所以妳自己看看那個錯誤怎麽解決把!
#include<iostream>
#include <cstdlib>
#include <ctime>
#define N 50
using namespace std;
void InsertSort(int r[])
{ int x=0,y=0;
for(int i=1;i<N;i++)
{
int s=r[i];
int low=0;
int high=i-1;
x++;
while(low <= high)
{
int mid=(low+high)/2;
x++;
if(s < r[mid])
{
high=mid-1;
y++;
}
else
{
low=mid+1;
y++;
}
}
for(int j=i-1;j>=high+1;i++,j--)
{
r[j+1]=r[j];
y++;
}
r[high+1]=s;
y++;
}
cout<<"插入排序:";
cout<<endl<<"***進行比較"<<x<<"次,進行交換"<<y<<"次"<<endl<<"***************************"<<endl;
}
void BubbleSort(int r[] )
{ int x=0,y=0;
for(int i=0;i<N-1;i++)
{
for(int j=0;j<N-1-i;j++)
{ i++;
if(r[j]>r[j+1])
{
int temp=r[j];
r[j]=r[j+1];
r[j+1]=temp;
y+=3;
}
}
}
cout<<"冒泡法:";
cout<<endl<<"***進行比較"<<x<<"次,進行交換"<<y<<"次"<<endl<<"***************************"<<endl;
}
void ShellSort(int r[] )
{
int step=N/2;
int x=0,y=0;
while(step >= 1)
{
for(int i=step;i<N;i+=step)
{
for(int j=i-step,s=r[i];s<r[j] && j>=0;j-=step,x++)
{
r[j+step]=r[j];
y++;
}
r[j+step]=s;
y++;
}
step/=2;
}
cout<<"希爾排序:";
cout<<endl<<"***進行比較"<<x<<"次,進行交換"<<y<<"次"<<endl<<"***************************"<<endl;
}
void SelectSort(int r[] )
{
int x=0,y=0;
for(int i=0;i<N-1;i++)
{
int s=i;
for(int j=i+1;j<N;j++)
{
if(r[s] > r[j])
{
x++;
s=j;
}
}
if(small_loc != i)
{
int temp=r[i];
r[i]=r[s];
r[s]=temp;
y+=3;
}
}
cout<<"選擇排序:";
cout<<endl<<"***進行比較"<<x<<"次,進行交換"<<y<<"次"<<endl<<"***************************"<<endl;
}
int Partition(int r[],int low,int high,int d[])
{
int pivotkey=r[low];
int i=low;
int j=high;
while(i<j)
{
while(i<j && r[j]>pivotkey) {
j--;d[0]++;
}
if(i<j){
r[i]=r[j];i++;d[1]++;
}
while(i<j && r[i]<pivotkey) {
i++;d[0]++;
}
if(i<j){
r[j]=r[i];j--;d[1]++;
}
}
r[j]=pivotkey;d[1]++;
return j;
}
void QuickSort(int r[],int low,int high,int d[])
{
if(low<high)
{
int pivot=Partition(r,low,high,d);
QuickSort(r,low,pivot-1,d);
QuickSort(r,pivot+1,high,d);
}
cout<<"快速排序:";
cout<<endl<<"***進行比較"<<d[0]<<"次,進行交換"<<d[1]<<"次"<<endl<<"***************************"<<endl;
}
int main()
{
int a[N];int d[2];
srand((unsigned)time(NULL));
a[N]=rand();
InsertSort(a);
BubbleSort(a);
ShellSort(a);
SelectSort(a);
QuickSort(a,0,N,d);
return 0;
}