Skip to content

Commit 24fc689

Browse files
authored
Create Quick Sort "JAVA"
0 parents  commit 24fc689

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

Quick Sort "JAVA"

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
class Main
2+
{
3+
4+
// PARTITION FUNCTION STARTS
5+
6+
int partition(int A[], int low, int high)
7+
{
8+
int p = A[high];
9+
int i = (low-1);
10+
for (int j=low; j<high; j++)
11+
{
12+
if (A[j] <= p)
13+
{
14+
i++;
15+
int t = A[i];
16+
A[i] = A[j];
17+
A[j] = t;
18+
}
19+
}
20+
int t = A[i+1];
21+
A[i+1] = A[high];
22+
A[high] = t;
23+
return i+1;
24+
}
25+
26+
// PARTITION FUNCTION ENDS
27+
28+
void sort(int A[], int low, int high)
29+
{
30+
if (low < high)
31+
{
32+
int pi = partition(A, low, high);
33+
sort(A, low, pi-1);
34+
sort(A, pi+1, high);
35+
}
36+
}
37+
static void printArray(int A[])
38+
{
39+
int n = A.length;
40+
for (int i=0; i<n; ++i)
41+
System.out.print(A[i]+" ");
42+
System.out.println();
43+
}
44+
public static void main(String args[])
45+
{
46+
int A[] = {9,1,4,0,6,3,5,2,7,8};
47+
int n = A.length;
48+
Main aj = new Main();
49+
aj.sort(A, 0, n-1);
50+
System.out.println("Sorted array :");
51+
printArray(A);
52+
}
53+
}
54+

0 commit comments

Comments
 (0)