遇到快速排序方法的问题

时间:2014-03-14 07:01:56

标签: java arrays quicksort

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

打印子阵列 在此挑战中,每次分区方法完成时打印数组,即打印每个已排序的子数组子数组中的第一个元素应用作数据透视表。在分割右侧之前对左侧进行分区。枢轴不应添加到任何一侧。相反,将子阵列组合在一起时将其放回中间。

输入格式

将有两行输入:

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);
}    
}

我必须使用这种格式,我可以编辑分区方法,但其余的按照挑战规则

1 个答案:

答案 0 :(得分:0)

我没有检查过您的代码,但这似乎有效。使用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);

    }
}