Skip to main content

I am a beginner in python and to learn python I have written this program. How to improvecan this programcode be improved? Should I use main method in this program? Python

lst = []

def insertion_sort(thelist):
    for index in range(1, len(thelist)):
        key = thelist[index]
        position = index - 1
        while position >= 0 and thelist[position] > key:
            thelist[position + 1] = thelist[position]
            position = position - 1
        thelist[position + 1] = key

n = int(input("Enter number of elements: "))
for i in range(0, n):
    ele = int(input())
    lst.append(ele)

insertion_sort(lst)
print(*lst, sep = ", ")

I am a beginner in python and to learn python I have written this program. How to improve this program? Should I use main method in this program? Python

lst = []

def insertion_sort(thelist):
    for index in range(1, len(thelist)):
        key = thelist[index]
        position = index - 1
        while position >= 0 and thelist[position] > key:
            thelist[position + 1] = thelist[position]
            position = position - 1
        thelist[position + 1] = key

n = int(input("Enter number of elements: "))
for i in range(0, n):
    ele = int(input())
    lst.append(ele)

insertion_sort(lst)
print(*lst, sep = ", ")

I am a beginner in python and to learn python I have written this program. How can this code be improved? Should I use main method in this program? Python

lst = []

def insertion_sort(thelist):
    for index in range(1, len(thelist)):
        key = thelist[index]
        position = index - 1
        while position >= 0 and thelist[position] > key:
            thelist[position + 1] = thelist[position]
            position = position - 1
        thelist[position + 1] = key

n = int(input("Enter number of elements: "))
for i in range(0, n):
    ele = int(input())
    lst.append(ele)

insertion_sort(lst)
print(*lst, sep = ", ")
Source Link
coder
  • 2.5k
  • 4
  • 33
  • 60

Python: Insertion Sort

I am a beginner in python and to learn python I have written this program. How to improve this program? Should I use main method in this program? Python

lst = []

def insertion_sort(thelist):
    for index in range(1, len(thelist)):
        key = thelist[index]
        position = index - 1
        while position >= 0 and thelist[position] > key:
            thelist[position + 1] = thelist[position]
            position = position - 1
        thelist[position + 1] = key

n = int(input("Enter number of elements: "))
for i in range(0, n):
    ele = int(input())
    lst.append(ele)

insertion_sort(lst)
print(*lst, sep = ", ")