Search This Blog

# Sorting the list without using inbuilt sort () in Python 3


num = [1,2,3,4]

def sort4(n): #Function defined

    # Nested loop  for checking the order 

    for i in range ( len ( n ) ):

        for j in range ( i + 1, len ( n ) ):

            if n[i] > n[j]:

                n[i], n[j] = n[j], n[i]

    return n  # Returning the sorted list

i = 0

while i < 4:

    num[i] = input("Enter the %s number: " % (i+1) )

    i += 1

print('\nThe input List is : \n%s' %num)
 
# Printing the function return.
print ('\nList after sorting the element is: \n %s' %sort4 ( num ) )  

Output
Enter the 1 number: 98
Enter the 2 number: 87
Enter the 3 number: 76
Enter the 4 number: 0

The input List is:
['98', '87', '76', '0']

List after sorting the element is:
 ['0', '76', '87', '98']


Post a Comment

0 Comments