InterviewSolution
Saved Bookmarks
| 1. |
In the following list containing integers, sort the list using Insertion sort algorithm.Also show the status of the list after each iteration.15 -5 20 -10 10 |
|
Answer» def insertion_sort(DATA_LIST): for K in range (1, len(DATA_LIST)): temp=DATA_LIST[K] # ptr=K-1, while(ptr>=0) and DATA_LIST[ptr]>temp: DATA_LIST[ptr+l]=DATA_LIST[ptr] ptr=ptr-1 DATA_LIST [ptr+1] =temp print DATA_LIST DATA_LIST = [15,-5,20,-10,10] print “LIST BEFOR SORTING”,DATAJ.IST insertion_sort(DATA_LIST) [-5,15, 20, -10,10] [-5,15,20,-10,10]-Pass 1 [-10,-5,15,20,10]-Pass 2 [-10,-5,10,15, 20]-Pass 3 |
|