-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorts.js
More file actions
110 lines (90 loc) · 2.5 KB
/
sorts.js
File metadata and controls
110 lines (90 loc) · 2.5 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
class Sorts {
/*MERGE*/
/**
* This sort has two parts, first the list must be broken up into individual arrays
* Second, the arrays must be joined together and "merged" to form a larger sorted array
* @param {Array} list un-ordered lisr
* @return {Array} sorted list
*/
mergeSort(list) {
// if the list is of length 1, it is alredy sorted!
if (list.length === 1) {
return list
}
const middle = Math.round(list.length / 2);
const left = list.slice(0, middle);
const right = list.slice(middle, list.length);
return this.merge(this.mergeSort(left), this.mergeSort(right));
}
/**
* Merges sorted lists into a biggest sorted list
* @param {Array} left ordered lisr
* @param {Array} right ordered lisr
* @return {Array} joined ordered list
*/
merge(left, right) {
const sortedList = [];
while(left.length && right.length) {
if (left[0] > right[0]) {
sortedList.push(right.shift());
continue;
}
sortedList.push(left.shift());
}
if(left.length) {
sortedList.push(...left);
}
if(right.length) {
sortedList.push(...right);
}
return sortedList;
}
/*QUICK*/
/**
* This algo is nlog(n) just like the merge but it does not do well on lists that are already sorted
* @param {Array} list list
* @return {Array} sorted list
*/
quickSort(list, left, right) {
left = left || 0;
right = right || list.length - 1;
const pivot = this.partition(list, left, right);
console.log(pivot, list[pivot]);
if(left < pivot - 1) {
this.quickSort(list, left, pivot - 1);
}
if(right > pivot) {
this.quickSort(list, pivot, right)
}
return list;
}
partition(list, left, right) {
const pivot = Math.floor((left + right) / 2);
while(left <= right) {
while(list[left] < list[pivot]) {
left++;
}
while(list[right] > list[pivot]) {
right--;
}
if(left <= right) {
this.swap(list, left, right);
left++;
right--;
}
}
return left;
}
/**
* Simple swap of two value in a list
*/
swap(list, firstI, secondI) {
const temp = list[firstI];
list[firstI] = list[secondI];
list[secondI] = temp;
return list;
}
}
const sortAlgos = new Sorts()
console.log(sortAlgos.mergeSort([9,4,5,7,3,2,4,7,4,3,3, 0, -99, 45, 100, 101, 34, -08, 9]));
console.log(sortAlgos.quickSort([9,4,5,7,3,2,4,7,4,3,3, 0, -99, 45, 100, 101, 34, -08, 9]));