File tree 1 file changed +13
-13
lines changed
Basic Algorithm Scripting/Bubble Sort
1 file changed +13
-13
lines changed Original file line number Diff line number Diff line change @@ -47,13 +47,13 @@ function bubbleSortWithForLoops(arr) {
47
47
for (var i = 0 ; i < arr .length ; i++ ) {
48
48
// Notice that j < (length - i)
49
49
for (var j = 0 ; j < arr .length - i - 1 ; j++ ) {
50
- // Compare the adjacent positions
51
- if (arr[j] > arr[j + 1 ]) {
52
- // Swap the numbers
53
- // Temporary variable to hold the current number
54
- var tmp = arr[j]
55
- // Replace current number with adjacent number
56
- arr[j] = arr[j + 1 ]
50
+ // Compare the adjacent positions
51
+ if (arr[j] > arr[j + 1 ]) {
52
+ // Swap the numbers
53
+ // Temporary variable to hold the current number
54
+ var tmp = arr[j]
55
+ // Replace current number with adjacent number
56
+ arr[j] = arr[j + 1 ]
57
57
// Replace adjacent number with current number
58
58
arr[j + 1 ] = tmp
59
59
}
@@ -70,12 +70,12 @@ function bubbbleSort(arr) {
70
70
do {
71
71
swapped = false
72
72
arr .forEach ((item , index ) => {
73
- if (item > arrr [index + 1 ]) {
74
- let temp = item
75
- arr[index] = arr[index + 1 ]
76
- arr[index + 1 ] = temp
77
- swapped = true
78
- }
73
+ if (item > arr [index + 1 ]) {
74
+ let temp = item
75
+ arr[index] = arr[index + 1 ]
76
+ arr[index + 1 ] = temp
77
+ swapped = true
78
+ }
79
79
})
80
80
} while (swapped)
81
81
return arr
You can’t perform that action at this time.
0 commit comments