Created
March 3, 2015 17:54
-
-
Save JeffCohen/28d3632f687b9bd21c10 to your computer and use it in GitHub Desktop.
In-Place Selection 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
import random | |
def sort(items): | |
for position in range(0, len(items)): | |
min_position_so_far = position | |
for z in range(position+1, len(items)): | |
if items[z] < items[min_position_so_far]: | |
min_position_so_far = z | |
if position != min_position_so_far: | |
temp = items[position] | |
items[position] = items[min_position_so_far] | |
items[min_position_so_far] = temp | |
return items | |
numbers = list(range(10)) | |
random.shuffle(numbers) | |
print("Will sort this list:", numbers) | |
assert list(range(10)) == sort(numbers) | |
print("The list was sorted correctly.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment