Skip to content

Instantly share code, notes, and snippets.

@jboursiquot
Created February 6, 2019 01:43
Show Gist options
  • Save jboursiquot/f47b0c2cff651bc5ba04521105c2ab19 to your computer and use it in GitHub Desktop.
Save jboursiquot/f47b0c2cff651bc5ba04521105c2ab19 to your computer and use it in GitHub Desktop.

Bubble Sort

Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list, compares adjacent pairs and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller or larger elements "bubble" to the top of the list. -- Wikipedia (https://en.wikipedia.org/wiki/Bubble_sort)

Create a function, bubleSort, that takes a list of integers and sorts them in non-decreasing order using a bubble sort algorithm.

For example:

bubbleSort([]int{9,5,23,11,2}) // should return []int{2, 5, 9, 11, 23}

Problem Set

[]int{5, 2, 4, 1} // []int{1, 2, 4, 5}
[]int{199,-15, 0, 59} // []int{-15, 0, 59, 199}
[]int{0, 0, 0, -1} // []int{-1, 0, 0, 0}

What's the time complexity of your solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment