-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubbleSort.js
40 lines (34 loc) · 1.18 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
class bubbleSort{
constructor(bubble){
this.bubble = bubble
}
sorting(list){
if (this.bubble){
if (list.indexOf(this.bubble) == (list.length-1)){
return list
}
let bubble = list[list.indexOf(this.bubble)]
let afterBubble = list[list.indexOf(this.bubble)+1]
if (bubble > afterBubble){
list[list.indexOf(this.bubble)] = afterBubble;
list[list.indexOf(this.bubble)+1] = bubble
this.sorting(list)
}else{
this.bubble = list[list.indexOf(this.bubble)+1]
this.sorting(list)
}
}else{
this.bubble = list[0];
let bubble = list[list.indexOf(this.bubble)]
let afterBubble = list[list.indexOf(this.bubble)+1]
if (bubble > afterBubble){
list[list.indexOf(this.bubble)] = afterBubble;
list[list.indexOf(this.bubble)+1] = bubble
this.sorting(list)
}else{
this.bubble = list[list.indexOf(this.bubble)+1]
this.sorting(list)
}
}
}
}