Bubble Sort
Bubble Sort example in Javascript and Go
Javascript
function BubbleSort(array) {
var swapCount = 1
while (swapCount > 0) {
swapCount = 0
for (let itemIndex = 0; itemIndex < array.length -1; itemIndex++) {
if (array[itemIndex] > array[itemIndex+1]) {
let temp = array[itemIndex+1]
array[itemIndex+1] = array[itemIndex];
array[itemIndex] = temp;
swapCount += 1
}
}
}
return array
}
Go
func BubbleSort(array []int) {
swapCount := 1
for swapCount > 0 {
swapCount = 0
for itemIndex := 0; itemIndex < len(array)-1; itemIndex++ {
if array[itemIndex] > array[itemIndex+1] {
array[itemIndex], array[itemIndex+1] = array[itemIndex+1], array[itemIndex]
swapCount += 1
}
}
}
}