-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.java
51 lines (45 loc) · 1007 Bytes
/
QuickSort.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
44
45
46
47
48
49
50
51
package sort;
public class QuickSort {
public static void main(String args[]){
int[] nums = {3,2,4,5,8,1,1,2};
int low = 0;
int high = nums.length -1;
quickSort(nums,low,high);
System.out.println("Quick Sort");
for(int i = 0; i <= high; i++){
System.out.print(nums[i]);
System.out.print(";");
}
}
public static void quickSort(int arr[], int low, int high) {
int index = partition(arr, low, high);
if (low < index - 1)
quickSort(arr, low, index - 1);
if (index < high)
quickSort(arr, index, high);
}
public static int partition(int[] nums, int low, int high){
int pivot = nums[low+(high-low)/2];
int i = low;
int j = high;
while(i <= j){
while(nums[i] < pivot){
i++;
}
while(nums[j] > pivot){
j--;
}
if(i <= j){
swap(nums,i,j);
i++;
j--;
}
}
return i;
}
public static void swap(int[] nums, int i, int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}