-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble.js
More file actions
executable file
·84 lines (75 loc) · 2.16 KB
/
bubble.js
File metadata and controls
executable file
·84 lines (75 loc) · 2.16 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
function bubbleSort(array, length) {
stat.loops += 1;
if (length < 2) return;
for (var i = 0; i < length - 1; i++) {
if (array[i] > array[i + 1]) {
swap(array, i, i + 1);
}
}
bubbleSort(array, length - 1);
}
function swap(array, first, last) {
stat.totalSwaps += 1;
const temp = array[first];
array[first] = array[last];
array[last] = temp;
}
// Non Recursive(NR) solution
function bubbleSortNR(array) {
const { length } = array;
for (var i = 0; i < length - 1; i++) {
for (var j = 0; j < length - i - 1; j++) {
stat.loops += 1;
if (array[j] > array[j + 1]) swap(array, j, j + 1);
}
}
}
// Minor optimization
function OptimizedBubbleSort(array, length, previousLoopSwaps = 1) {
stat.loops += 1;
if (length < 2) return;
if (previousLoopSwaps === 0) return;
previousLoopSwaps = 0;
for (var i = 0; i < length - 1; i++) {
if (array[i] > array[i + 1]) {
previousLoopSwaps++;
swap(array, i, i + 1);
}
}
OptimizedBubbleSort(array, length - 1, previousLoopSwaps);
}
let unsortedArray = [3, 6, 2, 8, 9, 10];
let sortedArray = [4, 4, 4, 4, 4];
let reverseSortedArray = [10, 8, 5, 3, 2];
showResults(unsortedArray, "unsortedArray");
showResults(sortedArray, "sortedArray");
showResults(reverseSortedArray, "reverseSortedArray");
function showResults(sampleInput, heading) {
console.log(`
HEADING: ${heading}
`);
let input = sampleInput;
let initialStats = { totalSwaps: 0, loops: 0 };
let stat = initialStats;
bubbleSort(input, input.length);
console.log(`bubbleSort
Sorted Array: ${input},
stats: { Total Swaps ${stat.totalSwaps} LOOPS: ${
stat.loops
} }
`);
input = sampleInput;
stat = initialStats;
bubbleSortNR(input, input.length);
console.log(`bubbleSortNR
Sorted Array: ${input},
stats: { Total Swaps ${stat.totalSwaps} LOOPS: ${stat.loops}
`);
input = sampleInput;
stat = initialStats;
OptimizedBubbleSort(input, input.length);
console.log(`bubbleSortNR
Sorted Array: ${input},
stats: { Total Swaps ${stat.totalSwaps} LOOPS: ${stat.loops}
`);
}