Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list, compares adjacent components and swaps them if they are in the wrong order. More efficient algorithms such as timsort, or merge sort are used by the sorting library build into popular programming languages such as Python and Java.
The only significant advantage that bubble sort has over most other algorithms, even quicksort, but not insertion sort, is that the ability to detect that the list is sorted efficiently is build into the algorithm. Therefore, bubble sort is not a practical sorting algorithm.
def bubble_sort(array) n = array.length loop do swapped = false (n-1).times do |i| if array[i] > array[i+1] array[i], array[i+1] = array[i+1], array[i] swapped = true end end break if not swapped end array end puts "Enter a list of numbers seprated by space" list = gets bubble_sort(list) print list