forked from pujazha/Hactoberfest2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quick_Sort.java
55 lines (49 loc) · 1.29 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
54
55
public class Quick_sort
{
int partition (int a[], int start, int end)
{
int pivot = a[end]; // pivot member
int i = (start - 1);
for (int j = start; j <= end - 1; j++)
{
// If current member is smaller than the pivot
if (a[j] < pivot)
{
i++; // increment index of smaller element by 1
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
int t = a[i+1];
a[i+1] = a[end];
a[end] = t;
return (i + 1);
}
void quick(int a[], int start, int end)
{
if (start < end)
{
int p = partition(a, start, end); //p is partitioning index
quick(a, start, p - 1);
quick(a, p + 1, end);
}
}
void printArr(int a[], int n)
{
int i;
for (i = 0; i < n; i++)
System.out.print(a[i] + " ");
}
public static void main(String[] args) {
int a[] = { 13, 18, 27, 2, 19, 25 };
int n = a.length;
System.out.println("\nBefore sorting array elements are - ");
Quick_sort q1 = new Quick_sort();
q1.printArr(a, n);
q1.quick(a, 0, n - 1);
System.out.println("\nAfter sorting array elements are - ");
q1.printArr(a, n);
System.out.println();
}
}