From 1cb6a7b9ff81fe13f96894bdcb7170eb327274c9 Mon Sep 17 00:00:00 2001 From: Kumar Abhishek Date: Sat, 19 Oct 2024 12:03:19 +0530 Subject: [PATCH 1/2] selection__sort --- sorts/selection__sort.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 sorts/selection__sort.py diff --git a/sorts/selection__sort.py b/sorts/selection__sort.py new file mode 100644 index 000000000000..75a37e315a4b --- /dev/null +++ b/sorts/selection__sort.py @@ -0,0 +1,21 @@ +def selectionSort(array, size): + + for s in range(size): + min_idx = s + + for i in range(s + 1, size): + + # For sorting in descending order + # for minimum element in each loop + if array[i] < array[min_idx]: + min_idx = i + + # Arranging min at the correct position + (array[s], array[min_idx]) = (array[min_idx], array[s]) + +data = [ 7, 2, 1, 6 ] +size = len(data) +selectionSort(data, size) + +print('Sorted Array in Ascending Order is :') +print(data) From d829068af3061f010c16aca1e9f115a19873c5bd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 19 Oct 2024 06:34:12 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/selection__sort.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/sorts/selection__sort.py b/sorts/selection__sort.py index 75a37e315a4b..3fcd6fb0ef1b 100644 --- a/sorts/selection__sort.py +++ b/sorts/selection__sort.py @@ -1,10 +1,8 @@ def selectionSort(array, size): - for s in range(size): min_idx = s - + for i in range(s + 1, size): - # For sorting in descending order # for minimum element in each loop if array[i] < array[min_idx]: @@ -13,9 +11,10 @@ def selectionSort(array, size): # Arranging min at the correct position (array[s], array[min_idx]) = (array[min_idx], array[s]) -data = [ 7, 2, 1, 6 ] + +data = [7, 2, 1, 6] size = len(data) selectionSort(data, size) -print('Sorted Array in Ascending Order is :') +print("Sorted Array in Ascending Order is :") print(data)