-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
49 lines (49 loc) · 1.43 KB
/
Copy pathQuickSort.java
File metadata and controls
49 lines (49 loc) · 1.43 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
package OOPJava;
import java.util.Scanner;
public class QuickSort {
public static void swap(int[] A, int a, int b) {
int t = A[a];
A[a] = A[b];
A[b] = t;
}
public static int partition(int[] A, int low, int high) {
int pivot = A[high];
int i = low - 1;
for (int j = low; j <= high - 1; j++) {
if (A[j] < pivot) {
i++;
swap(A, i, j);
}
}
swap(A, i + 1, high);
return i + 1;
}
public static void quickSort(int[] A, int low, int high) {
if (low < high) {
int pivot = partition(A, low, high);
quickSort(A, low, pivot - 1);
quickSort(A, pivot + 1, high);
}
}
public static void printArray(int[] A) {
for (int i = 0; i < A.length; i++) {
System.out.print(A[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of elements: ");
int x = scanner.nextInt();
int[] A = new int[x];
System.out.println("Enter your elements: ");
for (int i = 0; i < x; i++) {
A[i] = scanner.nextInt();
}
System.out.println("Original array: ");
printArray(A);
quickSort(A, 0, x - 1);
System.out.println("Sorted array: ");
printArray(A);
}
}