1
- /* Bubble Sort is a algorithm to sort an array. It
2
- * compares adjacent element and swaps thier position
3
- * The big O on bubble sort in worst and best case is O(N^2).
4
- * Not efficient.
1
+ /* Bubble Sort is an algorithm to sort an array. It
2
+ * compares adjacent element and swaps thier position
3
+ * The big O on bubble sort in worst and best case is O(N^2).
4
+ * Not efficient.
5
+ *
6
+ * In bubble sort, we keep iterating while something was swapped in
7
+ * the previous inner-loop iteration. By swapped I mean, in the
8
+ * inner loop iteration, we check each number if the number proceeding
9
+ * it is greater than itself, if so we swap them.
10
+ *
11
+ * Wikipedia: https://en.wikipedia.org/wiki/Bubble_sort
5
12
*/
6
13
14
+ /*
15
+ * Doctests
16
+ *
17
+ * > bubbleSort([5, 4, 1, 2, 3])
18
+ * [1, 2, 3, 4, 5]
19
+ * > bubbleSort([])
20
+ * []
21
+ * > bubbleSort([1, 2, 3])
22
+ * [1, 2, 3]
23
+ *
24
+ * > alternativeBubbleSort([5, 4, 1, 2, 3])
25
+ * [1, 2, 3, 4, 5]
26
+ * > alternativeBubbleSort([])
27
+ * []
28
+ * > alternativeBubbleSort([1, 2, 3])
29
+ * [1, 2, 3]
30
+ */
31
+
32
+ /*
33
+ * Using 2 for loops
34
+ */
7
35
function bubbleSort ( items ) {
8
36
const length = items . length
37
+
9
38
for ( let i = ( length - 1 ) ; i > 0 ; i -- ) {
10
39
// Number of passes
11
40
for ( let j = ( length - i ) ; j > 0 ; j -- ) {
@@ -16,31 +45,28 @@ function bubbleSort (items) {
16
45
}
17
46
}
18
47
}
19
- }
20
-
21
- // Implementation of bubbleSort
22
48
23
- var ar = [ 5 , 6 , 7 , 8 , 1 , 2 , 12 , 14 ]
24
- // Array before Sort
25
- console . log ( '-----before sorting-----' )
26
- console . log ( ar )
27
- bubbleSort ( ar )
28
- // Array after sort
29
- console . log ( '-----after sorting-----' )
30
- console . log ( ar )
49
+ return items
50
+ }
31
51
32
- /* alternative implementation of bubble sort algorithm.
33
- Using a while loop instead. For educational purposses only
34
- */
35
52
/*
36
- *In bubble sort, we keep iterating while something was swapped in
37
- *the previous inner-loop iteration. By swapped I mean, in the
38
- *inner loop iteration, we check each number if the number proceeding
39
- *it is greater than itself, if so we swap them.
53
+ * Implementation of 2 for loops method
40
54
*/
55
+ var array1 = [ 5 , 6 , 7 , 8 , 1 , 2 , 12 , 14 ]
56
+ // Before Sort
57
+ console . log ( '\n- Before Sort | Implementation using 2 for loops -' )
58
+ console . log ( array1 )
59
+ // After Sort
60
+ console . log ( '- After Sort | Implementation using 2 for loops -' )
61
+ console . log ( bubbleSort ( array1 ) )
62
+ console . log ( '\n' )
41
63
64
+ /*
65
+ * Using a while loop and a for loop
66
+ */
42
67
function alternativeBubbleSort ( arr ) {
43
68
let swapped = true
69
+
44
70
while ( swapped ) {
45
71
swapped = false
46
72
for ( let i = 0 ; i < arr . length - 1 ; i ++ ) {
@@ -50,12 +76,18 @@ function alternativeBubbleSort (arr) {
50
76
}
51
77
}
52
78
}
79
+
53
80
return arr
54
81
}
55
82
56
- // test
57
- console . log ( '-----before sorting-----' )
58
- var array = [ 10 , 5 , 3 , 8 , 2 , 6 , 4 , 7 , 9 , 1 ]
59
- console . log ( array )
60
- console . log ( '-----after sorting-----' )
61
- console . log ( alternativeBubbleSort ( array ) )
83
+ /*
84
+ * Implementation of a while loop and a for loop method
85
+ */
86
+ var array2 = [ 5 , 6 , 7 , 8 , 1 , 2 , 12 , 14 ]
87
+ // Before Sort
88
+ console . log ( '\n- Before Sort | Implementation using a while loop and a for loop -' )
89
+ console . log ( array2 )
90
+ // After Sort
91
+ console . log ( '- After Sort | Implementation using a while loop and a for loop -' )
92
+ console . log ( alternativeBubbleSort ( array2 ) )
93
+ console . log ( '\n' )
0 commit comments