Monday 24 August 2015

Selection sort program in python

 Python program for selection sort algorithm

 
 

# Selection sort method to sort an array
def selectionsort( aList ):
  for i in range( len( aList ) ):
    least = i
    for k in range( i + 1 , len( aList ) ):
      if aList[k] < aList[least]:
        least = k

    swap( aList, least, i )

# Swap method 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 insertion sort
A=[44,21,3,88,12,65,4]
# Calling selectionsort() function
selectionsort(A)
# printing the sorted array
print A

0 comments:

Post a Comment

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel