Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python sort an array

def bubble_sort(nums):
    n = len(nums)
    for i in range(n):
        swapped = False
        for j in range(1, n - i):
            if nums[j] < nums[j - 1]:
                nums[j], nums[j - 1] = nums[j - 1], nums[j]
                swapped = True
        if not swapped: break
    return nums
print(bubble_sort([9, 8, 7, 6, 5, 4, 3, 2, 1]))
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #python #sort #array
ADD COMMENT
Topic
Name
4+5 =