Write an Insertion sort program in python to input elements of the list and to print list after each insertion sort

Tech Programmer
0

WRITE AN INSERTION SORT PROGRAM IN PYTHON TO INPUT ELEMENTS OF THE LIST AND TO PRINT LIST AFTER EACH INSERTION SORT.


CODE FOR THE ABOVE  PYTHON  PROGRAM :


L = []
print(end="Enter the No of Elements You Want: ")
LSize = int(input())
print("Enter " +str(LSize)+ " Elements: ")
for i in range(LSize):
  L.append(int(input()))

for i in range(1LSize):
  element = L[i]
  if element<L[i-1]:
    for j in range(i+1):
      if element<L[j]:
        index = j
        for k in range(ij, -1):
          L[k] = L[k-1]
        break
  else:
    continue
  L[index] = element
  print(end="\nStep " +str(i)+ ": ")
  for j in range(LSize):
    print(end=str(L[j]) + " ")
print("\n")
print("The Sorted List is: ")
for i in range(LSize):
  print(end=str(L[i]) + " ")

print()


WRITTEN CODE FOR THE ABOVE PROGRAM :


L = []

print(end="Enter the No of Elements You Want: ")

LSize = int(input())

print("Enter " +str(LSize)+ " Elements: ")

for i in range(LSize):

  L.append(int(input()))


for i in range(1, LSize):

  element = L[i]

  if element<L[i-1]:

    for j in range(i+1):

      if element<L[j]:

        index = j

        for k in range(i, j, -1):

          L[k] = L[k-1]

        break

  else:

    continue

  L[index] = element

  print(end="\nStep " +str(i)+ ": ")

  for j in range(LSize):

    print(end=str(L[j]) + " ")

print("\n")

print("The Sorted List is: ")

for i in range(LSize):

  print(end=str(L[i]) + " ")


print()


OUTPUT :



Post a Comment

0Comments
Post a Comment (0)

Featured Highlighted Categories

You'll discover all of the most up-to-date bring innovative here.

Python List theory with List Programs
Get complete python lists theory and list basic and insane programs for your practice.
CBSE SAMPLE PAPERS
Get CBSE sample and question papers of last 7 years with answers to get best practice for your Board Exams
Python String theory and String Programs
Python strings complete theory with illustrations and practice programs for free