Created
August 7, 2022 13:33
-
-
Save everylittlefox/26a49c4c7e62051339c839a377aede35 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def bubble_sort(array) | |
| loop do | |
| swapped = false | |
| for i in (1...array.length) | |
| if array[i-1] > array[i] | |
| array[i-1], array[i] = array[i], array[i-1] | |
| swapped = true | |
| end | |
| end | |
| break unless swapped | |
| end | |
| array | |
| end | |
| p bubble_sort([4,3,78,2,0,2]) | |
| #=> [0,2,2,3,4,78] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment