这个quicksort C程序有简单的版本吗? [英] Is there any simple version of this quicksort C program?
本文介绍了这个quicksort C程序有简单的版本吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include <stdio.h>
#include <stdbool.h>
#define MAX 7
int intArray[MAX] = {4,6,3,2,1,9,7};
void printline(int count) {
int i;
for(i = 0;i < count-1;i++) {
printf("=");
}
printf("=\n");
}
void display() {
int i;
printf("[");
// navigate through all items
for(i = 0;i < MAX;i++) {
printf("%d ",intArray[i]);
}
printf("]\n");
}
void swap(int num1, int num2) {
int temp = intArray[num1];
intArray[num1] = intArray[num2];
intArray[num2] = temp;
}
int partition(int left, int right, int pivot) {
int leftPointer = left -1;
int rightPointer = right;
while(true) {
while(intArray[++leftPointer] < pivot) {
//do nothing
}
while(rightPointer > 0 && intArray[--rightPointer] > pivot) {
//do nothing
}
if(leftPointer >= rightPointer) {
break;
} else {
printf(" item swapped :%d,%d\n", intArray[leftPointer],intArray[rightPointer]);
swap(leftPointer,rightPointer);
}
}
printf(" pivot swapped :%d,%d\n", intArray[leftPointer],intArray[right]);
swap(leftPointer,right);
printf("Updated Array: ");
display();
return leftPointer;
}
void quickSort(int left, int right) {
if(right-left <= 0) {
return;
} else {
int pivot = intArray[right];
int partitionPoint = partition(left, right, pivot);
quickSort(left,partitionPoint-1);
quickSort(partitionPoint+1,right);
}
}
int main() {
printf("Input Array: ");
display();
printline(50);
quickSort(0,MAX-1);
printf("Output Array: ");
display();
printline(50);
}
我尝试过:
这个C程序的一个更简单的版本?
What I have tried:
A simpler version of this C program?
推荐答案
无论你从哪里获得该版本,请将其发回 - 如果作者不管怎么说也不能打算缩进它然后他显然根本不关心他的代码。
你想要一个更小的版本?只需致电 qsort [ ^ ] - 它是实现快速排序的C库函数。一旦你删除辅助支持代码以使用它,你就不会得到比你那里更短的代码版本。
Where ever you got that version from, send it back - if the author can't be bothered to even indent it properly then he clearly doesn't care about his code at all.
And you want a smaller version? Just call qsort[^] - it's a C library function implementing Quick Sort. You won't get a much shorter version of the code than you have there anyway, once you rip out the ancillary support code to use it.
这篇关于这个quicksort C程序有简单的版本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文