-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickShort.java
More file actions
51 lines (41 loc) · 1.23 KB
/
quickShort.java
File metadata and controls
51 lines (41 loc) · 1.23 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
46
47
48
49
50
51
public class quickShort {
public static void Quickshort(int[] arr, int low, int high) {
if (low < high) {
int pivot = partition(arr, low, high);
Quickshort(arr, low, pivot - 1);
Quickshort(arr, pivot + 1, high);
}
}
public static int partition(int[] arr, int low, int high) {
int key = arr[low];
int i = low;
int j = low + 1;
while (j <= high) {
// If the current element is less than the pivot
if (arr[j] < key) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
j++;
}
// Place the pivot at its correct position
arr[low] = arr[i];
arr[i] = key;
return i;
}
public static void main(String[] args) {
int arr[] = {48, 40, 50, 30, 10, 26, 2};
System.out.print("Before sorting: ");
for (int num : arr) {
System.out.print(num + " ");
}
Quickshort(arr, 0, arr.length - 1);
System.out.println(" ");
System.out.print("After sorting: ");
for (int num : arr) {
System.out.print(num + " ");
}
}
}