Write an Insertion Sort program in python to input and sort all the elements of the list.

Tech Programmer
0

WRITE AN INSERTION SORT PROGRAM IN PYTHON TO INPUT AND SORT ALL THE ELEMENTS OF THE LIST.


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):
  elements = L[i]
  if elements<L[i-1]:
    for j in range(i+1):
      if elements<L[j]:
        index = j
        for k in range(ij, -1):
          L[k] = L[k-1]
        break
  else:
    continue
  L[index] = elements

print("\n The New (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):

  elements = L[i]

  if elements<L[i-1]:

    for j in range(i+1):

      if elements<L[j]:

        index = j

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

          L[k] = L[k-1]

        break

  else:

    continue

  L[index] = elements


print("\n The New (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