forked from adarshpandey10t/Hacktoberfestmine
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
Latest commit
54 lines (50 loc) · 1.25 KB
/
Copy pathQuickSort.java
File metadata and controls
54 lines (50 loc) · 1.25 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
51
52
53
54
publicclassQuickSort
{
intpartition (inta[], intstart, intend)
{
intpivot = a[end]; // pivot element
inti = (start - 1);
for (intj = start; j <= end - 1; j++)
{
if (a[j] < pivot)
{
i++; // increment index
intt = a[i];
a[i] = a[j];
a[j] = t;
}
}
intt = a[i+1];
a[i+1] = a[end];
a[end] = t;
return (i + 1);
}
/* function quick sort */
voidquick(inta[], intstart, intend)
{
if (start < end)
{
intp = partition(a, start, end); // partitioning index
quick(a, start, p - 1);
quick(a, p + 1, end);
}
}
/* print an array */
voidprintArr(inta[], intn)
{
inti;
for (i = 0; i < n; i++)
System.out.print(a[i] + " ");
}
publicstaticvoidmain(String[] args) {
inta[] = { 13, 18, 27, 2, 19, 25 };
intn = a.length;
System.out.println("\nBefore sorting array elements are - ");
QuickSortq1 = newQuickSort();
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();
}
}