Advertisement
artyomblack

selectionSort (simple)

Apr 6th, 2020
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.00 KB | None | 0 0
  1. #  ==========================
  2. # Ф-я поиска наименьшнго эл-та
  3. #  ==========================
  4.  
  5. def findSmallest(arr):
  6.     smallest = arr[0]                  #для хранения наименьшего значения
  7.     smallestIndex = 0                  #Для хранения индекса наименьшего значения
  8.     for i in range (1, len(arr)):
  9.         if arr[i] < smallest:
  10.             smallest = arr[i]
  11.             smallestIndex = i
  12.     return smallestIndex
  13.  
  14. #  ================
  15. # Сортировка выбором
  16. #  ================
  17.  
  18. def selectionSort(arr):                 #Сортирует массив
  19.     newArr = []
  20.     for i in range (len(arr)):
  21.         smallest = findSmallest(arr)     #находит наименьший элемент в массиве
  22.         newArr.append(arr.pop(smallest))#и добавляет его в новый массив
  23.     return newArr
  24.    
  25.    
  26. # Пример
  27. print(selectionSort([5, 3, 6, 2, 10]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement