## Lekcja – Sortowanie przez wybiernie (select sort)

```def sort_selection(list):

for run in range(len(list)):
min_index = run
for i in range(run+1, len(list)):
if list[i] < list[min_index]:
min_index = i
list[run], list[min_index] = list[min_index], list[run]
print(f'Run {run}, list {list}')

list = [1,3,9,2,0,6,4,7,5,3]
sort_selection(list)

list
```

## Rozwiązanie

```import time
import random

#declarations
max_limit = 10000

my_list1 = [random.randint(0, max_limit) for i in range(max_limit)]
my_list2 = my_list1.copy()
my_list3 = my_list1.copy()
my_list4 = my_list1.copy()

# functions

def sort_bubble(list):

is_change = True

while is_change:
is_change = False
for i in range(len(list)-1):
if list[i] > list[i+1]:
list[i],list[i+1] = list[i+1],list[i]
is_change = True

def sort_bubble2(list):

# don't compare values at the end of the list - they are already sorted
max_index = len(list) - 1

for max_not_sorted_index in range(max_index,0,-1):
is_change = False
for i in range(max_not_sorted_index):
if list[i] > list[i+1]:
list[i],list[i+1] = list[i+1],list[i]
is_change = True

if not is_change:
break

def sort_insert(list):

# starting from 1, because this first element is already "sorted"
# all items need to be put in place, so this loop needs to iterate for every element
for sort_border in range(1,len(list)):
# we will try to put the new item into the sublist of sorted elements
curr_idx = sort_border - 1
value = list[curr_idx+1] # next value to be inserted into data

while list[curr_idx] > value and curr_idx >= 0:
list[curr_idx+1] = list[curr_idx]
curr_idx = curr_idx-1

#now the good position has been found and we put there the considered element
list[curr_idx+1] = value

def sort_select(list):

for run in range(len(list)):
min_index = run
for i in range(run+1, len(list)):
if list[i] < list[min_index]:
min_index = i
list[run], list[min_index] = list[min_index], list[run]

# checking time - case sort_bubble

start = time.time()
sort_bubble(my_list1)
stop = time.time()
print(f'Sorting duration for function sort_bubble:    {stop - start}')

# checking time - case sort_bubble2

start = time.time()
sort_bubble2(my_list2)
stop = time.time()
print(f'Sorting duration for function sort_bubble2:   {stop - start}')

# checking time - case  sort_insert

start = time.time()
sort_insert(my_list3)
stop = time.time()
print(f'Sorting duration for function sort_insert:    {stop - start}')

# checking time - case  sort_select

start = time.time()
sort_select(my_list4)
stop = time.time()
print(f'Sorting duration for function sort_select:    {stop - start}')
```