Hello and welcome back, in this Python solution article we will sort a number list with a Python function. If the function passes in an empty array or a none value then it should return an empty array or else it will sort the list and return the number list in ascending order!

Our strategy here is to compare the first number with the remaining numbers from the list and to put the smallest number in the list to the head of the list. Next we will compare the second, third and the remaining numbers with the numbers after it and placed the next smallest number in the correct position of the array.

def solution(nums):

    if nums == None:
        return []

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

    return nums

The sorting solution above is nothing different than the solution for Java or C++ so you should be very familiar with it.

Please follow and like us:

Leave a Reply

Your email address will not be published. Required fields are marked *