-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
39 lines (33 loc) · 1.04 KB
/
Copy pathQuickSort.java
File metadata and controls
39 lines (33 loc) · 1.04 KB
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
import java.util.Arrays;
public class QuickSort{
public static void swap(int[] arr, int i, int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void quickSort(int[] arr, int low, int high) {
if(low < high){
int pivot = arr[low];
int i = high+1;
for(int j = high; j > low; j--){
if(arr[j] >= pivot){
i--;
swap(arr, i, j);
}
}
swap(arr, i-1, low);
int pivotIndex = i-1;
quickSort(arr, low, pivotIndex-1);
quickSort(arr, pivotIndex+1, high);
}
}
public static void main(String[] args) {
int[] arr = {3, 69, 45, 88, 24, 1, 5};
System.out.println("Original array: ");
System.out.print(Arrays.toString(arr));
System.out.println();
quickSort(arr, 0, arr.length - 1);
System.out.println("Sorted array: ");
System.out.print(Arrays.toString(arr));
}
}