首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

排序有关问题,异常很不解,指教

2013-01-22 
排序问题,错误很不解,指教#includeiostream#includectime#includecctypeusing namespace stdint i,

排序问题,错误很不解,指教
#include<iostream>
#include<ctime>
#include<cctype>
using namespace std;
int i,j;
int temp;
//直接插入排序
void Insert(int r[],int n)
{
for(i=2;i<=n;i++)
{
r[0]=r[i];
for(j=i-1;r[0]<r[i];j--)
{
r[j+1]=r[j];
r[j+1]=r[0];
}
}
//希尔排序
void Shellsort(int r[],int n)
{
for(int d=n/2;d>=1;d=d/2)
{
for(i=d+1;i<=n;i++)
{
r[0]=r[i];
for(j=i-d;j>0&&r[0]<r[j];j=j-d)


r[j+d]=r[j];
r[j+d]=r[0];

}
}
}

//交换排序
   void BubbleSort(int r[],int n)
   {
   int exchange=n,bound;
   while(exchange)
   {
   boud=exchange;
   exchange=0;
   for(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;
   }
   }
   }
   //快速排序
 int Partition(int r[],int first,int end)
 {
 i=first;
 j=end;
 while(i<j)
 {
 while(i<j&&r[i]<r[j])
 j--;
 if(i<j)
 {
 temp=r[j];
 r[j]=r[i];
 r[i]=temp;
 i++;
 }
 while(i<j&&r[i]<=r[j])
i++;
 if(i<j)
 {
 temp=r[j];
 r[j]=r[i];
 r[i]=temp;
 j--;
 }
 }
 return i;
 }
void QuikSort(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 index;

for(i=1;i<n;i++)

{
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]=r[i];
}
}
//堆排序
void Sift(int r[],int k,int m)
{
i=k;
j=2*i;
while(j<=m)
{
if(j<m&&r[j]<r[j+1])j++;
if(r[i]>r[j])break;
    else
{
temp=r[i];
r[i]=r[j];
r[j]=temp;
i=j;
j=2*i;
}
}
}
void HeapSort(int r[],int n)
{
for(i=n/2;i>=1;i--)
Sift(r,i,n)
for(i=1;i<n;i++)
{
temp=r[1];
r[1]=r[n-i+1];
Sift(r,1,n-i);
}
}
 void input()
{
cout<<"1.直接插入排序。2.冒泡排序。3 希尔排序。4.快速排序。5.选择排序6.堆排序。"<<endl;
}


void main()
{
int r[2000],c;
char ch;
bool f=true;
srand(time(NULL));
for(i=1;i<=2000;i++)
{
r[i]=rand()%2000+1;
}
while(f)
{
input();
cin>>c;
switch(c)
{
case 1: Insert( r, 2000);
break;
        case2: BubbleSort( r, 2000);break;
case 3:Shellsort(r, 2000);break;
case 4:Partition( r,1,2000);break;


case 5:SelectSort(r, 2000);break;
case 6:HeapSort(r, 2000);break;
default: break;
}
cout<<"是否要继续排序(Y|N)"<<endl;
cin>>ch;
if(toupper(ch)=='Y')
     f=true;
 else
 f=false;
}
}





#include<iostream>
#include<ctime>
#include<cctype>
using namespace std;
int i,j;
int temp;
//直接插入排序
void Insert(int r[],int n)
{
for(i=2;i<=n;i++)
{
r[0]=r[i];
for(j=i-1;r[0]<r[i];j--)
{
r[j+1]=r[j];
r[j+1]=r[0];
}
}
//希尔排序
void Shellsort(int r[],int n)
{
for(int d=n/2;d>=1;d=d/2)
{
for(i=d+1;i<=n;i++)
{
r[0]=r[i];
for(j=i-d;j>0&&r[0]<r[j];j=j-d)


r[j+d]=r[j];
r[j+d]=r[0];

}
}
}

//交换排序
   void BubbleSort(int r[],int n)
   {
   int exchange=n,bound;
   while(exchange)
   {
   boud=exchange;
   exchange=0;
   for(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;
   }
   }
   }
   //快速排序
 int Partition(int r[],int first,int end)
 {
 i=first;
 j=end;
 while(i<j)
 {
 while(i<j&&r[i]<r[j])
 j--;
 if(i<j)
 {
 temp=r[j];
 r[j]=r[i];
 r[i]=temp;
 i++;
 }
 while(i<j&&r[i]<=r[j])
i++;
 if(i<j)
 {
 temp=r[j];
 r[j]=r[i];
 r[i]=temp;
 j--;
 }
 }
 return i;
 }
void QuikSort(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 index;

for(i=1;i<n;i++)

{
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]=r[i];
}
}
//堆排序
void Sift(int r[],int k,int m)
{
i=k;
j=2*i;
while(j<=m)
{
if(j<m&&r[j]<r[j+1])j++;
if(r[i]>r[j])break;
    else
{
temp=r[i];
r[i]=r[j];
r[j]=temp;
i=j;
j=2*i;
}
}
}
void HeapSort(int r[],int n)
{
for(i=n/2;i>=1;i--)
Sift(r,i,n)
for(i=1;i<n;i++)
{
temp=r[1];
r[1]=r[n-i+1];
Sift(r,1,n-i);
}
}
 void input()
{
cout<<"1.直接插入排序。2.冒泡排序。3 希尔排序。4.快速排序。5.选择排序6.堆排序。"<<endl;
}


