-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathQuickSort.java
More file actions
49 lines (44 loc) · 1.21 KB
/
QuickSort.java
File metadata and controls
49 lines (44 loc) · 1.21 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
import java.util.Scanner;
public class QuickSort {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int size;
int a[];
System.out.println("Enter a size of an array : ");
size = sc.nextInt();
a = new int[size];
for (int i = 0; i < size; i++) {
System.out.println("Enter " + (i + 1) + " of an array : ");
a[i] = sc.nextInt();
}
QSort(a, 0, size-1);
System.out.println("Sorted array : ");
for (int i = 0; i < size; i++) {
System.out.println(a[i]);
}
}
public static void QSort(int[] a, int p, int r) {
int j;
if (p < r) {
j = partition(a, p, r);
QSort(a, p, j-1);
QSort(a, j + 1, r);
}
}
public static int partition(int a[], int p, int r) {
int x = a[r];
int i = p - 1, temp;
for (int j = p; j < r ; j++) {
if (a[j] <= x) {
i = i + 1;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
temp = a[i + 1];
a[i + 1] = a[r];
a[r] = temp;
return (i + 1);
}
}