From 66259ddf90c77da1ac70160b773c4cd0128485dc Mon Sep 17 00:00:00 2001 From: Vijay331 <119308936+Viju331@users.noreply.github.com> Date: Tue, 31 Oct 2023 12:49:36 +0530 Subject: [PATCH] Create Selection.py --- Selection.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Selection.py diff --git a/Selection.py b/Selection.py new file mode 100644 index 0000000..ab901d6 --- /dev/null +++ b/Selection.py @@ -0,0 +1,20 @@ +# Selection sort in Python +# time complexity O(n*n) +#sorting by finding min_index +def selectionSort(array, size): + + for ind in range(size): + min_index = ind + + for j in range(ind + 1, size): + # select the minimum element in every iteration + if array[j] < array[min_index]: + min_index = j + # swapping the elements to sort the array + (array[ind], array[min_index]) = (array[min_index], array[ind]) + +arr = [-2, 45, 0, 11, -9,88,-97,-202,747] +size = len(arr) +selectionSort(arr, size) +print('The array after sorting in Ascending Order by selection sort is:') +print(arr)