Skip to content

Instantly share code, notes, and snippets.

@ksomemo
Created March 30, 2014 07:52
Show Gist options
  • Select an option

  • Save ksomemo/9869192 to your computer and use it in GitHub Desktop.

Select an option

Save ksomemo/9869192 to your computer and use it in GitHub Desktop.
選択ソート
import random
def my_selection_sort(nums):
l = len(nums)
for i in range(l):
min_idx = i
for j in range(i, l):
if (nums[min_idx] > nums[j]):
min_idx = j
nums[i], nums[min_idx] = nums[min_idx], nums[i]
print nums
return nums
if __name__ == '__main__':
nums = range(10)
random.shuffle(nums)
print nums
print my_selection_sort(nums)
@ksomemo
Copy link
Author

ksomemo commented Mar 30, 2014

$ python my_selection_sort.py
[1, 5, 0, 9, 2, 4, 8, 3, 6, 7]
[0, 5, 1, 9, 2, 4, 8, 3, 6, 7]
[0, 1, 5, 9, 2, 4, 8, 3, 6, 7]
[0, 1, 2, 9, 5, 4, 8, 3, 6, 7]
[0, 1, 2, 3, 5, 4, 8, 9, 6, 7]
[0, 1, 2, 3, 4, 5, 8, 9, 6, 7]
[0, 1, 2, 3, 4, 5, 8, 9, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 9, 8, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment