Selection Sort
Algorithm: Selection Sort
Input: A list of n numbers that are not sorted
Output: The list arranged in increasing (ascending) order
________________________________________
Steps
- Start
- Take the list of numbers.
- Begin with the first position in the list.
- Repeat the following steps until the second last position:
- Assume the value at the current position is the smallest.
- Look through all the numbers to the right to find the actual smallest value.
- If you find a number smaller than the assumed value, remember its position.
- If the smallest value is not already in the current position, swap the two values.
- Move to the next position and repeat.
- When finished, the list will be sorted.
- Show the sorted list.
- 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