Created
November 20, 2013 06:29
-
-
Save jacknoble/7558668 to your computer and use it in GitHub Desktop.
Unworkable completion detection in a ruby bubble sort.
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 single_run_bubble_sort(data) | |
| i = 0 | |
| while i < (data.count - 1) | |
| if unsorted?(data[i], data[i + 1]) | |
| data[i], data[i+1] = data[i+1], data[i] | |
| end | |
| i += 1 | |
| end | |
| data | |
| end | |
| def unsorted?(first, second) | |
| first > second | |
| end | |
| def bubble_sort(data) | |
| pre_sort = data | |
| p pre_sort | |
| post_sort = single_run_bubble_sort(data) | |
| p pre_sort | |
| until pre_sort == post_sort | |
| puts "this ran" | |
| pre_sort = post_sort | |
| post_sort = single_run_bubble_sort(post_sort) | |
| end | |
| post_sort | |
| end | |
| # workable sort, but time is always max | |
| # def bubble_sort(data) | |
| # post_data = data | |
| # data.length.times do | |
| # post_data = single_run_bubble_sort(data) | |
| # end | |
| # | |
| # post_data | |
| # end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment