-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble.js
More file actions
40 lines (33 loc) · 751 Bytes
/
bubble.js
File metadata and controls
40 lines (33 loc) · 751 Bytes
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
function bubbleSort(array) {
let iters = array.length;
/* your code here */
if (array.length === 0){
return [];
}
while (scan(array)){
for (let i = 0; i < array.length - 1; i++){
if (array[i] > array[i + 1]){
array = swap(array, i, i + 1);
}
}
}
return array;
}
function swap(arr, i, j){
let tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
return arr;
}
// let array1 = [1,2,3,5,4];
let array2 = [2,1,5,4,3,6,9,99];
// console.log(bubbleSort(array1));
console.log(bubbleSort(array2));
function scan(arr){
for (let i = 0; i < arr.length - 1; i++){
if (arr[i] > arr[i+1]){
return true;
}
}
return false;
}