繁体   English   中英

递归快速排序 java

[英]Recursive Quick Sort in java

这是我的快速排序代码。 它给了我一个错误的答案,但我认为我的分区 function 是正确的。

public class Quick_Sort {

public static void main(String[] args) 
{

    int a[] = {99,88,5,4,3,2,1,0,12,3,7,9,8,3,4,5,7};
        quicksort(a, 0, a.length-1);
}

static int partition(int[] a, int low , int hi)
{
    int pivot = hi;
    int i =low;
    int j = hi-1;
    while(i<j)
    {
        if(a[i]<=a[pivot])
        {
            i++;
        }
        if(a[i]>a[pivot])
        {   
        if((a[i]>a[pivot]) && (a[j]<=a[pivot]))
        {
            int temp= a[i];
            a[i]=a[j];
            a[j]=temp;
            i++;    
        }
        if(a[j]>a[pivot])
        {
            j--;
        }
        }
    }
    int temp= a[i];
    a[i]=a[pivot];
    a[pivot]=temp;
    return i;
}
static void quicksort(int[] a, int low, int hi)
{
    if(low>=hi)
    {
        return;
    }
    int split = partition(a, low, hi);
    quicksort(a, low, split-1);
    quicksort(a, split+1, hi);
}
}

这是最终的 output:

1 0 3 2 3 4 4 5 5 7 3 7 8 9 12 88 99

尝试空运行它,看不到错误

partition方法中,您已将j分配给hi - 1 应该将其设置为仅hi

static int partition(int[] a, int low , int hi)
{
    int pivot = hi;
    int i =low;
//  int j = hi-1; // CHANGE THIS TO 
    int j = hi; // THIS 
    while(i<j)

做出更改后,我得到了以下输出:

[0, 1, 2, 3, 3, 3, 4, 4, 5, 5, 7, 7, 8, 9, 12, 88, 99]

希望这可以帮助!

这是 C# 代码:

 public void RecursiveQuickSort(int[] array, int start, int end)
    {
        if (start < end)
        {
            int pivot = start;
            int left = start + 1;
            int right = end;

            while (true)
            {
                while (array[left] <= array[pivot] && left < right)
                    left++;
                while (array[right] > array[pivot] && left < right)
                    right--;
                if (left < right)
                {
                    Swap(array, left, right);
                }
                else
                {
                    pivot = (array[pivot] > array[left]) ? left : left - 1;
                    Swap(array, start, pivot);
                    RecursiveQuickSort(array, start, pivot - 1);
                    RecursiveQuickSort(array, pivot + 1, end);
                    return;
                }
            }
        }
    }

    private void Swap(int[] array, int index1, int index2)
    {
        int temp = array[index1];
        array[index1] = array[index2];
        array[index2] = temp;
    }

这是一个很好的实现,并且是Java标准。 也许您想在这里看看,显然所有学分归原始作者所有。

http://www.vogella.com/tutorials/JavaAlgorithmsQuicksort/article.html

这就是我制作它的方式 C:

private static List<Integer> recursive_quick_sort (List<Integer> list){
    if (list.size()< 2){
        return list;
    }else {
        int pivot = list.get(0);
        List<Integer> less  = list.stream().filter(element -> element  < pivot ).collect(Collectors.toList());
        List <Integer> greater  = list.stream().filter(element -> element  > pivot ).collect(Collectors.toList());
        List<Integer> newList = Stream.of(recursive_quick_sort(less),List.of(pivot),recursive_quick_sort(greater))
                        .flatMap(Collection::stream)
                        .collect(Collectors.toList());
    return newList;
    }
}

暂无
暂无

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

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