In this post, you will learn a python program to sort a list (in ascending or descending order) without using the sort() function.

we should write this program in two different way:

  1. by taking user input.
  2. by using function (User define function)

But before writing this program you should Know about:

  1. if-else
  2. for loop
  3. Python list

So let’s start writing program to sorting a list in both assending or descending order.

Sort a list in ascending order By taking user input

Question may be asked like: Write a program to sort a list in ascending order without using the sort() function.

Source code:

len_my_list = int(input("Enter length of list :"))

my_list = []

for k in range(len_my_list):
    element = int(input(f"Enter {k+1} number: "))
    my_list.append(element)


print(f"Before sorting: {my_list}")

tem = 0
for i in range(0,len(my_list)):
    for j in range(0,len(my_list)):
        if my_list[i]<my_list[j]:
            tem = my_list[i]
            my_list[i] = my_list[j]
            my_list[j] = tem

print(f"After sorting: {my_list}")

Output:

Enter length of list :5
Enter 1 number: 69
Enter 2 number: 96
Enter 3 number: 86
Enter 4 number: 85
Enter 5 number: 153
Before sorting: [69, 96, 86, 85, 153]
After sorting: [69, 85, 86, 96, 153]

Using function (User define function)

Question may be asked like: Write a program to sort a list in ascending order by using a user-defined function

Source code:

def sort_list(my_list):
    tem = 0
    for i in range(0,len(my_list)):
        for j in range(0,len(my_list)):
            if my_list[i]<my_list[j]:
                tem = my_list[i]
                my_list[i] = my_list[j]
                my_list[j] = tem

    return f"After sorting: {my_list}"


length = int(input("Enter length of list :"))

my_list = []

for k in range(length):
    element = int(input(f"Enter {k+1} number: "))
    my_list.append(element)

print(f"Before sorting: {my_list}")

print(sort_list(my_list=my_list))

Output:

Enter length of list :5
Enter 1 number: 34
Enter 2 number: 21
Enter 3 number: 56
Enter 4 number: 78
Enter 5 number: 12
Before sorting: [34, 21, 56, 78, 12]
After sorting: [12, 21, 34, 56, 78]

Sort a list in descending order By taking user input

Question may be asked like: Write a program to sort a list in descending order without using the sort() function.

Source code:

# only difference between ascending and descending order programs is
#  (my_list[i]>my_list[j]) for descending order
#  (my_list[i]<my_list[j]) for ascending order

len_my_list = int(input("Enter length of list :"))

my_list = []

for k in range(len_my_list):
    element = int(input(f"Enter {k+1} number: "))
    my_list.append(element)


print(f"Before sorting: {my_list}")

tem = 0
for i in range(0,len(my_list)):
    for j in range(0,len(my_list)):
        if my_list[i]>my_list[j]:
            tem = my_list[i]
            my_list[i] = my_list[j]
            my_list[j] = tem

print(f"After sorting: {my_list}")

Output:

Enter length of list :7
Enter 1 number: 45
Enter 2 number: 12
Enter 3 number: 89
Enter 4 number: 90
Enter 5 number: 56
Enter 6 number: 34
Enter 7 number: 100
Before sorting: [45, 12, 89, 90, 56, 34, 100]
After sorting: [100, 90, 89, 56, 45, 34, 12]

Using function (User define function)

Question may be asked like: Write a function to sort a list in descending order by using a user-defined function.

Source code:

# only difference between ascending and descending order programs is
#  (my_list[i]>my_list[j]) for descending order
#  (my_list[i]<my_list[j]) for ascending order

def sort_list(my_list):
    tem = 0
    for i in range(0,len(my_list)):
        for j in range(0,len(my_list)):
            if my_list[i]>my_list[j]:
                tem = my_list[i]
                my_list[i] = my_list[j]
                my_list[j] = tem

    return f"After sorting: {my_list}"


length = int(input("Enter length of list :"))

my_list = []

for k in range(length):
    element = int(input(f"Enter {k+1} number: "))
    my_list.append(element)

print(f"Before sorting: {my_list}")

print(sort_list(my_list=my_list))

Output:

nter length of list :7
Enter 1 number: 34
Enter 2 number: 89
Enter 3 number: 67
Enter 4 number: 45
Enter 5 number: 11
Enter 6 number: 88
Enter 7 number: 55
Before sorting: [34, 89, 67, 45, 11, 88, 55]
After sorting: [89, 88, 67, 55, 45, 34, 11]

Hope this post add some value to your life – thankyou

Author

Hi, I'm Yagyavendra Tiwari, a computer engineer with a strong passion for programming. I'm excited to share my programming knowledge with everyone here and help educate others in this field.

Write A Comment