Line 12: |
Line 12: |
| | | |
| | | |
− | Insertion Sort (sorting algorithm for small number of elements): | + | Insertion Sort (sorting algorithm for small number of elements): This Algorithm rearranges a disordered list of integers and rearranges them in natural order. |
| + | This algorithm is typically used for small array of elements. |
| | | |
| + | Sample Input : [ 8, 3, 9, 0, 2, 7, 4, 5 ] |
| + | Sample Output : [ 0, 2, 3, 4, 5, 7, 8, 9 ] |
| | | |
| '''Python Code''' | | '''Python Code''' |
| --- | | --- |
| def insertionSort(alist): | | def insertionSort(alist): |
− | for index in range(1,len(alist)):
| + | :for index in range(1,len(alist)): |
| | | |
− | currentvalue = alist[index]
| + | ::currentvalue = alist[index] |
− | position = index
| + | ::position = index |
| | | |
− | while position>0 and alist[position-1]>currentvalue:
| + | ::while position>0 and alist[position-1]>currentvalue: |
− | alist[position]=alist[position-1]
| + | :::alist[position]=alist[position-1] |
− | position = position-1
| + | :::position = position-1 |
| | | |
− | alist[position]=currentvalue
| + | ::alist[position]=currentvalue |
| | | |
| alist = [54,26,93,17,77,31,44,55,20] | | alist = [54,26,93,17,77,31,44,55,20] |