Skip to content

Commit 7f24299

Browse files
committed
Quicksort in Java Added
1 parent a6fdb2c commit 7f24299

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

classical_algorithms/QuickSort.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
public class QuickSort{
2+
3+
public static int partition(int[] arr, int low, int high){
4+
int pivot = arr[high]; // taking pivot at rightmost pos
5+
int start_ptr = low - 1;
6+
for(int i = low; i < high; i++){
7+
if(arr[i] < pivot){
8+
start_ptr++;
9+
10+
int tmp = arr[i];
11+
arr[i] = arr[start_ptr];
12+
arr[start_ptr] = tmp;
13+
}
14+
}
15+
16+
// swaping the last element i.e. "pivot"
17+
int temp = arr[high];
18+
arr[high] = arr[start_ptr + 1];
19+
arr[start_ptr + 1] = temp;
20+
21+
return start_ptr + 1;
22+
}
23+
24+
public static void sort(int[] arr, int low, int high){
25+
if(low < high){
26+
int pi_index = partition(arr, low,high);
27+
28+
partition(arr, low, pi_index - 1);
29+
partition(arr, pi_index + 1, high);
30+
}
31+
}
32+
33+
}

0 commit comments

Comments
 (0)