2017-08-22 04:00

# 为什么我这个快排没有用？

#include
void swap(int *a, int *b) {
int temp = 0;
temp = *b;
*b = *a;
*a = temp;
}

int Rand(int low, int high) {
int size = high - low + 1;
return low + rand() % size;
}
int Partition(int a[],int start,int end,int length) {
int index = Rand(start, end);
swap(&a[index], &a[end]);

``````int small = start - 1;
for (index = start; index < end; ++index) {
if (a[index] < a[end]) {
++small;
if (small != index)
swap(&a[index], &a[small]);
}
}

++small;
swap(&a[small], &a[end]);

return small;
``````

}

void Qsort(int a[], int length, int start, int end) {
if (start == end)
return;

``````int index = Partition(a, start, end, length);
if (index > start) {
Qsort(a, start, index - 1, length);

}
if (index < end) {
Qsort(a, index + 1, end, length);

}
``````

}

void main() {
int a[] = { 7,6,44,8,2,3,1,9,6 };

``````Qsort(a, 9, 0, 8);

for (int j = 0; j < 9; j++) {
printf("%d ", a[j]);
}
``````

}

• 点赞
• 写回答
• 关注问题
• 收藏
• 复制链接分享
• 邀请回答

#### 2条回答

• das白 4年前

你参数传错了 如下

`````` void Qsort(int a[], int length, int start, int end) {
if (start == end)
return;
int index = Partition(a, start, end, length);
if (index > start) {
Qsort(a,length, start, index - 1);

}
if (index < end) {
Qsort(a, length,index + 1, end );

}
``````
点赞 评论 复制链接分享
• das白 4年前

if (start = end)
return
改成== 要不直接退出了

点赞 评论 复制链接分享