简体   繁体   English

如何在Quicksort算法中实现递归?

[英]How can I implement the recursion in my Quicksort algorithm?

I'm trying to implement quicksort in Java to learn basic algorithms. 我正在尝试在Java中实现quicksort,以学习基本算法。 I understand how the algo works (and can do it on paper) but am finding it hard to write it in code. 我了解算法的工作原理(并且可以在纸上完成),但是发现很难用代码编写它。 I've managed to do step where we put all elements smaller than the pivot to the left, and larger ones to the right (see my code below). 我设法完成了以下步骤:将所有元素都比左侧的枢轴小,而将右侧的枢轴大(请参见下面的代码)。 However, I can't figure out how to implement the recursion part of the algo, so sort the left and right sides recursively. 但是,我无法弄清楚如何实现算法的递归部分,因此需要对左​​侧和右侧进行递归排序。 Any help please? 有什么帮助吗?

public void int(A, p, q){
    if(A.length == 0){ return; }
    int pivot = A[q];
    j = 0; k = 0;
    for(int i = 0; i < A.length; i++){
        if(A[i] <= pivot){
            A[j] = A[i]; j++;
        }
        else{
            A[k] = A[i]; k++;
        }
    }
    A[j] = pivot;
}

Big Disclaimer : I did not write this piece of code, so upvotes is not needed. 大免责声明 :我没有编写这段代码,因此不需要投票。 But I link to a tutorial which explains quicksort in detail. 但我链接到详细解释quicksort的教程。 Gave me a much needed refreshment on the algorithm as well! 也为我提供了急需的算法更新! The example given has very good comments that might just help you to wrap your head around it. 给出的示例有很好的注释,也许可以帮助您绕开它。

I suggest you adapt it to your code and write som tests for it to verify it works 我建议您将其适应您的代码并为其编写som测试以验证其是否有效

Quicksort is a fast, recursive, non-stable sort algorithm which works by the divide and conquer principle. Quicksort是一种快速的,递归的,非稳定的排序算法,它按照分而治之的原理工作。 Quicksort will in the best case divide the array into almost two identical parts. 在最佳情况下,Quicksort会将阵列分为几乎两个相同的部分。 It the array contains n elements then the first run will need O(n). 如果数组包含n个元素,则第一次运行将需要O(n)。 Sorting the remaining two sub-arrays takes 2 O(n/2). 排序其余的两个子数组需要2 O(n / 2)。 This ends up in a performance of O(n log n). 最终得到O(n log n)的性能。 In the worst case quicksort selects only one element in each iteration. 在最坏的情况下,快速排序在每次迭代中仅选择一个元素。 So it is O(n) + O(n-1) + (On-2).. O(1) which is equal to O(n^2).* 因此,它是O(n)+ O(n-1)+(On-2).. O(1)等于O(n ^ 2)。*

public class Quicksort  {
private int[] numbers;
private int number;

public void sort(int[] values) {
    // check for empty or null array
    if (values ==null || values.length==0){
        return;
    }
    this.numbers = values;
    number = values.length;
    quicksort(0, number - 1);
}

private void quicksort(int low, int high) {
    int i = low, j = high;
    // Get the pivot element from the middle of the list
    int pivot = numbers[low + (high-low)/2];

    // Divide into two lists
    while (i <= j) {
        // If the current value from the left list is smaller than the pivot
        // element then get the next element from the left list
        while (numbers[i] < pivot) {
            i++;
        }
        // If the current value from the right list is larger than the pivot
        // element then get the next element from the right list
        while (numbers[j] > pivot) {
            j--;
        }

        // If we have found a value in the left list which is larger than
        // the pivot element and if we have found a value in the right list
        // which is smaller than the pivot element then we exchange the
        // values.
        // As we are done we can increase i and j
        if (i <= j) {
            exchange(i, j);
            i++;
            j--;
        }
    }
    // This is the recursion part you had trouble with i guess?
    // Recursion
    if (low < j)
        quicksort(low, j);
    if (i < high)
        quicksort(i, high);
}

private void exchange(int i, int j) {
    int temp = numbers[i];
    numbers[i] = numbers[j];
    numbers[j] = temp;
}
}

Link to tutorial 链接到教程

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

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