1
votes

So i am doing a challenge located at https://www.hackerrank.com/challenges/quicksort2 and im having trouble wrapping my head around doing this the way they want. They are asking for the program to do a quicksort using subArrays and then put those sub arrays together at the end to come up with the result. Here is the challenge and i will have my code below. needless to say i don't have it working. All the code was provided and the challenge is to write the partition method

Print Sub-Arrays In this challenge, print your array every time your partitioning method finished, i.e. print every sorted sub-array The first element in a sub-array should be used as a pivot. Partition the left side before partitioning the right side. The pivot should not be added to either side. Instead, put it back in the middle when combining the sub-arrays together.

Input Format

There will be two lines of input:

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

Output Format

Print every partitioned sub-array on a new line.

Constraints

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

Sample Input

7
5 8 1 3 7 9 2

Sample Output

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

Code

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

i have to use this format, i can edit the partition method but the rest stays per rules of the challenge

1

1 Answers

0
votes

I haven't checked your code, but this seems to work. It's easier to make it with 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);

    }
}