Skip to content

Commit 8688ece

Browse files
committed
quick sort algorithm implementation
1 parent 6c3c02c commit 8688ece

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package Lecture14;
2+
3+
public class quickSort {
4+
5+
public static void main(String[] args) {
6+
7+
int[] arr = { 7, 2, 1, 6, 8, 5, 3, 4 };
8+
9+
int start = 0, end = arr.length - 1;
10+
quickSorting(arr, start, end);
11+
display(arr);
12+
}
13+
14+
public static void quickSorting(int[] arr, int startIndex, int endIndex) {
15+
if (startIndex >= endIndex) {
16+
return;
17+
}
18+
int pindex = partition(arr, startIndex, endIndex);
19+
quickSorting(arr, startIndex, pindex - 1);
20+
quickSorting(arr, pindex + 1, endIndex);
21+
}
22+
23+
public static int partition(int[] arr, int si, int ei) {
24+
int pivot = arr[ei];
25+
int pindex = si;
26+
27+
for (int index = si; index <= ei - 1; index++) {
28+
if (arr[index] <= pivot) {
29+
swap(arr, index, pindex);
30+
pindex++;
31+
}
32+
}
33+
swap(arr, pindex, ei);
34+
return pindex;
35+
}
36+
37+
public static void swap(int[] arr, int i, int j) {
38+
int temp = arr[i];
39+
arr[i] = arr[j];
40+
arr[j] = temp;
41+
}
42+
43+
public static void display(int[] arr) {
44+
for (int index = 0; index < arr.length; index++) {
45+
System.out.print(arr[index] + " ");
46+
}
47+
System.out.println();
48+
}
49+
}

0 commit comments

Comments
 (0)