-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path23. QuickSortForBeginners.java
More file actions
73 lines (56 loc) · 1.82 KB
/
23. QuickSortForBeginners.java
File metadata and controls
73 lines (56 loc) · 1.82 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
66
67
68
69
70
71
72
73
/*
Quick Sort: As the name suggests, Quicksort is one of the fastest sorting algorithms. The Quicksort algorithm takes an array of values, chooses one of the values as
the 'pivot' element, and moves the other values so that lower values are on the left of the pivot element, and higher values are on the right of it.
How it works:
1. Choose a value in the array to be the pivot element.
2. Order the rest of the array so that lower values than the pivot element are on the left, and higher values are on the right.
3. Swap the pivot element with the first element of the higher values so that the pivot element lands in between the
lower and higher values.
4. Do the same operations (recursively) for the sub-arrays on the left and right side of the pivot element.
*/
//quick sort
//tip: last element is the pivot in first case. compare the elements with pivot.
//smaller element before pivot and the bigger one after pivot (last element).
import java.util.*;
public class QuickSort {
public static int partition(int[] arr, int low, int high) {
int pivot = arr[high];
int i = low-1;
for(int j=low; j<high; j++) {
if(arr[j] < pivot) {
i++;
//swap
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
i++;
int temp = arr[i];
arr[i] = arr[high];
arr[high] = temp;
return i; //pivot index
}
public static void quickSort(int[] arr, int low, int high) {
if(low < high) {
int pivotIdx = partition(arr, low, high);
quickSort(arr, low, pivotIdx-1);
quickSort(arr, pivotIdx+1, high);
}
}
public static void main(String[] args) {
int[] arr = {6, 3, 9, 5, 2, 8};
int n = arr.length;
quickSort(arr, 0, n-1);
//print
for(int i=0; i<n; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
/*
Time Complexity:
- worst case: O(n^2)
- average case: O(nlogn)
*/