-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQuickSort.java
More file actions
59 lines (53 loc) · 1.21 KB
/
QuickSort.java
File metadata and controls
59 lines (53 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
50
51
52
53
54
55
56
57
58
59
package sorting;
public class QuickSort {
public static void main(String[] args) {
int i;
int[] arr = { 90, 23, 101, 45, 65, 23, 67, 89, 34, 23 };
System.out.println("The initial array is:");
for (i = 0; i < 10; i++)
System.out.print(arr[i] + " ");
quickSort(arr, 0, 9);
System.out.println("\n\nThe sorted array is:");
for (i = 0; i < 10; i++)
System.out.print(arr[i] + " ");
}
public static int partition(int a[], int beg, int end) {
int left, right, temp, loc, flag;
loc = left = beg;
right = end;
flag = 0;
while (flag != 1) {
while ((a[loc] <= a[right]) && (loc != right))
right--;
if (loc == right)
flag = 1;
else if (a[loc] > a[right]) {
temp = a[loc];
a[loc] = a[right];
a[right] = temp;
loc = right;
}
if (flag != 1) {
while ((a[loc] >= a[left]) && (loc != left))
left++;
if (loc == left)
flag = 1;
else if (a[loc] < a[left]) {
temp = a[loc];
a[loc] = a[left];
a[left] = temp;
loc = left;
}
}
}
return loc;
}
static void quickSort(int a[], int beg, int end) {
int loc;
if (beg < end) {
loc = partition(a, beg, end);
quickSort(a, beg, loc - 1);
quickSort(a, loc + 1, end);
}
}
}