Selection Sort In Python

 

DIFFERENT TYPES OF PYTHON PROGRAMS TO PERFORM SELECTION SORT :

  • SELECTION SORT ON DEFINED(GIVEN) LIST
  • SELECTION SORT ON A LIST WITH N NUMBER OF ELEMENTS



1) SELECTION SORT ON DEFINED LIST OF 10 ELEMENTS


WRITE A PYTHON PROGRAM TO SORT A LIST OF 10 ELEMENTS USING SELECTION SORT.


CODE FOR THE ABOVE  PYTHON  PROGRAM :


L = []
print("Enter 10 Elements for a List: ")
for i in range(10):
    L.append(int(input()))

for i in range(9):
    ch = 0
    x = L[i]
    for j in range(i+110):
        if x > L[j]:
            x = L[j]
            ch = ch + 1
            index = j
    if ch != 0:
        temp = L[i]
        L[i] = x
        L[index] = temp

print("\n Sorted List is: ")
for i in range(10):
    print(L[i])


WRITTEN CODE FOR THE ABOVE PROGRAM :


L = []

print("Enter 10 Elements for a List: ")

for i in range(10):

    L.append(int(input()))


for i in range(9):

    ch = 0

    x = L[i]

    for j in range(i+1, 10):

        if x > L[j]:

            x = L[j]

            ch = ch + 1

            index = j

    if ch != 0:

        temp = L[i]

        L[i] = x

        L[index] = temp


print("\n Sorted List is: ")

for i in range(10):

    print(L[i])


OUTPUT :




2) SELECTION SORT ON LIST WITH N NUMBER OF ELEMENTS


WRITE A PYTHON PROGRAM TO SORT A LIST WITH  N NUMBER OF ELEMENTS USING SELECTION SORT.


CODE FOR THE ABOVE  PYTHON  PROGRAM :


L = []
print("Enter the size of list: "end="")
elements = int(input())
print("Enter"elements"numbers for the list: "end="")
for i in range(elements):
  L.append(int(input()))

for i in range(elements-1):
    ch = 0
    x = L[i]
    for j in range(i+1elements):
        if x > L[j]:
            x = L[j]
            ch = ch + 1
            index = j
    if ch != 0:
        temp = L[i]
        L[i] = x
        L[index] = temp

print("\n Sorted List is: "end="")
for i in range(elements):
    print(L[i], end=" ")


WRITTEN CODE FOR THE ABOVE PROGRAM :


L = []

print("Enter the size of list: ", end="")

elements = int(input())

print("Enter", elements, "numbers for the list: ", end="")

for i in range(elements):

  L.append(int(input()))


for i in range(elements-1):

    ch = 0

    x = L[i]

    for j in range(i+1, elements):

        if x > L[j]:

            x = L[j]

            ch = ch + 1

            index = j

    if ch != 0:

        temp = L[i]

        L[i] = x

        L[index] = temp


print("\n Sorted List is: ", end="")

for i in range(elements):

    print(L[i], end=" ")


OUTPUT :





Different types of insertion sort python programs => click here


Different types of bubble sort python programs => click here 


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