Last active
May 25, 2017 07:54
-
-
Save mortymacs/8c2967610a65e9159625628cfe7b9955 to your computer and use it in GitHub Desktop.
Simple Bubble Sort in Python
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(data): | |
| i, j, swap = 0, 0, 0 | |
| while i < len(data) - 1: | |
| while j < len(data) - 1: | |
| if data[j] > data[j+1]: | |
| data[j], data[j+1] = data[j+1], data[j] | |
| swap += 1 | |
| j += 1 | |
| if swap == 0: # If no change find, so array is already sorted | |
| break | |
| j = 0 | |
| i += 1 | |
| return data | |
| print(bubble_sort([3,2,1])) | |
| # [1, 2, 3] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment