-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubbleSort.html
46 lines (43 loc) · 1.1 KB
/
bubbleSort.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
const arr = [5,4,3,2,1];
// function bubbleSort(arr) {
// let len = arr.length;
// for(let i=1;i<len;i++){
// if(arr[i-1] && arr[i]>arr[i-1]){
// continue;
// }else{
// let n = arr[i];
// arr.splice(i,1);
// for(let j=0;j<len-1;j++){
// if(arr[j]>=n){
// arr.splice(j,0,n);
// break;
// }
// }
// }
// }
// return arr;
// }
function bubbleSort(){
let len = arr.length;
for(let i=0;i<len-1;i++){
for(let j=0;j<len-1-i;j++){
if(arr[j]>arr[j+1]){
[arr[j],arr[j+1]] = [arr[j+1],arr[j]];
console.log(arr);
}
}
}
return arr;
}
console.log(bubbleSort(arr));
</script>
</body>
</html>