-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuick_Sort.java
More file actions
50 lines (41 loc) · 1.02 KB
/
Quick_Sort.java
File metadata and controls
50 lines (41 loc) · 1.02 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
public class Quick_Sort {
static void quicksort(int a[], int low, int high) {
//System.out.println(low+" "+high);
if (low < high) {
int pivot = partition(a, low, high);
quicksort(a, low, pivot - 1);
quicksort(a, pivot + 1, high);
}
}
static int partition(int a[], int low, int high) {
int pivot = a[low];
int i = low;
int j = high;
while (i<j) {
while (a[i] <= pivot && i<=high)
i++;
while (a[j] > pivot && j>=low)
j--;
if (i<j)
swap(a,i,j);
}
return j;
}
static void swap(int a[], int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static void printArray(int a[], int n) {
for (int i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
}
public static void main(String[] args) {
int[] arr = { 10, 7, 8, 9, 1, 5 };
int n = arr.length - 1;
quicksort(arr, 0, n);
System.out.println("Sorted array: ");
printArray(arr, n);
}
}