-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsortingAlgorithms.js
139 lines (123 loc) · 3.74 KB
/
sortingAlgorithms.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
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
// Bubblesort
// Start on the right of the array. Compare two items. Make the items
// switch places if the left one is bigger than the right one. Proceed
// one step. Repeat until at the end of array: the smallest element is
// now at the front.
// Proceed like this with the rest of the array.
// [2][5][1]
// [2][1][5]
// [1][2][5]
function bubblesort(array, testFunction) {
const ArrayToSort = array;
if (!testFunction) {
testFunction = function regularSort(a, b) {
return a - b;
};
}
for (let j = 0; ArrayToSort.length > j; j++) {
for (let i = ArrayToSort.length - 1; i > 0; i--) {
const indexPosition = i;
const leftFromIndexPosition = i - 1;
let leftElement = ArrayToSort[leftFromIndexPosition];
let rightElement = ArrayToSort[indexPosition];
if (testFunction(leftElement, rightElement) > 0) {
const temp = leftElement;
leftElement = rightElement;
rightElement = temp;
ArrayToSort[leftFromIndexPosition] = leftElement;
ArrayToSort[indexPosition] = rightElement;
}
}
}
return ArrayToSort;
}
// Selection Sort
// Go through all elements of the input array to find the smallest one.
// Swap this item to be on the far left. Proceed like this with the rest
// of the array.
// [8][3][5]
// [3][8][5]
// [3][5][8]
function selectionsort(array, testFunction) {
if (!testFunction) {
testFunction = function regularSort(a, b) {
return a - b;
};
}
for (let i = 0; i < array.length; i++) {
let smallestElementIndex = i;
for (let j = i + 1; j < array.length; j++) {
if (testFunction(array[smallestElementIndex], array[j]) > 0) {
smallestElementIndex = j;
}
}
swapPositions(array, i, smallestElementIndex);
}
return array;
}
// Gnome Sort
// Suppose you have a gnome who wants to sort your array. He starts
// at the left.
// If he sees that the two items in front of him are sorted, he makes a
// step to the right.
// If he sees that the two items in front of him are not sorted, he swaps
// them and makes a step to the left.
// If he can't go further to the left, he goes one step to the right.
// If he arrives at the far right, he stops, since there is no number to
// compare to.
function gnomesort(array, testFunction) {
let gnomeComparesElements = testFunction;
let positionOfGnome = 1;
function moveGnomeToLeft() {
positionOfGnome--;
}
function moveGnomeToRight() {
positionOfGnome++;
}
function isGnomeOnTheLeft() {
return positionOfGnome === 1;
}
function isGnomeOnTheRight() {
return positionOfGnome >= array.length;
}
function getElementLeftOfGnome() {
return array[positionOfGnome - 1];
}
function getRightElementOfGnome() {
return array[positionOfGnome];
}
if (!gnomeComparesElements) {
gnomeComparesElements = function regularSort(a, b) {
return a - b;
};
}
function gnomeSwitchesElements() {
const indexOfLeft = positionOfGnome - 1;
const indexOfRight = positionOfGnome;
swapPositions(array, indexOfLeft, indexOfRight);
}
while (!isGnomeOnTheRight()) {
const leftElement = getElementLeftOfGnome();
const rightElement = getRightElementOfGnome();
if (gnomeComparesElements(leftElement, rightElement) <= 0) {
moveGnomeToRight();
} else {
gnomeSwitchesElements();
if (isGnomeOnTheLeft()) {
moveGnomeToRight();
} else {
moveGnomeToLeft();
}
}
}
return array;
}
function swapPositions(array, firstIndex, secondIndex) {
const myArray = array;
const temp = myArray[firstIndex];
myArray[firstIndex] = myArray[secondIndex];
myArray[secondIndex] = temp;
}
module.exports.selectionsort = selectionsort;
module.exports.gnomesort = gnomesort;
module.exports.bubblesort = bubblesort;