繁体   English   中英

Java-如何实现混合的QuickSort和Inserts排序方法

[英]Java - How to implement a hybrid QuickSort and Insertion sort method

当分区大小低于某个阈值(使用下面的10)时,尝试在quickSort时使用插入排序对混合的quickSort进行编码。 我似乎无法正常工作。 数组将总是返回一些乱序的数字。

这是我的部分代码:

public static void quickSort(int[] list) {
quickSort(list, 0, list.length - 1);
}  

private static void quickSort(int[] list, int first, int last) { 
 int size = (last+1) - first;
 if (size <= 10){ // insertion sort if 10 or smaller
   insertionSort(list, first, size);
 }
 else // quicksort if large
 {
   int pivotIndex = partition(list, first, last);
   quickSort(list, first, pivotIndex - 1);
   quickSort(list, pivotIndex + 1, last);
 }
}

public static void insertionSort(int[] list, int first, int size) {
 for (int i = first+1; i < size; i++) {
   int currentElement = list[i];
   int k;
   for (k = i - 1; k >= 0 && list[k] > currentElement; k--) {
     list[k + 1] = list[k];
   }

   // Insert the current element into list[k+1]
   list[k + 1] = currentElement;
 }
}

预期输出:以升序排列的随机数组。

样本输出包含错误:9 18 34 36 53 61 87 89 117 115 109 120 129 154 163 136 131 164 175 193 206 182 182 243 243 181 165 216 261 274 276 281 320 338 341 322 322 379 372 382 392 397 419 401 401 479 479 508 512 494 518 558 578 588 606 660 657 665 617 674 698 728 683 692 684 685 737 738 741 745 753 777 799 816 824 791 791 823 823 762 761 825 845 833 854 854 860 934 886 933 938 880 890 989 935 939 970 948 953945968977995

问题是当您调用插入排序时

 int size = (last+1) - first;
 if (size <= 10){ // insertion sort if 10 or smaller
   insertionSort(list, first, size);
 }

假设在某个位置上first = 5,last = 7,所以size =2。您最终将调用insertSort(list,5,2)

因此,在您的insertSort()方法中,初始的for循环将如下所示:

for (int i = 5+1; i < 2; i++) {

什么时候应该看起来像:

for (int i = 5+1; i < 7; i++) {

我没有对其进行测试,但是看起来这就是问题所在。

通过更改以下内容使其生效:

 private static void quickSort(int[] list, int first, int last) {

 int size = (last +1) - first;

 if (first < last){
     if (last < 11){ // insertion sort if 10 or smaller
        insertionSort(list, first, size);
     }
     else // quicksort if large
     {
     int pivotIndex = partition(list, first, last);
     quickSort(list, first, pivotIndex - 1);
     quickSort(list, pivotIndex + 1, last);
     }
 }    
}

我没有评论,因为我还没有50名代表。 我认为您的解决方案不会像您认为的那样起作用。 您将最终仅对前10个元素运行插入排序。

返回您的初始代码,并将您的调用更改为insertSort(list,first,size); 到:insertSort(list,first,last);

好吧,我做了这项工作...

第一种更改方法quickSort:

private static void quickSort(int[] list, int first, int last) { 
        int size = (last+1) - first;
        if (first < last){
            if (size <= 10){
                insertionSort(list, first, last); //Changed this line
            }
            else{
                int pivotIndex = partition(list, first, last);
                quickSort(list, first, pivotIndex); //Changed this line just because i used the partition method from Hoare and not Lomuto
                quickSort(list, pivotIndex + 1, last);
            }
        }
    }

然后在方法insertSort中:

public static void insertionSort(int[] list, int first, int last) {
        for (int i = first+1; i <= last; i++) { // Change i <= last 
            int currentElement = list[i];
            int j = i-1;
            while (j>=0 && list[j]>currentElement) {
                list[j+1] = list[j];
                j--;
            }
            list[j+1] = currentElement;
        }
    }

希望将是您想要的答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM