Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,990 questions

51,935 answers

573 users

How to implement selection sort in Python

1 Answer

0 votes
import random
 
def selection_sort(lst):
    size = len(lst)
    
    # range(start, stop, step)
    
    for i in range(size):
        min_index = i
 
        for j in range(i + 1, size):
            if lst[j] < lst[min_index]:
                min_index = j
        
        lst[i], lst[min_index] = lst[min_index], lst[i]

numbers = [0] * 15
size = len(numbers)
 
for i in range(size):
    numbers[i] = random.randint(1, 1000)
 
selection_sort(numbers)
 
print(numbers)
 
 
 
'''
run:
 
[57, 76, 90, 190, 225, 251, 320, 405, 517, 613, 669, 753, 784, 899, 934]
 
'''

 



answered May 13, 2018 by avibootz
edited Feb 20, 2024 by avibootz

Related questions

2 answers 544 views
544 views asked May 13, 2018 by avibootz
1 answer 177 views
1 answer 202 views
202 views asked May 12, 2018 by avibootz
1 answer 227 views
227 views asked May 12, 2018 by avibootz
1 answer 175 views
175 views asked May 12, 2018 by avibootz
1 answer 175 views
175 views asked May 12, 2018 by avibootz
1 answer 185 views
185 views asked May 12, 2018 by avibootz
...