-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathQuickSort.java
More file actions
36 lines (31 loc) · 980 Bytes
/
QuickSort.java
File metadata and controls
36 lines (31 loc) · 980 Bytes
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
public class QuickSort {
public static int partition(int array[], int low , int high) {
int pivot = array[high];
int ptr = low-1; // left
for(int k =low;k<high;k++) {
if(pivot>array[k] ) {
ptr++;
int temp = array[ptr];
array[ptr]= array[k];
array[k]=temp;
}
}
ptr++;
int temp =array[ptr];
array[ptr]= array[high];
array[high]=temp;
return ptr;
}
public static void quickSort(int array[], int low, int high ) {
if(low<high) {
int partIndex = partition(array , low ,high);
quickSort(array ,low , partIndex-1); // left array recursion
quickSort(array, partIndex+1 , high);
}
}
public static void main(String[] args) {
// Quick Sort
int array []= {45,13,23,9,54,23,67,4,22};
quickSort(array ,0 , array.length-1);
}
}