Wednesday, 7 October 2015

bubble sort

# Program Name: Bubble Sort using python

#array[] contains the element that we want to sort
array = [2,5,4,1,7]

#BUBBLE SORT LOGIC

swapped = True
while (swapped):
     swapped = False
     for i in range(0,len(array)-1):
         if (array[i] > array[i+1]):
             temp = array[i]
             array[i] = array[i+1]
             array[i+1] = temp
             swapped = True

# PRINT THE SORTED ARRAY

print (array)

No comments:

Post a Comment