We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Thank You Man. I don't know how to do quick sort, I learnt it from your code. Instead of creating number of methods, u can write it in one method itself. Best part in ur code is the print array statement. I'm a bit confused to print the answer. Modifing the same code of urs as:
public class Solution {
static void quickSort(int ar[], int minpos, int maxpos){
if(minpos >= maxpos)
return;
int p = ar[minpos];
ArrayList<Integer> leftlist = new ArrayList<Integer>();
ArrayList<Integer> rightlist = new ArrayList<Integer>();
for(int i = minpos+1 ; i <= maxpos; i++)
{
if(ar[i] > p)
rightlist.add(ar[i]);
else
leftlist.add(ar[i]);
}
int ppos=minpos;
for(int num : leftlist)
ar[ppos++] = num;
ar[ppos++] = p;
for(int num : rightlist)
ar[ppos++] = num;
quickSort(ar,minpos,minpos + leftlist.size()-1);
quickSort(ar,minpos + leftlist.size()+1,maxpos);
printArray(ar,minpos, maxpos);
}
static void printArray(int[] ar,int start, int end) {
for(int i = start; i <= end;i++)
System.out.print(ar[i]+" ");
System.out.println("");
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] ar = new int[n];
for(int i=0;i<n;i++){
ar[i]=in.nextInt();
}
quickSort(ar,0,n-1);
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Quicksort 2 - Sorting
You are viewing a single comment's thread. Return to all comments →
Thank You Man. I don't know how to do quick sort, I learnt it from your code. Instead of creating number of methods, u can write it in one method itself. Best part in ur code is the print array statement. I'm a bit confused to print the answer. Modifing the same code of urs as:
}
}