- Notifications
You must be signed in to change notification settings - Fork 363
Expand file tree
/
Copy pathQuickSort.java
More file actions
Latest commit
59 lines (53 loc) · 1.52 KB
/
Copy pathQuickSort.java
File metadata and controls
59 lines (53 loc) · 1.52 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
59
importjava.util.Scanner;
publicclassQuickSort {
publicstaticvoidswap(int[] arr, intx, inty) {
inttemp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
publicstaticintpartition(int[] arr, intlow, inthigh) {
intpivot = arr[high];
inti = low - 1;
for (intj = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
swap(arr, i, j);
}
}
i++;
swap(arr, high, i);
returni;
}
voidquickSort(int[] arr, intlow, inthigh) {
if (low < high) {
intpivot;
pivot = partition(arr, low, high);
quickSort(arr, low, pivot - 1);
quickSort(arr, pivot + 1, high);
}
}
voidprtArr(int[] arr, intn) {
for (inti : arr) {
System.out.print(i + " ");
}
System.out.println("");
}
publicstaticvoidmain(String[] args) {
QuickSortqs = newQuickSort();
Scannersc = newScanner(System.in);
intn;
System.out.print("Enter the size of the array : ");
n = sc.nextInt();
int[] arr = newint[n];
System.out.print("Enter the Elements of the array : ");
for (inti = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
// Printing The Array Before Sorting
qs.prtArr(arr, n);
qs.quickSort(arr, 0, n - 1);
// Printing The Array After Sorting
qs.prtArr(arr, n);
sc.close();
}
}