void main()
{
int r[2000],c;
char ch;
bool f=true;
srand(time(NULL));
for(i=1;i<=2000;i++)
{
r[i]=rand()%2000+1;
}


while(f)
{
input();
cin>>c;
switch(c)
{
case 1: Insert( r, 2000);
break;
        case2: BubbleSort( r, 2000);break;
case 3:Shellsort(r, 2000);break;
case 4:Partition( r,1,2000);break;
case 5:SelectSort(r, 2000);break;
case 6:HeapSort(r, 2000);break;
default: break;
}
cout<<"是否要继续排序(Y|N)"<<endl;
cin>>ch;
if(toupper(ch)=='Y')
     f=true;
 else
 f=false;
}
}


#include<iostream>
#include<ctime>
#include<cctype>
using namespace std;
int i,j;
int temp;
//直接插入排序
void Insert(int r[],int n)
{
for(i=2;i<=n;i++)
{
r[0]=r[i];
for(j=i-1;r[0]<r[i];j--)
{
r[j+1]=r[j];
r[j+1]=r[0];
}
}
//希尔排序
void Shellsort(int r[],int n)
{
for(int d=n/2;d>=1;d=d/2)
{
for(i=d+1;i<=n;i++)
{
r[0]=r[i];
for(j=i-d;j>0&&r[0]<r[j];j=j-d)


r[j+d]=r[j];
r[j+d]=r[0];

}
}
}

//交换排序
   void BubbleSort(int r[],int n)
   {
   int exchange=n,bound;
   while(exchange)
   {
   boud=exchange;
   exchange=0;
   for(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;
   }
   }
   }
   //快速排序
 int Partition(int r[],int first,int end)
 {
 i=first;
 j=end;
 while(i<j)
 {
 while(i<j&&r[i]<r[j])
 j--;
 if(i<j)
 {
 temp=r[j];
 r[j]=r[i];
 r[i]=temp;
 i++;
 }
 while(i<j&&r[i]<=r[j])
i++;
 if(i<j)
 {
 temp=r[j];
 r[j]=r[i];
 r[i]=temp;
 j--;
 }
 }
 return i;
 }
void QuikSort(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 index;

for(i=1;i<n;i++)

{
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]=r[i];
}
}
//堆排序
void Sift(int r[],int k,int m)
{
i=k;
j=2*i;
while(j<=m)
{
if(j<m&&r[j]<r[j+1])j++;
if(r[i]>r[j])break;
    else
{
temp=r[i];
r[i]=r[j];
r[j]=temp;
i=j;
j=2*i;
}
}
}
void HeapSort(int r[],int n)
{
for(i=n/2;i>=1;i--)
Sift(r,i,n)
for(i=1;i<n;i++)
{
temp=r[1];
r[1]=r[n-i+1];
Sift(r,1,n-i);
}
}


 void input()
{
cout<<"1.直接插入排序。2.冒泡排序。3 希尔排序。4.快速排序。5.选择排序6.堆排序。"<<endl;
}


void main()
{
int r[2000],c;
char ch;
bool f=true;
srand(time(NULL));
for(i=1;i<=2000;i++)
{
r[i]=rand()%2000+1;
}
while(f)
{
input();
cin>>c;
switch(c)
{
case 1: Insert( r, 2000);
break;
        case2: BubbleSort( r, 2000);break;
case 3:Shellsort(r, 2000);break;
case 4:Partition( r,1,2000);break;
case 5:SelectSort(r, 2000);break;
case 6:HeapSort(r, 2000);break;
default: break;
}
cout<<"是否要继续排序(Y|N)"<<endl;
cin>>ch;
if(toupper(ch)=='Y')
     f=true;
 else
 f=false;
}
}


