-
Notifications
You must be signed in to change notification settings - Fork 0
/
quicksort4.js
68 lines (60 loc) · 2.23 KB
/
quicksort4.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
// Title: Quicksort (Hoare partition scheme - https://en.wikipedia.org/wiki/Quicksort#Hoare_partition_scheme)
// Complexity: Time: O(n^2) - while loop O(n) + recursive function call O(n), but in average O(n log(n)) (with the middle element as the pivot) https://en.wikipedia.org/wiki/Quicksort#:~:text=With%20the%20middle%20element%20as%20the%20pivot%2C%20however%2C%20sorted%20data%20results%20with%20(almost)%20no%20swaps%20in%20equally%20sized%20partitions%20leading%20to%20best%20case%20behavior%20of%20Quicksort%2C%20i.e.%20O(n%20log(n))
// Space: O(n) - storing recursive function call frame O(n)
// Hint: Main aglorithm sorts a (portion of an) array, divides it into partitions, then sorts those;
// Partition algorithm divides array into two partitions - less than pivot and greater than pivot
// Additional if check in quickSort algorithm reduces the function call on average by half.
/**
*@param {array} array Unsorted array
*@param {number} left Beginning index of an array
*@param {number} right End index of an array
*@returns {array} Sorted array
*/
function quickSort(items, left = 0, right = items.length - 1) {
if (left < right) {
let index = partition(items, left, right);
if (left < index - 1) {
quickSort(items, left, index - 1);
}
if (index < right) {
quickSort(items, index, right);
}
}
return items;
}
/**
* Swaps two elements in array
*@param {array} items
*@param {number} leftIndex
*@param {number} rightIndex
*@returns {void}
*/
function swap(items, leftIndex, rightIndex){
let temp = items[leftIndex];
items[leftIndex] = items[rightIndex];
items[rightIndex] = temp;
}
/**
* Sorted element by pivot
*@param {array} items
*@param {number} left
*@param {number} right
*@returns {number} pivot index
*/
function partition(items, left, right) {
let pivot = items[Math.floor((right + left) / 2)];
while (left <= right) {
while (items[left] < pivot) {
left++;
}
while (items[right] > pivot) {
right--;
}
if (left <= right) {
swap(items, left, right);
left++;
right--;
}
}
return left;
}