-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quick_Sort.java
42 lines (35 loc) · 1011 Bytes
/
Quick_Sort.java
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
import java.util.Arrays;
class Quick_Sort {
static int partition(int array[], int low, int high) {
int pivot = array[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (array[j] <= pivot) {
i++;
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
int temp = array[i + 1];
array[i + 1] = array[high];
array[high] = temp;
return (i + 1);
}
static void quick_Sort(int array[], int low, int high) {
if (low < high) {
int pi = partition(array, low, high);
quick_Sort(array, low, pi - 1);
quick_Sort(array, pi + 1, high);
}
}
}
class Main {
public static void main(String args[]) {
int[] data = { 10, 15, 2, 55, 3, 9, 23, 87, 34 };
System.out.printf("Input Array : " + Arrays.toString(data) + "\n\n");
int size = data.length;
Quick_Sort.quick_Sort(data, 0, size - 1);
System.out.println("Quick Sorted Array : " + Arrays.toString(data) + "\n");
}
}