selection sort statements in python code example

Example 1: selection sort python

def selection(s):
    for i in range(0,len(s)-1):
        p=0
        mini=s[-1]
        for j in range(i,len(s)):
            if s[j]<=mini:
                mini=s[j]
                p=j
        s[i],s[p]=s[p],s[i]
print(s)
selection([2,3,4,2,1,1,1,2])

Example 2: selection sort python

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]))

Tags:

Java Example