Q:

selection sort python

def selection_sort(arr):        
    for i in range(len(arr)):
        minimum = i
        
        for j in range(i + 1, len(arr)):
            # Select the smallest value
            if arr[j] < arr[minimum]:
                minimum = j

        # Place it at the front of the 
        # sorted end of the array
        arr[minimum], arr[i] = arr[i], arr[minimum]
            
    return arr
4
def selection_sort(lst):
    empty_lst = []
    x = len(lst) - 1
    while x>=0:
        for i in range(len(lst)):
            if lst[i] <= lst[0]:
                lst[0],lst[i] = lst[i],lst[0]
                # this part compares the number in first index and numbers after the first index.
        g = lst.pop(0)
        empty_lst.append(g)
        x -= 1
    return empty_lst
    
print(selection_sort([2,3,4,2,1,1,1,2]))
1
# Python program for implementation of Selection 
# Sort 
import sys 
A = [64, 25, 12, 22, 11] 
  
# 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] 
  
# Driver code to test above 
print ("Sorted array") 
for i in range(len(A)): 
    print("%d" %A[i]),  
0

New to Communities?

Join the community