-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubbleSort.js
More file actions
37 lines (36 loc) · 869 Bytes
/
bubbleSort.js
File metadata and controls
37 lines (36 loc) · 869 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
function bubbleSort(arr){
var noswaps;
for(var i = arr.length;i>0;i--){
noswaps = true;
for(var j =0;j< i-1;j++){
if(arr[j]>arr[j+1]){
var temp = arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
noswaps = false;
}
}
if(noswaps) break;
}
return arr;
}
console.log(bubbleSort([5,3,4,1,2]));
function BubbleSort(arr){
const swap = (arr,i,j)=>{
[arr[i],arr[j]]=[arr[j],arr[i]];
}
for(var i = arr.length;i>0;i--){
for(var j=0;j<i-1 ;j++){
console.log(arr,j,j+1);
if(arr[j]>arr[j+1]){
swap(arr,j,j+1);
}
}
}
return arr;
}
console.log(BubbleSort([8,1,2,3,4,5,6,7]));
// Time Complexity {
// bestCase:O[N];sorted Array,
// wrostCase:O(N^2);
// }