-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.js
More file actions
142 lines (131 loc) · 4.68 KB
/
Copy pathQuickSort.js
File metadata and controls
142 lines (131 loc) · 4.68 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
export function QuickSortAlgorithm(array) {
let animations = [];
let auxillaryArray = array.slice();
QuickSort(auxillaryArray, 0, auxillaryArray.length - 1, animations);
let sortedArray = array.slice().sort((a, b) => a - b);
console.log(areEqualCheck(sortedArray, auxillaryArray));
array = auxillaryArray;
return [animations, array];
}
function areEqualCheck(sortedArray, auxillaryArray) {
if (sortedArray.length !== auxillaryArray.length) return false;
for (let i = 0; i < sortedArray.length; i++) {
if (sortedArray[i] !== auxillaryArray[i]) return false;
}
return true;
}
function QuickSort(auxillaryArray, startIndex, endIndex, animations) {
if (startIndex < endIndex) {
let pivotIndex = partition(
auxillaryArray,
startIndex,
endIndex,
animations
);
QuickSort(auxillaryArray, startIndex, pivotIndex - 1, animations);
QuickSort(auxillaryArray, pivotIndex + 1, endIndex, animations);
}
}
function partition(auxillaryArray, startIndex, endIndex, animations) {
let pivot = auxillaryArray[endIndex];
let checkIndex = startIndex;
for (let i = startIndex; i < endIndex; i++) {
animations.push(['comparison1', i, endIndex]);
animations.push(['comparison2', i, endIndex]);
if (auxillaryArray[i] <= pivot) {
animations.push(['swapHeight', i, auxillaryArray[checkIndex]]);
animations.push(['swapHeight', checkIndex, auxillaryArray[i]]);
swap(auxillaryArray, i, checkIndex);
checkIndex++;
} else {
animations.push(['swapHeight', -1, -1]);
animations.push(['swapHeight', -1, -1]);
}
animations.push(['swapHeight', -1, -1]);
animations.push(['swapHeight', -1, -1]);
}
animations.push(['comparison1', -1, -1]);
animations.push(['comparison2', -1, -1]);
animations.push(['swapHeight', -1, -1]);
animations.push(['swapHeight', -1, -1]);
animations.push(['swapHeight', checkIndex, auxillaryArray[endIndex]]);
animations.push(['swapHeight', endIndex, auxillaryArray[checkIndex]]);
swap(auxillaryArray, checkIndex, endIndex);
return checkIndex;
}
function swap(auxillaryArray, firstIndex, secondIndex) {
let temp = auxillaryArray[firstIndex];
auxillaryArray[firstIndex] = auxillaryArray[secondIndex];
auxillaryArray[secondIndex] = temp;
}
// export function QuickSortAlgorithm(array) {
// let animations = [];
// let auxillaryArray = array.slice();
// quickSort(auxillaryArray, 0, auxillaryArray.length - 1, animations);
// const javaScriptSortedArray = array.slice().sort((a, b) => a - b);
// console.log(
// 'sort works correctly? ',
// arraysAreEqual(javaScriptSortedArray, auxillaryArray)
// );
// array = auxillaryArray;
// return [animations, array];
// }
// //QuickSortAlgorithm([7, 2, 1, 6]);
// function quickSort(auxillaryArray, startIndex, endIndex, animations) {
// let pivotIndex;
// if (startIndex < endIndex) {
// pivotIndex = partitionArray(
// auxillaryArray,
// startIndex,
// endIndex,
// animations
// );
// quickSort(auxillaryArray, startIndex, pivotIndex - 1, animations);
// quickSort(auxillaryArray, pivotIndex + 1, endIndex, animations);
// }
// }
// function partitionArray(auxillaryArray, startIndex, endIndex, animations) {
// let pivot = auxillaryArray[endIndex];
// let pivotIndex = startIndex;
// for (let i = startIndex; i <= endIndex - 1; i++) {
// animations.push([i, endIndex]);
// animations.push([i, endIndex]);
// if (auxillaryArray[i] <= pivot) {
// //Swap these two heights
// animations.push([i, auxillaryArray[pivotIndex]]);
// animations.push([pivotIndex, auxillaryArray[i]]);
// swap(auxillaryArray, i, pivotIndex);
// pivotIndex++;
// } else {
// animations.push([-1, -1]);
// animations.push([-1, -1]);
// }
// animations.push([-1, -1]);
// animations.push([-1, -1]);
// }
// animations.push([-1, -1]);
// animations.push([-1, -1]);
// animations.push([-1, -1]);
// animations.push([-1, -1]);
// //Swap these two heights
// animations.push([pivotIndex, auxillaryArray[endIndex]]);
// animations.push([endIndex, auxillaryArray[pivotIndex]]);
// swap(auxillaryArray, pivotIndex, endIndex);
// return pivotIndex;
// }
// function swap(auxillaryArray, firstIndex, secondIndex) {
// let temp = auxillaryArray[firstIndex];
// auxillaryArray[firstIndex] = auxillaryArray[secondIndex];
// auxillaryArray[secondIndex] = temp;
// }
// function arraysAreEqual(firstArray, secondArray) {
// if (firstArray.length !== secondArray.length) {
// return false;
// }
// for (let i = 0; i < firstArray.length; i++) {
// if (firstArray[i] !== secondArray[i]) {
// return false;
// }
// }
// return true;
// }