-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.js
87 lines (75 loc) · 2.43 KB
/
sort.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
const { merge } = require("./merge");
function bubbleSort1(array) {
for (let i = 0; i < array.length; i++) {
for (let j = 0; j < array.length; j++) {
if (array[j] > array[j + 1]) {
[array[j], array[j + 1]] = [array[j + 1], array[j]];
}
}
}
return array;
}
function bubbleSort2(array) {
for (let i = 0; i < array.length; i++) {
let swapFlag = 1;
for (let j = 0; j < array.length; j++) {
if (array[j] > array[j + 1]) {
[array[j], array[j + 1]] = [array[j + 1], array[j]];
swapFlag = 0;
}
}
if (swapFlag) {
break;
}
}
return array;
}
function selectionSort1(array) {
for (let i = 0; i < array.length; i++) {
let minValueIndex = i;
for (let j = i + 1; j < array.length; j++)
if (array[j] < array[i])
minValueIndex = j;
if (minValueIndex !== i)
[array[i], array[minValueIndex]] = [array[minValueIndex], array[i]];
}
return array;
}
function selectionSort2(array) {
for (let i = 0; i < array.length; i++) {
let minValueIndex = i;
let alreadySorted = 1;
for (let j = i + 1; j < array.length; j++) {
if (array[j] < array[i])
minValueIndex = j;
if (array[j] < array[j - 1])
alreadySorted = 0;
}
if (alreadySorted)
break;
if (minValueIndex !== i)
[array[i], array[minValueIndex]] = [array[minValueIndex], array[i]];
}
return array;
}
function mergeSort1(array) {
if (array.length === 1) return array;
const midpoint = Math.floor(array.length / 2);
return merge(mergeSort1(array.slice(0, midpoint)), mergeSort1(array.slice(midpoint)))
}
function mergeSort2(array) {
if (array.length === 1) return array;
let isAlreadySorted = 1;
for (let i = 1; i < array.length; i++)
if (array[i] < array[i - 1])
isAlreadySorted = 0;
if (isAlreadySorted) return array;
const midpoint = Math.floor(array.length / 2);
return merge(mergeSort2(array.slice(0, midpoint)), mergeSort2(array.slice(midpoint)))
}
/* RETURN GIVEN ARRAY IN SORTED ORDER */
/* Args:-
arg1 => array to be sorted
NOTE:- Sorting will be in increasing order.
*/
module.exports = { bubbleSort1, bubbleSort2, selectionSort1, selectionSort2, mergeSort1, mergeSort2 }