合并排序和快速排序 C++ 的工具箱(有错误的代码)

Toolbox for MergeSort & QuickSort C++ (Code with errors)

大家好,我找不到错误。这是我的代码:

#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;
 }

我想更正那个错误,但我能找到它。 非常欢迎任何帮助或提示。 终端说这是一个遗漏的括号,但我看到了所有的括号。

在此处在线缩进您的代码。然后,正如编译器所说,转到第 131 行并注意 main 没有右大括号。添加一个,例如:

    ...
    }while(opt != -1);
    return 0;
}

这将解决这个问题,但代码也有其他错误,但我会把有趣的部分留给你。

我有一个 Quicksort (C++) 的例子,可能会派上用场。