-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubbleSort.js
80 lines (58 loc) · 1.51 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
function bubble(array){
let count=0
for(let i=0;i<array.length;i++){
for(j=0;j<array.length-1;j++){
if(array[j]>array[j+1]){
let temp=array[j]
array[j]=array[j+1]
array[j+1]=temp
count++
}
}
}return [count,array]
}
function bubble(array){
for(let i=0;i<array.length;i++){
for(j=0;j<array.length-1;j++){
if(array[j]>array[j+1]){
[array[j],array[j+1]]=[array[j+1],array[j]]
}
}
}return array
}
function bubble(arra){
for(let i=0;i<arra.length-1;i++){
for(j=i+1;j<arra.length;j++){
if(arra[i]>arra[j]){
temp=arra[i]
arra[i]=arra[j]
arra[j]=temp
}
}
}return arra
}
function bubble(arra,n){
for(let i=0;i<arra.length-1;i++){
for(j=i+1;j<arra.length;j++){
if(arra[i]>arra[j]){
temp=arra[i]
arra[i]=arra[j]
arra[j]=temp
}
}
}console.log(arra);
for(let i=0;i<arra.length;i++){
if(n<arra[0]){
console.log(`index of new element is 0`);
break;
}
else if(arra[i]<n&&arra[i+1]>n)
{
console.log(`index of element ${n} is ${i+1}`);
}
else{
console.log(`index is ${arra.length}`);
break;
}
}
}