Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Hostgator 1 Penny

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Distribute your Resume to 500+ Job Boards & Recruiters


7,621 questions

10,674 answers

573 users

How to implement selection sort in Python

1 Answer

0 votes
import random


def selection_sort(a_list):
    for index_current in range(len(a_list)-1, 0, -1):
        index_max = 0
        for index_next in range(1, index_current + 1):
            if a_list[index_next] > a_list[index_max]:
                index_max = index_next

        tmp = a_list[index_current]
        a_list[index_current] = a_list[index_max]
        a_list[index_max] = tmp


the_numbers = [0] * 15
size = len(the_numbers)

for i in range(size):
    the_numbers[i] = random.randint(1, 1000)

selection_sort(the_numbers)

print(the_numbers)

'''
run:

[35, 64, 72, 221, 471, 490, 583, 640, 670, 673, 739, 821, 934, 985, 997]

'''

 




answered May 13 by avibootz
...