-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubbleSort.js
44 lines (38 loc) · 873 Bytes
/
bubbleSort.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
function bubbleSort(numArray) {
var result = numArray.slice();
var swapped;
do {
swapped = false;
for (var i = 1; i < result.length; i++) {
if (result[i] < result[i - 1]) {
var temp = result[i];
result[i] = result[i - 1];
result[i - 1] = temp;
swapped = true;
}
}
}
while(swapped);
return result;
}
function _swap(arr, indexOne, indexTwo) {
var temp = arr[indexOne];
arr[indexOne] = arr[indexTwo];
arr[indexTwo] = temp;
}
var testCase = [
[1, 3, -2, 5, 100, -2],
[6, 5, 4, 3, 1, 0, -10, -21],
[0, 1, 2, 3 ,4 ,5, 6, 7],
[1000, 1, 2000, 2, 3000, 3],
[9999, 9, 8888, 8, 7777, 7],
[],
[1, 2],
[1, 0],
[1, 1, 1, 1, 1, 10, 1, 1, 1, 1],
[1, 1, 1, 1, 1, -10, 1, 1, 1, 1]
];
for (var i = 0; i < testCase.length; i++) {
console.log("Before sort: " + testCase[i] );
console.log("After sort: " + bubbleSort(testCase[i]) + "\n");
}