forked from iiitv/algos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickSort.js
83 lines (79 loc) · 1.64 KB
/
quickSort.js
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
74
75
76
77
78
79
80
81
82
83
/*
* Implementation of quicksort algorithm
* Time complexity: O(nlogn)
* Space complexity: O(n)
* n is the size of array
*/
function median (arr, i, j, k) {
/*
:param arr: array of elements
:param i: index of first element
:param j: index of last element
:param k: index of middle element
:return: return median of values at indices i, j and k
*/
if (arr[i] > arr[j] && arr[i] > arr[k]) {
if (arr[k] > arr[j]) {
return k;
} else {
return j;
}
} else if (arr[j] > arr[i] && arr[j] > arr[k]) {
if (arr[k] > arr[i]) {
return k;
} else {
return i;
}
} else {
if (arr[i] > arr[j]) {
return i;
} else {
return j;
}
}
}
function partition (arr, start, end) {
/*
:param arr: array of elements
:param start: index of first element
:param end: index of last element
:return: return value for function, used in partitioning of array
*/
let j = start - 1;
let tmp;
let pi = median(arr, start, end, parseInt((start + end) / 2));
tmp = arr[pi];
arr[pi] = arr[end];
arr[end] = tmp;
let pivot = arr[end];
for (let i = start; i < end; i++) {
if (arr[i] <= pivot) {
j++;
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
tmp = arr[j + 1];
arr[j + 1] = arr[end];
arr[end] = tmp;
return j + 1;
}
function quickSort (arr, left = 0, right = arr.length - 1) {
/*
:param arr: array to be sorted
:param left: index of first element
:param right: index of last element
*/
if (left < right) {
let p = partition(arr, left, right);
quickSort(arr, left, p - 1);
quickSort(arr, p + 1, right);
}
}
function main () {
let arr = [2, 1, 6, 44, 8, 9, 10];
quickSort(arr);
console.log('Sorted data is', arr);
}
main();