Python Program To Sort Array Elements In Ascending Order

Python Program To Sort Array

Method-01

Using predefine sort() function

def sortArray(array):
    array.sort()
    print("Sorted Array: ",array)

    
A = [30, 50, 10, 70, 90, 120]

sortArray(A)

Output:

Sorted Array:  [10, 30, 50, 70, 90, 120]

 

Method-02

Using bubble sort

def sortArray(array):
    for i in range(len(array)):
        for j in range(len(array)):
            if(array[i] < array[j]):
                array[i], array[j] = array[j], array[i] 
    print("Sorted Array: ",array)
            

A = [30, 50, 10, 70, 90, 120]
sortArray(A)

Output:

Sorted Array:  [10, 30, 50, 70, 90, 120]

Leave a comment