Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import random
#the sort function
def sort(array):
#the length of the array
length = len(array)
#for loop which ranges from
#1 to the length of the array
#this loop is to cover each and
#every number and ensure its
#position
for i in range(1, length):
#assign the value of i to j
#for further manipuation
#j is used to traverse in the opposite direction
#and used to swap the number which don't belong
j = i
#use j only until the value reaches 0
#don't exceed or that will cause an
#out of bound error
while j > 0:
#and like the traditional bubblesort
#swap if the number is greater or
#smaller (requirement)
if array[j] < array[j-1]:
#swapper
temp = array[j]
array[j] = array[j-1]
array[j-1] = temp
#decrement j
j -= 1
#return array (obviously)
return array
#the main function
def main():
#declaring an array
array = [3,2,6,1,4,9,6,8,10,22]
#printing the array
print array
#calling the sort function
print sort(array)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.