sort list in assending order INPUT: list=list of values to be sorted n=size of list that contains values to be sorted OUTPUT: list of sorted values in assending order
(list, n)
| 12 | |
| 13 | |
| 14 | def insertion_sort(list, n): |
| 15 | """ |
| 16 | sort list in assending order |
| 17 | |
| 18 | INPUT: |
| 19 | list=list of values to be sorted |
| 20 | n=size of list that contains values to be sorted |
| 21 | |
| 22 | OUTPUT: |
| 23 | list of sorted values in assending order |
| 24 | """ |
| 25 | for i in range(0, n): |
| 26 | key = list[i] |
| 27 | j = i - 1 |
| 28 | # Swap elements witth key iff they are |
| 29 | # greater than key |
| 30 | while j >= 0 and list[j] > key: |
| 31 | list[j + 1] = list[j] |
| 32 | j = j - 1 |
| 33 | list[j + 1] = key |
| 34 | return list |
| 35 | |
| 36 | |
| 37 | def insertion_sort_desc(list, n): |