java - 快速排序方法有问题

标签 java arrays quicksort

所以我正在做一个位于 https://www.hackerrank.com/challenges/quicksort2 的挑战我很难按照他们想要的方式去做这件事。他们要求程序使用子数组进行快速排序,然后将这些子数组放在最后得出结果。这是挑战,下面是我的代码。不用说我没有它工作。提供了所有代码,挑战是编写分区方法

打印子数组 在此挑战中,每次分区方法完成时打印您的数组,即打印每个排序的子数组子数组中的第一个元素应用作主元。先划分左侧,再划分右侧。枢轴不应添加到任何一侧。相反,当将子数组组合在一起时,将其放回中间。

输入格式

将有两行输入:

n - the size of the array
ar - the n numbers of the array

输出格式

在新行上打印每个分区子数组。

约束

1<=n<=1000 
-1000<=x<= 1000 , x ∈ ar
There are no duplicate numbers.

示例输入

7
5 8 1 3 7 9 2

示例输出

2 3
1 2 3 
7 8 9
1 2 3 5 7 8 9

代码

import java.util.*;

public class Solution {
static int[] result;   
static void partition(int[] ar) {
int p = 0;
    int s = 0;
    int l = 0;
    int small[] = new int[ar.length];
    int pivot[] = new int[ar.length];
    int large[] = new int[ar.length];
    for(int i = 0; i < ar.length; i++){
        if(i == 0 || ar[i]==pivot[0]){
            pivot[p] = ar[i];
            p++;
        }
        else if(ar[i]>pivot[0]){
            large[l] = ar[i];
            l++;
        }
        else if(ar[i]<pivot[0]){
            small[s] = ar[i];
            s++;
        }
    }
    if(s>2){
        int[] smallA = new int[s];
        for(int i = 0; i < s; i ++){
            smallA[i] = small[i];
        }
        partition(smallA);
    }
    if(l>2){
        int[] largeA = new int[l];
        for(int i = 0; i < l; i ++){
            largeA[i] = large[i];
        }
        partition(largeA);
    }

}   

static void printArray(int[] ar) {
    for(int n: ar){
        System.out.print(n+" ");
    }
    System.out.println("");
}

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    int[] ar = new int[n];
    result = new int[n];
    for(int i=0;i<n;i++){
       ar[i]=in.nextInt(); 
    }
    partition(ar);
}    
}

我必须使用这种格式,我可以编辑分区方法,但其余部分保留根据挑战规则

最佳答案

我还没有检查你的代码,但这似乎有效。用List制作更简单

import java.util.*;

public class quickSorter
{

    public quickSorter()
    {
    }

    public List<Integer> partition(List<Integer> list){

        int pivot = list.get(0);
        List<Integer> result;
        List<Integer> leftSide = new ArrayList<>();
        List<Integer> rightSide = new ArrayList<>();

        for (int i=0;i<list.size();i++){
            if (list.get(i)<pivot){

                leftSide.add(list.get(i));
            }
            else if (list.get(i)>pivot){
                rightSide.add(list.get(i));
            }

        }

        if (leftSide.size()>1){
             result = this.partition(leftSide);
             leftSide=result;
        }
        if (rightSide.size()>1){
             result = this.partition(rightSide);
             rightSide=result;
        }
        List<Integer> combined = new ArrayList<>();
        combined.addAll(leftSide);
        combined.add(pivot);
        combined.addAll(rightSide);
        //print out
        for (int j:combined){
               System.out.print(j+" ");
        }
        System.out.println();



        return combined;
    }


    public static void main(String[] a){

           quickSorter qs = new quickSorter();

           List<Integer> list = new ArrayList<>();

           System.out.println();
           Scanner in = new Scanner(System.in);
           int n = in.nextInt();

           for(int i=0;i<n;i++){
               list.add(in.nextInt()); 
            }
           System.out.println();
           List<Integer> sorted = qs.partition(list);

    }
}

关于java - 快速排序方法有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22398226/

相关文章:

java - 如何使用 Mongodb java 获取文档数组?

java - 可滚动 JTextField 的大小问题

javascript - 如何根据动态变量将元素插入数组

arrays - z3py:如何在 z3py 中表示整数或字符数组

c++ QuickSort不清楚

java - 等待 Executor 中的所有线程完成?

java - 在 Maven 中设置 selenium-java 依赖项,但 Selenium 要求提供驱动程序的路径

java - 带有 CONCAT 的 NamedQuery 返回错误

c# - 使用 LINQ 在 C# 中查找数字数组的累加和

c++ - 无法理解快速选择算法