-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.java
More file actions
65 lines (52 loc) · 2.05 KB
/
quick_sort.java
File metadata and controls
65 lines (52 loc) · 2.05 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
55
56
57
58
59
60
61
62
63
64
65
package datastructures;
public class quick_sort {
// Main method to call quickSort
public static void main(String[] args) {
int[] array = {10, 7, 8, 9, 1, 5};
System.out.println("Given Array");
printArray(array);
quick_sort ob = new quick_sort();
ob.quickSort(array, 0, array.length - 1);
System.out.println("\nSorted array");
printArray(array);
}
// Method to sort an array using quickSort
public void quickSort(int[] array, int low, int high) {
if (low < high) {
// pi is partitioning index, array[pi] is now at right place
int pi = partition(array, low, high);
// Recursively sort elements before partition and after partition
quickSort(array, low, pi - 1);
quickSort(array, pi + 1, high);
}
}
// Method to take the last element as pivot, places the pivot element at its correct
// position in sorted array, and places all smaller (smaller than pivot) to left of pivot
// and all greater elements to right of pivot
public int partition(int[] array, int low, int high) {
int pivot = array[high];
int i = (low - 1); // Index of smaller element
for (int j = low; j < high; j++) {
// If current element is smaller than or equal to pivot
if (array[j] <= pivot) {
i++;
// Swap array[i] and array[j]
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
// Swap array[i+1] and array[high] (or pivot)
int temp = array[i + 1];
array[i + 1] = array[high];
array[high] = temp;
return i + 1;
}
// Utility method to print the array
public static void printArray(int[] array) {
int n = array.length;
for (int i = 0; i < n; ++i)
System.out.print(array[i] + " ");
System.out.println();
}
}