forked from DhanushNehru/Hacktoberfest2025
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuick.java
More file actions
58 lines (51 loc) · 1.49 KB
/
Quick.java
File metadata and controls
58 lines (51 loc) · 1.49 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
52
53
54
55
56
57
58
// The following code is of Quick Sort Algorithm in Java.
import java.util.*;
class QuickSort {
static int partition(List<Integer> arr, int low, int high) {
int pivot = arr.get(low);
int i = low;
int j = high;
while (i < j) {
while (arr.get(i) <= pivot && i <= high - 1) {
i++;
}
while (arr.get(j) > pivot && j >= low + 1) {
j--;
}
if (i < j) {
int temp = arr.get(i);
arr.set(i, arr.get(j));
arr.set(j, temp);
}
}
int temp = arr.get(low);
arr.set(low, arr.get(j));
arr.set(j, temp);
return j;
}
static void qs(List<Integer> arr, int low, int high) {
if (low < high) {
int pIndex = partition(arr, low, high);
qs(arr, low, pIndex - 1);
qs(arr, pIndex + 1, high);
}
}
public static List<Integer> quickSort(List<Integer> arr) {
qs(arr, 0, arr.size() - 1);
return arr;
}
}
public class Quick {
public static void main(String args[]) {
List<Integer> arr = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
arr.add(scanner.nextInt());
}
arr = QuickSort.quickSort(arr);
for (int i = 0; i < n; i++) {
System.out.print(arr.get(i) + " ");
}
}
}