forked from sanketpatil02/Code-Overflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreate QuickSort
More file actions
54 lines (44 loc) · 1.39 KB
/
Create QuickSort
File metadata and controls
54 lines (44 loc) · 1.39 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
// Quick sort in Java
import java.util.Arrays;
class QuickSort {
// Function to partition the array on the basis of pivot element
int partition(int array[], int low, int high) {
// Select the pivot element
int pivot = array[high];
int i = (low - 1);
// Put the elements smaller than pivot on the left and
// greater than pivot on the right of pivot
for (int j = low; j < high; j++) {
if (array[j] <= pivot) {
i++;
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
int temp = array[i + 1];
array[i + 1] = array[high];
array[high] = temp;
return (i + 1);
}
void quickSort(int array[], int low, int high) {
if (low < high) {
// Select pivot position and put all the elements smaller
// than pivot on left and greater than pivot on right
int pi = partition(array, low, high);
// Sort the elements on the left of pivot
quickSort(array, low, pi - 1);
// Sort the elements on the right of pivot
quickSort(array, pi + 1, high);
}
}
// Driver code
public static void main(String args[]) {
int[] data = { 8, 7, 2, 1, 0, 9, 6 };
int size = data.length;
QuickSort qs = new QuickSort();
qs.quickSort(data, 0, size - 1);
System.out.println("Sorted Array in Ascending Order: ");
System.out.println(Arrays.toString(data));
}
}