Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
insertion sort
#1
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
Reply
#2
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).
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
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.
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to sort a list without .sort() function letmecode 3 3,372 Dec-28-2020, 11:21 PM
Last Post: perfringo
  [split] Manual Sort without Sort function fulir16 2 3,118 Jun-02-2019, 06:13 AM
Last Post: perfringo
  Manual Sort without Sort function dtweaponx 26 48,626 Jun-01-2019, 06:02 PM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020