From 54abd31c078f59ee2c89f65cb27be0e4b98a6503 Mon Sep 17 00:00:00 2001 From: Daksheshapkare <113005270+Daksheshapkare@users.noreply.github.com> Date: Mon, 2 Oct 2023 20:49:01 +0530 Subject: [PATCH] Create Selection sort in python.py Hacktoberfest --- Python/Selection sort in python.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Python/Selection sort in python.py diff --git a/Python/Selection sort in python.py b/Python/Selection sort in python.py new file mode 100644 index 00000000..ae9438e9 --- /dev/null +++ b/Python/Selection sort in python.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)