You are viewing a single comment's thread. Return to all comments →
I realised going through the discussions for this problem that my approach is a bit different to that being suggested by the others.
This being my first attempt at Quick Sort would appreciate constructive comments
import java.util.Iterator; import java.util.List; import java.util.Scanner; import java.util.Vector;
public class QuickSort2_Sorting {
static List<Integer> QuickSort(List<Integer> arr) { int n=arr.size(); List<Integer> left = new Vector<Integer>(); List<Integer> right = new Vector<Integer>(); int pivot=0; if(n<2) return arr; else { pivot = arr.get(0); Iterator<Integer> i = arr.iterator(); int x =0; while(i.hasNext()){ x = i.next(); if(x > pivot) { right.add(x); } else if( x < pivot) { left.add(x); } } } left = QuickSort(left); right = QuickSort(right); left.add(pivot); left.addAll(right); printArray(left); return left; } static void printArray(List<Integer> ar) { for(int n: ar){ System.out.print(n+" "); } System.out.println(""); } public static void main(String args[]) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); List<Integer> arr = new Vector<Integer>(); for(int i=0;i<n;i++) { arr.add(scan.nextInt()); } scan.close(); QuickSort(arr); }
}
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 →
I realised going through the discussions for this problem that my approach is a bit different to that being suggested by the others.
This being my first attempt at Quick Sort would appreciate constructive comments
import java.util.Iterator; import java.util.List; import java.util.Scanner; import java.util.Vector;
public class QuickSort2_Sorting {
}