Skip to content

Instantly share code, notes, and snippets.

@lidio601
Last active August 29, 2015 14:27
Show Gist options
  • Save lidio601/9a40f3107ca971c18b07 to your computer and use it in GitHub Desktop.
Save lidio601/9a40f3107ca971c18b07 to your computer and use it in GitHub Desktop.
Sample implementation of the Naive sorting algorithm ( O(N^2/2) order of complexity )
import random
__author__ = 'fabiocigliano'
def genarray():
n = int(random.random() * 10) + 1
ris = range(n)
random.shuffle(ris)
return ris
def scambia(array, i, j):
temp = array[i]
array[i] = array[j]
array[j] = temp
def naive_sort(a):
for i in range(len(a)):
max = 0
for j in range(1, len(a)-i):
if a[j] > a[max]:
max = j
# print "max", max, a[max]
scambia(a, len(a)-i-1, max)
# break
return a
if __name__ == "__main__":
array = genarray()
print "test input array: ", array
array = naive_sort(array)
print "test output array:", array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment