Created
March 30, 2014 09:13
-
-
Save ksomemo/9870034 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
| # coding: utf-8 | |
| import random | |
| def my_insertion_sort(nums): | |
| for i in range(len(nums)-1): | |
| num = False | |
| idx = -1 | |
| target = i + 1 | |
| for j in range(target): | |
| # 対象より大きい、かつその中で1番小さい値を決定する | |
| if (nums[target] < nums[j] and (num == False or nums[j] < num)): | |
| idx = j | |
| num = nums[j] | |
| if (idx != -1): | |
| nums.insert(idx, nums[target]) | |
| # 対象より前に挿入されるため、1つずれる | |
| del nums[target+1] | |
| print nums | |
| return nums | |
| if __name__ == '__main__': | |
| nums = range(10) | |
| random.shuffle(nums) | |
| print nums | |
| my_insertion_sort(nums) |
Author
ksomemo
commented
Mar 30, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment