Created
April 2, 2020 05:26
-
-
Save AntiKnot/5f9c2fa38b211735b9d9f68e07300d96 to your computer and use it in GitHub Desktop.
inset 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
| import random | |
| from typing import List | |
| test_case = [2, 1, 3, 8, 0] | |
| def insert_sort(l: List[int]) -> List[int]: | |
| if l is []: | |
| return [] | |
| length = len(l) | |
| for i in range(1, length): | |
| cur = l[i] | |
| for j in range(i)[::-1]: | |
| if l[j] < cur: | |
| break | |
| if l[j] >= cur: | |
| l[j + 1] = l[j] | |
| l[j] = cur | |
| return l | |
| if __name__ == '__main__': | |
| print(insert_sort(test_case)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment