Merge pull request #397 from khongta0932488598/patch-1

QuickSortAlgo.java
This commit is contained in:
Christian Bender
2018-03-30 21:25:24 +02:00
committed by GitHub

View File

@ -15,11 +15,13 @@ class QuickSort {
* Sorts the array in increasing order * Sorts the array in increasing order
**/ **/
public static <T extends Comparable<T>> void QS(T array[], int start, int end) { public static <T extends Comparable<T>> void QS(List<T> list, int left, int right) {
if (start < end) { if (left>=right) { return }
int PIndex = partition(array, start, end); else
QS(array, start, PIndex - 1); {
QS(array, PIndex + 1, end); int pivot = partition(array, left,right);
QS(list, left, pivot- 1);
QS(list, pivot + 1, right);
} }
} }
@ -32,17 +34,32 @@ class QuickSort {
* Finds the partition index of an array * Finds the partition index of an array
**/ **/
public static <T extends Comparable<T>> int partition(T array[], int start, int end) { public static <T extends Comparable<T>> int partition(List<T> list, int left, int right) {
T pivot = array[end]; int mid=(left+right)/2;
int PIndex = start; T pivot=list.get(mid);
for (int i=start;i<end;i++) { swap(list,mid,right);
if (array[i].compareTo(pivot) <= 0) { while(left<right)
swap(array, i, PIndex); {
PIndex++; while(left<right && pivot.compareTo(list.get(left))>=0)
{
++left;
}
if(left<right)
{
swap(list,left,right);
--right;
}
while(left<right && list.get(right).compareTo(pivot)>=0)
{
--right;
}
if(left<right)
{
swap(list,left,right);
++left;
} }
} }
swap(array, PIndex, end); return left;
return PIndex;
} }
/** /**
@ -54,39 +71,36 @@ class QuickSort {
* Swaps initial and fin element * Swaps initial and fin element
**/ **/
public static <T extends Comparable<T>> void swap(T[] array, int initial, int fin) { public static <T extends Comparable<T>> void swap(List<E> list, int initial, int fin) {
T temp = array[initial]; E temp= list.get(initial);
array[initial] = array[fin]; list.set(initial,list.get(fin));
array[fin] = temp; list.set(fin,temp);
} }
// Driver Program // Driver Program
public static void main(String[] args) { public static void main(String[] args) {
// For integer input // For integer input
int[] arr = {3,4,1,32,0,2,44,111,5}; ArrayList<Integer> array = new ArrayList<Integer>(9);
Integer[] array = new Integer[arr.length]; array = {3,4,1,32,0,2,44,111,5};
for (int i=0;i<arr.length;i++) {
array[i] = arr[i];
}
QS(array, 0, arr.length-1); QS(array, 0, array.size()-1);
//Output => 0 1 2 3 4 5 32 44 111 //Output => 0 1 2 3 4 5 32 44 111
for (int i=0;i<array.length;i++) { for (int i=0;i<array.size();i++) {
System.out.print(array[i] + " "); System.out.print(array.get(i) + " ");
} }
System.out.println(); System.out.println();
// String Input ArrayList<String> array1=new ArrayList<String>(5);
String[] array1 = {"c", "a", "e", "b","d"}; array1 = {"c", "a", "e", "b","d"};
QS(array1, 0,array1.length-1); QS(array1, 0,array1.size()-1);
//Output => a b c d e //Output => a b c d e
for(int i=0; i<array1.length; i++) for(int i=0; i<array1.size(); i++)
{ {
System.out.print(array1[i]+"\t"); System.out.print(array1.get(i)+"\t");
} }
} }
} }