-
Notifications
You must be signed in to change notification settings - Fork 1
/
QuickSort.java
58 lines (49 loc) · 1.71 KB
/
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
52
53
54
55
56
57
58
public class QuickSort {
public static void main(String[] args) {
int[] arr = new int[]{6,4,1,2,3,10,9,8,7,5};
quicksort(arr);
for(int num: arr) {
System.out.print(num + " ");
}
}
public static void quicksort(int[] nums) {
quicksort(nums,0,nums.length-1);
}
public static void quicksort(int[] nums, int lowIndex, int highIndex) {
if(lowIndex >= highIndex) return;
int pivot = nums[highIndex];
int leftPointer = partition(nums,lowIndex,highIndex,pivot);
quicksort(nums,lowIndex, leftPointer -1);
quicksort(nums,leftPointer+1,highIndex);
}
public static int partition(int[] nums, int lowIndex, int highIndex, int pivot) {
int leftPointer = lowIndex;
int rightPointer = highIndex;
while(leftPointer < rightPointer) {
while(nums[leftPointer] <= pivot && leftPointer < rightPointer) {
leftPointer++;
}
while(nums[rightPointer] >= pivot && leftPointer < rightPointer) {
rightPointer--;
}
swap(nums,leftPointer,rightPointer);
}
if(nums[leftPointer] > nums[highIndex]) {
swap(nums, leftPointer, highIndex);
}
else {
leftPointer = highIndex;
}
return leftPointer;
}
public static void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
//Quicksort takes a int low index and int high index and a pivot
//does a while loop to make sure left < right at all times
//increment left if left <= pivot && left < right
//decrement right if right >= pivot && left < right
//swap(