From 5154204f11eac5a7be7a42d008ef5ed7825a0092 Mon Sep 17 00:00:00 2001 From: Kumar Abhishek Date: Sat, 19 Oct 2024 11:56:17 +0530 Subject: [PATCH] Selection Sort --- sorts/Selection Sort | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 sorts/Selection Sort diff --git a/sorts/Selection Sort b/sorts/Selection Sort new file mode 100644 index 000000000000..75a37e315a4b --- /dev/null +++ b/sorts/Selection Sort @@ -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)