forked from PrajaktaSathe/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
45 lines (39 loc) · 1.06 KB
/
QuickSort.java
File metadata and controls
45 lines (39 loc) · 1.06 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
40
41
42
43
44
45
//Quick Sort
import java.util.Arrays;
public class QuickSort {
public static void main(String args[]) {
int arr[] = {-1, -2, 42, 1, 24, 44, 32, 0, 12, 100};
quickSort(arr, 0, arr.length - 1);
System.out.println(Arrays.toString(arr));
}
public static void quickSort(int arr[], int i, int j) {
int pivot = i;
if (i < j) {
sort(arr, i, j, pivot);
quickSort(arr, i, pivot);
quickSort(arr, pivot + 1, j);
}
}
public static int[] sort(int arr[], int i, int j, int pivot) {
if (j <= pivot || i >= arr.length) {
return arr;
}
while (arr[i] < arr[pivot]) {
i++;
}
while (arr[j] > arr[pivot]) {
j--;
}
if (i < j) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
return sort(arr, i, j, pivot);
} else {
int tmp = arr[pivot];
arr[pivot] = arr[j];
arr[j] = tmp;
return arr;
}
}
}