From 146ba553363ed540b4b4835ccfcbc7ab9058257e Mon Sep 17 00:00:00 2001 From: vkyadavdel <76583450+vkyadavdel@users.noreply.github.com> Date: Sat, 2 Oct 2021 09:09:08 +0530 Subject: [PATCH] Create Selection Sort.py --- Selection Sort.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Selection Sort.py diff --git a/Selection Sort.py b/Selection Sort.py new file mode 100644 index 0000000..ae9438e --- /dev/null +++ b/Selection Sort.py @@ -0,0 +1,24 @@ +# Selection sort in Python + + +def selectionSort(array, size): + + for step in range(size): + min_idx = step + + for i in range(step + 1, size): + + # to sort in descending order, change > to < in this line + # select the minimum element in each loop + if array[i] < array[min_idx]: + min_idx = i + + # put min at the correct position + (array[step], array[min_idx]) = (array[min_idx], array[step]) + + +data = [-2, 45, 0, 11, -9] +size = len(data) +selectionSort(data, size) +print('Sorted Array in Ascending Order:') +print(data)