帮我找找我这段程序哪里错了?c++编程,使用以下格式给定一个输入,读取数据后,用快速排序法将每排从小到大重新排好(Qui
帮我找找我这段程序哪里错了?
c++编程,使用以下格式给定一个输入,读取数据后,用快速排序法将每排从小到大重新排好(Quicksort:pivot = (x[left] + x[right])/2)
Cases:3
10
1 2 3 4 5 6 7 8 9 10
10
10 9 8 7 6 5 4 3 2 1
11
8 1 11 2 10 9 3 4 7 6 5
我的程序,结果是
1 2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10 1
1 2 3 4 7 8 9 10 11 6 5
#include
using namespace std;
// The partition function
int partition(int* input,int p,int r)
{
x05 int pivot = (input[p] + input[r])/2;
while ( p < r )
{
while ( input[p] < pivot )
p++;
while ( input[r] > pivot )
r--;
if ( input[p] == input[r] )
p++;
else if ( p < r )
{
int tmp = input[p];
input[p] = input[r];
input[r] = tmp;
}
}
return r;
}
// The quicksort recursive function
void quicksort(int* input,int p,int r)
{
if ( p < r )
{
int j = partition(input,p,r);
quicksort(input,p,j-1);
quicksort(input,j+1,r);
}
}
int main()
{
x05int input[100],i,n,beg,end,test,j;
x05couttest;
x05coutinput[i];
x05x05}
x05x05
x05x05beg=0;
x05x05end=n-1;
x05x05quicksort(input,0,9); //Calling of QuickSort Function
x05x05for(i=1;i