-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
139 lines (121 loc) · 3.65 KB
/
index.js
File metadata and controls
139 lines (121 loc) · 3.65 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
const getRandomInt = require("./helpers/getRandomInt")
const { performance } = require('perf_hooks');
bubbleSort = (array) => {
for (let i = 0; i < array.length; i++) {
for (let j = 0; j < array.length - i - 1; j++) {
if (array[j + 1] < array[j]) {
[array[j + 1], array[j]] = [array[j], array[j + 1]];
}
}
}
return array
}
insertionSort = (array) => {
for (let i = 0; i < array.length; i++) {
let key = array[i]
let j = i - 1
while (j >= 0 && array[j] > key) {
array[j + 1] = array[j]
j--
}
array[j + 1] = key
}
return array
}
selectionSort = (array) => {
for (let i = 0; i < array.length - 1; i++) {
let minIndex = i;
for (let j = i + 1; j < array.length; j++) {
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
[array[i], array[minIndex]] = [array[minIndex], array[i]];
}
return array;
}
/* quickSortPartition = (array, left = 0, right = array.length - 1) => {
let pivot = array[Math.floor((right + left) / 2)]
let low = left
let high = right
while (low <= high) {
while (array[low] < pivot) {
low++
}
while (array[high] > pivot) {
high--
}
if (low <= high) {
[array[low], array[high]] = [array[high], array[low]]
//swap(array, low, high); //sawpping two elements
low++
high--
}
}
//[array[low], array[high]] = [array[high], array[low]]
return low
}
quickSort = (array, left = 0, right = array.length - 1) => {
let pivotIndex = quickSortPartition(array, left, right);
quickSort(array, left, pivotIndex - 1);
quickSort(array, pivotIndex + 1, right);
return array
}
*/
merge = (arr1, arr2) => {
let sorted = [];
while (arr1.length && arr2.length) {
if (arr1[0] < arr2[0]) {
sorted.push(arr1.shift())
} else {
sorted.push(arr2.shift())
}
}
return sorted.concat(arr1.slice().concat(arr2.slice()));
}
mergeSort = (array) => {
if (array.length <= 1) return array
let mid = Math.floor(array.length / 2),
left = mergeSort(array.slice(0, mid)),
right = mergeSort(array.slice(mid));
return merge(left, right);
}
buildAarray = (n) => {
let a = []
for (let i = 0; i < n; i++) {
a.push(getRandomInt.getRandomInt(-100, 100))
}
return a
}
console.log("BUBBLE---------------------------")
let bubbleArray = buildAarray(20)
console.log(bubbleArray)
const t0 = performance.now();
bubbleArray = bubbleSort(bubbleArray)
const t1 = performance.now();
console.log(`Bubble sort: ${t1 - t0} milliseconds.`);
console.log(bubbleArray)
console.log("INSERTION---------------------------")
let insertionArray = buildAarray(20)
console.log(insertionArray)
const t2 = performance.now();
insertionArray = insertionSort(insertionArray)
const t3 = performance.now();
console.log(`Insertion sort: ${t3 - t2} milliseconds.`);
console.log(insertionArray)
console.log("SELECTION---------------------------")
let selectionArray = buildAarray(20)
console.log(selectionArray)
const t4 = performance.now();
selectionArray = selectionSort(selectionArray)
const t5 = performance.now();
console.log(`Selection sort: ${t3 - t2} milliseconds.`);
console.log(selectionArray)
console.log("MERGE---------------------------")
let mergeArray = buildAarray(20)
console.log(mergeArray)
const t6 = performance.now();
mergeArray = mergeSort(mergeArray)
const t7 = performance.now();
console.log(`Merge sort: ${t5 - t4} milliseconds.`);
console.log(mergeArray)