Skip to content

Instantly share code, notes, and snippets.

@armenr
Last active November 30, 2022 09:43
Show Gist options
  • Save armenr/cec233206459697f229a6e0354aa004a to your computer and use it in GitHub Desktop.
Save armenr/cec233206459697f229a6e0354aa004a to your computer and use it in GitHub Desktop.
Super bare-bones, naive implementation of recurisve bubble sort in JS
const myShittyArray = [55, 12, 32, 42, 17, 88, 19, 23, 71, 86, 22]
const bubbleSort = (array) => {
// a method to swap values @ a given pair of index positions
this.shifter = (input, idxNext, idxElement) => {
let temp = input[idxNext]
input[idxNext] = input[idxElement]
input[idxElement] = temp
return input
}
// a recursive method to sort some shit
this.recursor = (array) => {
array.forEach((element, index) => {
let madeSwitch = false
let next = array[index + 1]
if (next < element) {
let nextIndex = index + 1
this.shifter(array, nextIndex, index)
madeSwitch = true
}
if (madeSwitch === true) {
this.recursor(array)
}
})
}
this.recursor(array)
return array
}
bubbleSort(myShittyArray)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment