混合快速排序&x2B;C语言中的插入排序
我被要求做两个版本的混合快速排序混合快速排序&x2B;C语言中的插入排序,c,C,我被要求做两个版本的混合快速排序 当元素小于10时,调用插入排序对每个子问题进行排序 当元素小于10时,停止快速排序,并对我当时得到的整个未排序数组使用插入排序 这是第一个练习的快速排序代码。我似乎找不到它们之间的编码差异。我知道这是一个很小的区别。我试了很多 void quickSort(int array[], int start, int finish){ int pivot; if ( low < high ){ pivot = partition(
void quickSort(int array[], int start, int finish){
int pivot;
if ( low < high ){
pivot = partition(A, start, finish);
if ((high - low) < 10){
insertionSort(A, start, finish);
}
quickSort(A, low, pivot - 1);
quickSort(A, pivot + 1, high);
}
}
void快速排序(int数组[],int开始,int结束){
int轴;
如果(低<高){
枢轴=分区(A、开始、结束);
如果((高-低)<10){
插入排序(A、开始、结束);
}
快速排序(A,低位,枢轴-1);
快速排序(A,轴+1,高);
}
}
第一个选项是在递归快速排序功能中插入对小分区进行排序:
void quickSort(int *A, int low, int high) {
if ((high - low) < 10) { // insertionSort small partitions
insertionSort(A, low, high);
return;
}
if (low < high) {
int pivot = partition(A, low, high);
quickSort(A, low, pivot - 1);
quickSort(A, pivot + 1, high);
}
}
请注意,我还没有测试这段代码,但它显示了总体思路
void quickSort_helper(int *A, int low, int high) {
if ((high - low) > 10) { // ignore small partitions
int pivot = partition(A, low, high);
quickSort_helper(A, low, pivot - 1);
quickSort_helper(A, pivot + 1, high);
}
}
void quickSort(int *A, int low, int high) {
quickSort_helper(A, low, high);
insertionSort(A, low, high);
}