\编程练习\排序.cpp(21) : error C2601: 'Shellsort' : local function definitions are illegal
E:\编程练习\排序.cpp(39) : error C2601: 'BubbleSort' : local function definitions are illegal
E:\编程练习\排序.cpp(57) : error C2601: 'Partition' : local function definitions are illegal
E:\编程练习\排序.cpp(84) : error C2601: 'QuikSort' : local function definitions are illegal
E:\编程练习\排序.cpp(95) : error C2601: 'SelectSort' : local function definitions are illegal
E:\编程练习\排序.cpp(189) : fatal error C1075: end of file found before the left brace '{' at 'E:\编程练习\排序.cpp(100)' was matched
执行 cl.exe 时出错.

错哪了










[解决办法]
你第一个函数少了半个大括号...然后造成了函数中定义函数的问题...
[解决办法]


//修改一下
#include<iostream>
#include<ctime>
#include<cctype>
using namespace std;
int i,j;
int temp;
int boud;
//直接插入排序
void Insert(int r[],int n)
{
for(i=2;i<=n;i++)
{
r[0]=r[i];
for(j=i-1;r[0]<r[i];j--)
{
r[j+1]=r[j];
r[j+1]=r[0];
}
}
}
//希尔排序
void Shellsort(int r[],int n)
{
for(int d=n/2;d>=1;d=d/2)
{
for(i=d+1;i<=n;i++)
{
r[0]=r[i];
for(j=i-d;j>0&&r[0]<r[j];j=j-d)


r[j+d]=r[j];
r[j+d]=r[0];

}
}
}

//交换排序
void BubbleSort(int r[],int n)
{
int exchange=n,bound;
while(exchange)
{
boud=exchange;
exchange=0;
for(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;
}
}
}
//快速排序
int Partition(int r[],int first,int end)
{
i=first;
j=end;
while(i<j)
{
while(i<j&&r[i]<r[j])
j--;
if(i<j)
{
temp=r[j];
r[j]=r[i];
r[i]=temp;
i++;
}
while(i<j&&r[i]<=r[j])
i++;
if(i<j)
{
temp=r[j];
r[j]=r[i];
r[i]=temp;
j--;
}
}
return i;


}
void QuikSort(int r[],int first,int end)
{
if(first<end)
{
int pivot=Partition(r,first,end);
QuikSort(r,first,pivot-1);
QuikSort(r,pivot+1,end);
}
}

//简单选择排序
void SelectSort(int r[],int n)
{
int index;
for(i=1;i<n;i++)
{
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]=r[i];
}
}
}
}
//堆排序
void Sift(int r[],int k,int m)
{
i=k;
j=2*i;
while(j<=m)
{
if(j<m&&r[j]<r[j+1])j++;
if(r[i]>r[j])break;
else
{
temp=r[i];
r[i]=r[j];
r[j]=temp;
i=j;
j=2*i;
}
}
}
void HeapSort(int r[],int n)
{
for(i=n/2;i>=1;i--)
Sift(r,i,n);
for(i=1;i<n;i++)
{
temp=r[1];
r[1]=r[n-i+1];
Sift(r,1,n-i);
}
}
void input()
{
cout<<"1.直接插入排序。2.冒泡排序。3 希尔排序。4.快速排序。5.选择排序6.堆排序。"<<endl;
}


void main()
{
int r[2000],c;
char ch;
bool f=true;
srand(time(NULL));
for(i=1;i<=2000;i++)
{
r[i]=rand()%2000+1;
}
while(f)
{
input();
cin>>c;
switch(c)
{
case 1: 
Insert( r, 2000);
break;
case2: 
BubbleSort( r, 2000);
break;
case 3:
Shellsort(r, 2000);
break;
case 4:
Partition( r,1,2000);
break;
case 5:
SelectSort(r, 2000);
break;
case 6:
HeapSort(r, 2000);
break;
default:
break;
}
cout<<"是否要继续排序(Y
[解决办法]
N)"<<endl;
cin>>ch;
if(toupper(ch)=='Y')
f=true;
else
f=false;
}
}

热点排行