Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def my_insertion_sort(arr):
- sort_arr = []
- unsorted_arr = arr.copy()
- for i in range(len(arr)):
- sort_arr.append(unsorted_arr.pop(0))
- if len(sort_arr) < 1:
- sort_arr.append(unsorted_arr.pop(0))
- if len(unsorted_arr) > 0:
- if sort_arr[-1] > unsorted_arr[0]:
- sort_arr[-1], unsorted_arr[0], = unsorted_arr[0], sort_arr[-1]
- for j in range(len(sort_arr)-1, 0, -1):
- if sort_arr[j-1] > sort_arr[j]:
- sort_arr[j-1], sort_arr[j] = sort_arr[j], sort_arr[j-1]
- else:
- break
- return sort_arr
- myList = [4, 2, 89, 12, 6, 32]
- print(my_insertion_sort(myList))
- myList = [4, 2, 89, 12, 6, 32, 17, 29, 8, 1]
- print(my_insertion_sort(myList))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement