Thursday, March 31, 2016

selection sort - O(n*n)

#!/usr/bin/python
def selection_sort(arr):
    for i in range(0,len(arr)):
        min_index = i
        ## find minimum element in remaining
        ## array
        for j in range(i+1, len(arr)):
            if arr[j] < arr[min_index]:
                min_index = j
        ## swap the minimum element with arr[i]
        arr[i], arr[min_index] = arr[min_index], arr[i]

arr = [3, 0, 9, 4, 2, 9, 1, 0]
selection_sort(arr)
print arr

No comments:

Post a Comment