How to Sort Numbers in Python without sort Function

Today we are going out to Sort Numbers in Python without sort Function. There is a similar Kata in Codewars to Sort only odd Numbers without sort Function in Python. The link of the similar Kata (sorting only odd numbers) is given HERE

Task:

You will be given an array of numbers. You have to sort the numbers in ascending order.

Python has built in sort function which has made us lazy to think that how can we sort a list in python without using that function.

how to sort numbers in python without sort function

Now let’s get to the coding part to extract the Domain out of the URL.

Code

def sort_array(source_array):

    for i in range(len(source_array)):

        for j in range(len(source_array)):

            if source_array[i] < source_array[j]:

                k = source_array[i]
                source_array[i] = source_array[j]
                source_array[j] = k

    return source_array
            
            
            
    # Return a sorted array.


Output

Input: [0, 4, 2, 1, 5]
Output: [0, 1, 2, 4, 5]

Input: [ ]
Output: [ ]

Input: [6, 5, 4, 3, 2, 1, 0]
Output: [0, 1, 2, 3, 4, 5, 6]

READ MORE

Python-related posts Visit HERE

C++ related posts Visit HERE

Databases related posts Visit HERE

Data Structures related posts visit HERE

Algorithms related posts visit HERE

Data Science related posts visit HERE