Python Forum

Full Version: insertion sort
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi guys can u help me

idky when i print this code, there's the None printed too. how do i get rid of the none thingy when i print my result?


this is my code:
def insertionSort(my_list):
    
    for i in range (1, len(my_list)):
        current_element= my_list[i]
        a = i 
        while current_element < my_list[a-1] and a>0 :
            my_list[a] = my_list[a-1]
            a = a-1
            
        my_list[a] = current_element  
      
for i in range (1,21):
    y=[(5*i+2)%37]
    
    print(y)
    print(insertionSort(y)) 


and i got:
Output:
[7] None [12] None [17] None [22] None [27] None [32] None [0] None [5] None [10] None [15] None [20] None [25] None [30] None [35] None [3] None [8] None [13] None [18] None [23] None [28] None
If function does not explicitly return (or yield) anything it returns None. This is the case with insertionSort function (no return or yield, so None).
You are sorting y. After calling insertionSort(y) y should be sorted. Just print(y) again to see the sorted list.

Also your print statements should not be indented. They are running inside the for loop that creates your list. Indentation in python is like {} in C.
everytime you run the for loop it doesn't add the y to a list but considers y as a list of single element, thus on insertion sort it gives you non. To get your desired solution in the for loop append y to a list and insertion sort the list.
Hope this helps