C++ MergeSort&;快速排序C++;(带错误的代码)
嘿,伙计们,我很难找到错误。这是我的密码:C++ MergeSort&;快速排序C++;(带错误的代码),c++,quicksort,mergesort,C++,Quicksort,Mergesort,嘿,伙计们,我很难找到错误。这是我的密码: #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 lo
#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;
}
#包括
使用名称空间std;
INTA[20],n,lb,loc,ub,左,右,temp,temp1;
无效快速排序(int[10],int,int);
int轴(int[],int,int);
无效合并(int*,int,int,int);
无效合并排序(整数*a、整数低位、整数高位)
{
int mid;
如果(低<高)
{
中等=(低+高)/2;
合并排序(a、低、中);
合并排序(a、中+1、高);
合并(a、低、高、中);
}
返回;
}
无效合并(整数*a、整数低、整数高、整数中)
{
inti,j,k,c[50];
i=低;
k=低;
j=mid+1;
while(i在这里在线缩进您的代码。然后,正如编译器所说,转到第131行,注意main没有结束的花括号。添加一个,例如:
...
}while(opt != -1);
return 0;
}
这将解决这个问题,但代码也有其他错误,但我将把有趣的部分留给您
我有一个例子,它可能会派上用场