Repository navigation
Expand file tree
/
Copy pathquickSort.jsx
More file actions
62 lines (59 loc) · 1.86 KB
/
Copy pathquickSort.jsx
File metadata and controls
62 lines (59 loc) · 1.86 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
export function getQuickSortAnimations(array) {
const animations = [];
if (array.length <= 1) return array;
const javaScriptSortedArray = array.slice().sort((a, b) => a - b);
if (arraysAreEqual(javaScriptSortedArray, array)) return animations;
const auxiliaryArray = array.slice();
doQuickSort(animations, auxiliaryArray, 0, auxiliaryArray.length - 1);
return animations;
}
function arraysAreEqual(arrayOne, arrayTwo) {
if (arrayOne.length !== arrayTwo.length) return false;
for (let i = 0; i < arrayOne.length; i++) {
if (arrayOne[i] !== arrayTwo[i]) {
return false;
}
}
return true;
}
function swap(animations, items, leftIndex, rightIndex) {
var temp = items[leftIndex];
items[leftIndex] = items[rightIndex];
items[rightIndex] = temp;
animations.push([leftIndex, rightIndex]);
}
export function quickSortPartition(items, left, right, animations) {
var pivot = items[Math.floor((right + left) / 2)], //middle element
i = left, //left pointer
j = right; //right pointer
while (i <= j) {
while (items[i] < pivot) {
i++;
}
while (items[j] > pivot) {
j--;
}
if (i <= j) {
swap(animations, items, i, j); //sawpping two elements
i++;
j--;
}
}
return i;
}
export function doQuickSort(animations, items, left, right) {
var index;
if (items.length > 1) {
index = quickSortPartition(items, left, right, animations); //index returned from partition
if (left < index - 1) {
//more elements on the left side of the pivot
doQuickSort(animations, items, left, index - 1);
}
if (index < right) {
//more elements on the right side of the pivot
doQuickSort(animations, items, index, right);
}
}
console.log(items);
return items;
}