早教吧 育儿知识 作业答案 考试题库 百科 知识分享

帮我找找我这段程序哪里错了?c++编程,使用以下格式给定一个输入,读取数据后,用快速排序法将每排从小到大重新排好(Quicksort:pivot=(x[left]+x[right])/2)Cases:3101234567891010109876543

题目详情
帮我找找我这段程序哪里错了?
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];
\x05\x05}
\x05\x05
\x05\x05beg=0;
\x05\x05end=n-1;
\x05\x05quicksort(input,0,9); //Calling of QuickSort Function
\x05\x05for(i=1;i
▼优质解答
答案和解析
for(i=1;i>input[i];
}
输入的时候是下标1~n
quicksort(input, 0, 9);
为什么排序时候会是0~9呢,应该是也是1~n才对吧