Selection sort

Selection Sort


Algorithm: Selection Sort

Input: A list of n numbers that are not sorted

Output: The list arranged in increasing (ascending) order

________________________________________

Steps

  1. Start
  2. Take the list of numbers.
  3. Begin with the first position in the list.
  4. Repeat the following steps until the second last position:
    1.        Assume the value at the current position is the smallest.
    2.        Look through all the numbers to the right to find the actual smallest value.
    3.        If you find a number smaller than the assumed value, remember its position.
    4.        If the smallest value is not already in the current position, swap the two values.
    5.        Move to the next position and repeat.
  5. When finished, the list will be sorted.
  6. Show the sorted list.
  7. Stop

 


# Selection sort


def fnSelectionsort(L):

for i in range(0,len(L)-1):

min_pos=i

min_ele=min(L[i+1:])

if(L[min_pos] > min_ele):

min_ele_pos = L.index(min_ele)

L[min_pos],L[min_ele_pos]=L[min_ele_pos],L[min_pos]

L = eval(input("Enter list of elements : "))

fnSelectionsort(L)

print(L)


"""

Sample output

>python Selectionsort.py

Enter list of elements : [34, 8, 64, 51, 32, 21]

[8, 21, 32, 34, 51, 64]

"""

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu