# Implementation of Selection Sort Algorithm in Python

Selection sort is a sorting algorithm, specifically an in-place comparison sort. It sorts an array by finding the minimum element in every iteration. This algorithm finds the lowest element in the array and place it at first position in first iteration, second lowest element in second iteration and so on. Selection sort algorithm is given below with example. python program is also given below to implement Selection sort algorithm.

The best thing about Selection Sort is that it takes maximum O(n) swaps unlike Bubble sort which may take more swap operations. Therfore, this algorithm is efficient when memory writing operation is costly.

Time complexity of Selection Sort is: O(n^2)

Algorithm:

step 1: For i = 1 to n-1   /*where n is the size of the array */
step 2:        set: min_index=1    /*here we took starting index as 1 */
step 3:        For j=i+1 to n
step 4:                    If arr[j]<arr[min_index]
step 5:                              set:  min_index=j
step 6:                    EndIf
step 7:        EndFor
step 8:        swap  (arr[i] and swap[min_index])
step 9: EndFor
step 10. Exit.

Example:

Let arr[] ={ 12, 4, 6, 13, 2}

Pass 1, i=0:  { 2 , 4, 6, 13, 12 }
Pass 2, i=1:  { 2 , 4, 6, 13, 12 }
Pass 3, i=2:  { 2 , 4, 6, 13, 12 }
Pass 4, i=3:  { 2 , 4, 6, 12, 13 }

Python Program for Implementation of Selection Sort:

```def selection_sort(listing):
for i in range(0,len(listing)-1):
minimum=i
for j in range(i+1,len(listing)):
if(listing[minimum]>listing[j]):
minimum=j
temp=listing[i]
listing[i]=listing[minimum]
listing[minimum]=temp
return listing
listing=[]
print("\nEnter the elements: \n")
listing=list(map(int, raw_input().split()))
listing=selection_sort(listing)
print("\nElements after sorting: \n")
for i in range(0,len(listing)):
print listing[i],```

OUTPUT: