Skip to content

Commit

Permalink
QuickSort.java Implemented QuickSort in java.
Browse files Browse the repository at this point in the history
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 NITSkmOS#150
  • Loading branch information
udayvig committed Oct 2, 2018
1 parent 6a7e79e commit a02dd41
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions merge_sort/Java/QuickSort.java
Original file line number Diff line number Diff line change
@@ -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<arr.length;i++){
System.out.print(arr[i]+" ");
}
}
public static void quicksort(int arr[],int low, int high){
if(low>=high){
return;
}
int left=low;
int right=high;
int mid=(low+high)/2;
int pivot=arr[mid];
while(left<=right){
while(arr[left]<pivot){
left++;
}
while(arr[right]>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);
}

}

0 comments on commit a02dd41

Please sign in to comment.