From a02dd41574f0bcd1249f91b7bd65ddfd2cacba4b Mon Sep 17 00:00:00 2001 From: Uday Vig Date: Tue, 2 Oct 2018 20:54:17 +0530 Subject: [PATCH] QuickSort.java Implemented QuickSort in java. This adds QuickSort Algorithm which return the concatenation of the quicksorted list of elements that are less than or equal to the pivot, the pivot, and the quicksorted list of elements that are greater than the pivot. Closes https://github.com/NITSkmOS/Algorithms/issues/150 --- merge_sort/Java/QuickSort.java | 47 ++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 merge_sort/Java/QuickSort.java diff --git a/merge_sort/Java/QuickSort.java b/merge_sort/Java/QuickSort.java new file mode 100644 index 00000000..1ce9ccce --- /dev/null +++ b/merge_sort/Java/QuickSort.java @@ -0,0 +1,47 @@ +package lecture9; + +public class QuickSort { + + public static void main(String[] args) { + // TODO Auto-generated method stub + Scanner s = new Scanner(System.in); + System.out.println("Enter size: "); + int n = s.nextInt(); + int[] arr = new int[n]; + System.out.println("Enter elements: "); + for (int i = 0; i < n; i++) { + arr[i] = s.nextInt(); + } + quicksort(arr,0,arr.length-1); + for(int i=0;i=high){ + return; + } + int left=low; + int right=high; + int mid=(low+high)/2; + int pivot=arr[mid]; + while(left<=right){ + while(arr[left]pivot){ + right--; + } + if(left<=right){ + int temp=arr[left]; + arr[left]=arr[right]; + arr[right]=temp; + left++; + right--; + } + } + quicksort(arr,low,right); + quicksort(arr,left,high); + } + +}