Monday 24 August 2015

Bubble sort program in python


#function for bubble sort which takes array as parameter
def bubblesort( A ):
 for i in range( len( A ) ):
   for k in range( len( A ) - 1, i, -1 ):
     if ( A[k] < A[k - 1] ):
       swap( A, k, k - 1 )
 
# Swap function to swap the values
def swap( A, x, y ):
  tmp = A[x]
  A[x] = A[y]
  A[y] = tmp



# Example to sort an array using bubble sort
A=[44,21,3,88,12,65,4]
# Calling bs() function
bubblesort(A)
# printing the sorted array
print A

0 comments:

Post a Comment

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel