Table of Contents                    
                show            
            
- The selection sort algorithm sorts an array by repeatedly finding the minimum element from unsorted part and put it at the beginning. The algorithm maintains two subarrays in a given array.
- The subarray which is already sorted
- Remaining subarray which is unsorted
 
- In every iteration of selection sort, the minimum element from the unsorted subarray is picked and moved to the sorted subarray
- Selection sort is an in-place algorithm meaning you won’t need to allocate additional lists
- While slow, it is still used as the main sorting algorithm in systems where memory is limited
Get the elements from user
#Get the elements from user
n = int(input("Enter the number of elements:"))
A=[]
print("Enter the elements:")
for i in range(n):
	A.append(int(input()))
# Traverse through all array elements 
for i in range(len(A)): 
      
    # Find the minimum element in remaining  
    # unsorted array 
    min_idx = i 
    for j in range(i+1, len(A)): 
        if A[min_idx] > A[j]: 
            min_idx = j 
              
    # Swap the found minimum element with  
    # the first element         
    A[i], A[min_idx] = A[min_idx], A[i] 
print("Sorted Array:")
for i in range(n):
	print("At index",i,"the element is:",A[i])Output
Enter the number of elements: 4
Enter the elements:
21
-1
4
3
Sorted Array:
At index 0 the element is: -1
At index 1 the element is: 3
At index 2 the element is: 4
At index 3 the element is: 21Example:
Views: 1