-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsorting.js
121 lines (106 loc) · 2.55 KB
/
sorting.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
//selection sort
let selectionSort = (nums) => {
for (let i = 0; i < nums.length; i++) {
let lowestNum = nums[i];
let lowestNumIndex = i;
for (let j = i + 1; j < nums.length; j++) {
if (nums[j] < lowestNum) {
lowestNum = nums[j];
lowestNumIndex = j;
}
}
if (lowestNum != nums[i]) {
nums[lowestNumIndex] = nums[i];
nums[i] = lowestNum;
}
}
return nums;
}
//a stable selection sort
let stableSelectionSort = (nums) => {
for (let i = 0; i < nums.length; i++) {
let lowestNum = nums[i].val;
let lowestNumIndex = i;
for (let j = i + 1; j < nums.length; j++) {
if (nums[j].val < lowestNum) {
lowestNum = nums[j].val;
lowestNumIndex = j;
}
}
if (lowestNum != nums[i].val) {
//splice(position to add things, how many to delete, what to add)
nums.splice(i, 0, nums[lowestNumIndex]);
nums.splice((lowestNumIndex + 1), 1);
}
}
return nums;
}
//An insertion sort
let insertionSort = (nums) => {
for (let i = 1; i < nums.length; i++) {
if (nums[i] < nums[i - 1]) {
for (let j = i - 1; j >= 0; j--) {
if (nums[j + 1] < nums[j]) {
swap(nums, i, j);
}
else { break }
}
}
}
return nums;
}
/******* A few useful functions for above functions *******/
//a swapping function
function swap(nums, i, j) {
let tmpVal = nums[i];
nums[i] = nums[j];
nums[j] = tmpVal;
}
//a random pivot for a sorting method
let randomPivot = (nums) => {
return Math.floor(Math.random() * Math.floor(nums.length));
}
let sortMe = [2, 1, 4, 6, 9, 8, 3];
let sortMeObj = [
{
val: 2,
val2: "yay1"
},
{
val: 1,
val2: "yay"
},
{
val: 2,
val2: "yay2"
},
{
val: 4,
val2: "yay"
},
{
val: 6,
val2: "yay"
},
{
val: 9,
val2: "yay"
},
{
val: 8,
val2: "yay"
},
{
val: 3,
val2: "yay"
},
]
console.log(`selection sort: ${selectionSort(sortMe)}`);
console.log("");
console.log("stable selection sort with obj:");
console.log(stableSelectionSort(sortMeObj));
console.log("");
console.log(`insertion sort: ${insertionSort(sortMe)}`);
console.log("");
console.log("insertion sort with objects:");
console.log(insertionSort(sortMeObj));