-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSortDemo.java
43 lines (34 loc) · 1.09 KB
/
QuickSortDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.util.Arrays;
public class QuickSortDemo {
public static void main(String[] args) {
int[] nums={17,13,2,6,8,1,-19,45,67,42,3,2,0};
System.out.println("Input Array before sorting : "+ Arrays.toString(nums));
int sorted[]=doQuickSort(nums,0,nums.length-1);
System.out.println("Input Array After sorting using QuickSort Algorithm :"+Arrays.toString(sorted));
}
private static int[] doQuickSort(int[] nums, int low, int high) {
if (low<high) {
int p=partition(nums,low,high);
doQuickSort(nums,low,p-1);
doQuickSort(nums,p+1,high);
}
return nums;
}
private static int partition(int[] nums, int low, int high) {
int last=nums[high];
int i=low-1;
int j;
for (j = low; j <high ; j++) {
if(nums[j]<=last){
i++;
int temp=nums[j];
nums[j]=nums[i];
nums[i]=temp;
}
}
int temp=nums[j];
nums[j]=nums[i+1];
nums[i+1]=temp;
return i+1;
}
}