In this post we understand about selection sort with a example of python program
First we understand about sorting
Ordering data in an increasing or decreasing manner is called sorting
Selection Sort
It means that it check first element with second element if second element is greater than it swap .
Input :
x= [10,20,90,100,80,50,30,40 ]
Output :
x=[10,20,30,40,50,80,90,100 ]
Algorithm of Selection Sort
- First we take a list ( list is not in ascending order ).
- Than we use two for loop.
- First for first element and second for second element .
- Than we check the condition after that we swap them .
- at last we print the list in ascending order.
# Selection Sort Program x= [10,90,80,30,40,60,20,50,100 ,70] # list is not ascending order for i in range(0,len(x)-1): # for first element for j in range(i+1, len(x)): # for second element if x[i]>x[j]: # condition for checking c= x[i] # where c is tem. variable x[i]= x[j] # Swaping process x[j]=c print(x) Output : [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]