;

Python Program to Sort a List in an Ascending Order


Tutorialsrack 04/05/2020 Python

In this Python program, we will learn how to sort a list in ascending order. In this program, for sorting a list we used the python built-in function such as sort(), sorted() and without using the sorting function.

Here is the source code of the program to sort a list in ascending order.

Program 1: Python Program to Sort a List in an Ascending Order Using sort() Method

In this program, we used the python built-in function sort() to sort the list in ascending order by default.

The sort() function takes two parameters: 

reverse = the first parameter is optional.if reverse = True then it returns a sorted list in descending order.

Key = second Parameter. It is also optional and this function used to specify the sorting criteria(s)

Python Program to Sort a List in an Ascending Order Using sort() Method
# Python Program to Sort a List in an Ascending Order Using sort() Method

NumList = [12,45,87,49,56,46]

# Print the List
print("\nElement Before Sorting is: ", NumList)

NumList.sort()
print("\nElement After Sorting List in Ascending Order is: \n", NumList)
Output

Element Before Sorting is:  [12, 45, 87, 49, 56, 46]

 

Element After Sorting List in Ascending Order is: 

 [12, 45, 46, 49, 56, 87]

Program 2: Python Program to Sort a List in an Ascending Order Using sorted() Method

In this program, we used the python built-in sorted() function to sort the list in ascending order. Python lists have a built-in sorted() function that builds a new sorted list from an iterable. sorted() have a key parameter to specify a function to be called on each list element prior to making comparisons.

Python Program to Sort a List in an Ascending Order Using sorted() Method
# Python Program to Sort a List in an Ascending Order Using sorted() Method

NumList = [12,45,87,49,56,46]

# Print the List
print("\nElement Before Sorting is: ", NumList)

new_NumList=sorted(NumList)
print("\nElement After Sorting List in Ascending Order is: \n", new_NumList)

Output

Element Before Sorting is:  [12, 45, 87, 49, 56, 46]

 

Element After Sorting List in Ascending Order is: 

 [12, 45, 46, 49, 56, 87]

Program 3: Python Program to Sort a List in an Ascending Order Using For Loop

Python Program to Sort a List in an Ascending Order Using For Loop
# Python Program to Sort a List in an Ascending Order Using For Loop

NumList = [12,45,87,49,56,46]

# Print the List
print("\nElement Before Sorting is: ", NumList)

Number=len(NumList)
for i in range (Number):
    for j in range(i + 1, Number):
        if(NumList[i] > NumList[j]):
            temp = NumList[i]
            NumList[i] = NumList[j]
            NumList[j] = temp

print("\nElement After Sorting List in Ascending Order is: \n", NumList)
Output

Element Before Sorting is:  [12, 45, 87, 49, 56, 46]

 

Element After Sorting List in Ascending Order is: 

 [12, 45, 46, 49, 56, 87]

Program 4: Python Program to Sort a List in an Ascending Order Using While Loop

Python Program to Sort a List in an Ascending Order Using While Loop
# Python Program to Sort a List in Ascending Order Using While Loop

NumList = [12,45,87,49,56,46]

# Print the List
print("\nElement Before Sorting is: ", NumList)
Number=len(NumList)

i = 0
while(i < Number):
    j = i + 1
    while(j < Number):
        if(NumList[i] > NumList[j]):
            temp = NumList[i]
            NumList[i] = NumList[j]
            NumList[j] = temp
        j = j + 1
    i = i + 1

print("\nElement After Sorting List in Ascending Order is: \n", NumList)
Output

Element Before Sorting is:  [12, 45, 87, 49, 56, 46]

 

Element After Sorting List in Ascending Order is: 

 [12, 45, 46, 49, 56, 87]


Related Posts



Comments

Recent Posts
Tags