Hey Guys我很难找到错误。这是我的代码:
#include <iostream>
using namespace std;
int a[20], n, lb, loc, ub, left, right, temp, temp1;
void quicksort(int[10],int,int);
int pivot(int[],int,int);
void merge(int *,int, int , int );
void mergesort(int *a, int low, int high)
{
int mid;
if (low < high)
{
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,high,mid);
}
return;
}
void merge(int *a, int low, int high, int mid)
{
int i, j, k, c[50];
i = low;
k = low;
j = mid + 1;
while (i <= mid && j <= high)
{
if (a[i] < a[j])
{
c[k] = a[i];
k++;
i++;
}
else
{
c[k] = a[j];
k++;
j++;
}
}
while (i <= mid)
{
c[k] = a[i];
k++;
i++;
}
while (j <= high)
{
c[k] = a[j];
k++;
j++;
}
for (i = low; i < k; i++)
{
a[i] = c[i];
}
}
int main()
{
int opt;
cout << "QuickSort & MergeSort Toolbox: " << endl;
do
{
int opt;
cout << "1. MergeSort" << endl;
cout << "2. QuickSort" << endl;
cout << "enter option: ";
cin >> opt;
switch(opt)
{
case 1:
{
cout << "MERGE SORT" << endl;
int a[20], i, b[20];
cout<<"Enter the elements\n";
for (i = 0; i < 5; i++)
{
cin>>a[i];
}
mergesort(a, 0, 4);
cout<<"Sorted array\n";
for (i = 0; i < 5; i++)
{
cout<<a[i];
}
cout<<"Enter the elements\n";
for (i = 0; i < 5; i++)
{
cin>>b[i];
}
mergesort(b, 0, 4);
cout<<"Sorted array\n";
for (i = 0; i < 5; i++)
{
cout<<b[i];
}
}
case 2:
{
cout<<"Enter size of array";
cin>>n;
cout<<"Enter Array Elements ";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
quicksort(a,0,n-1);
for(int z=0;z<n;z++)
{
cout<<" "<<a[z];
}
return 0;
}
default:
{
cout << "Invalid Input" << endl;
}
}
}while(opt != -1);
void quicksort(int a[], int lb, int ub)
{
int p;
if(lb<ub)
{
p=pivot(a,lb,ub);
quicksort(a,lb,p-1);
quicksort(a,p+1,ub);
}
}
int pivot( int a[],int lb,int ub )
{
for(int z=0;z<n;z++)
{
cout<<" "<<a[z];
}
cout<<endl;
left =lb;
right = ub;
loc =lb;
cout<<"Right Side is:- "<<right;
cout<<"\tLocation is:-"<<loc;
cout<<"Left Side is:- "<<left;
cout<<"Now Right Side is: \n";
while((a[loc]<=a[right]) && (loc!=right))
{
right=right-1;
}
if(loc==right)
{
return loc;
}
temp=a[loc];
a[loc]=a[right];
a[right]=temp;
loc=right;
cout<<"Now Left Side is: \n";
while((a[left]<=a[loc]) && (loc!=left))
{
left=left+1;
}
if(loc==left)
{
return loc;
}
temp1=a[loc];
a[loc]=a[left];
a[left]=temp1;
loc=left;
}
我想纠正这些错误,但我很难找到它。 任何帮助或提示都是非常受欢迎的。 终端说这是一个错过的支架,但我看到了所有这些。
答案 0 :(得分:1)
在线缩进您的代码。然后,正如编译器所说,转到第131行并注意到main没有结束花括号。添加一个,例如:
...
}while(opt != -1);
return 0;
}
这将解决这个问题,但代码也会遇到其他错误,但我会为你留下有趣的部分。
我有一个Quicksort (C++)的例子,它可能派上用场。