-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuick Sort "JAVA"
54 lines (49 loc) · 1.17 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
class Main
{
// PARTITION FUNCTION STARTS
int partition(int A[], int low, int high)
{
int p = A[high];
int i = (low-1);
for (int j=low; j<high; j++)
{
if (A[j] <= p)
{
i++;
int t = A[i];
A[i] = A[j];
A[j] = t;
}
}
int t = A[i+1];
A[i+1] = A[high];
A[high] = t;
return i+1;
}
// PARTITION FUNCTION ENDS
void sort(int A[], int low, int high)
{
if (low < high)
{
int pi = partition(A, low, high);
sort(A, low, pi-1);
sort(A, pi+1, high);
}
}
static void printArray(int A[])
{
int n = A.length;
for (int i=0; i<n; ++i)
System.out.print(A[i]+" ");
System.out.println();
}
public static void main(String args[])
{
int A[] = {9,1,4,0,6,3,5,2,7,8};
int n = A.length;
Main aj = new Main();
aj.sort(A, 0, n-1);
System.out.println("Sorted array :");
printArray(A);
}
}