-
Notifications
You must be signed in to change notification settings - Fork 15
/
ex06.js
51 lines (38 loc) · 1.02 KB
/
ex06.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
/*
Bubble sort
Write a function "bubbleSort".
The function takes and sorts an array.
The array contains only positive numbers.
Exemple:
[8, 3, 0] -> [0, 3, 8]
Forbidden functions:
sort
Tips:
use google
Validations:
Use the bubble sort algorithm (wikipedia);
Write your own tests !
*/
// write your code below this comment
function bubbleSort(numArray){
var sortedarray = [];
for(i of numArray){
if(sortedarray[0] != undefined){
for(num = 0; num < sortedarray.length; num++){
if( i < sortedarray[num] ){
sortedarray.splice(num, 0, i);
break;
}
else if (sortedarray.length-1 == num){
sortedarray.push(i);
break;
}
}
}
else{
sortedarray.push(i);
}
}
return sortedarray;
}
console.log(bubbleSort([5, 4, 8, 3, 0, 1555, 1444, 6167, 84, 12, 6, 2]));