From a4b675ac59695392195c25628175571cd2793346 Mon Sep 17 00:00:00 2001 From: Abdullah Rizwan <143324825+abdullahmir007@users.noreply.github.com> Date: Sat, 28 Oct 2023 20:11:31 +0500 Subject: [PATCH] Create Selection sort.py --- Selection sort.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Selection sort.py diff --git a/Selection sort.py b/Selection sort.py new file mode 100644 index 00000000..72f62462 --- /dev/null +++ b/Selection sort.py @@ -0,0 +1,21 @@ +def selection_sort(arr): + n = len(arr) + # Traverse through all array elements + for i in range(n): + # Find the minimum element in the remaining unsorted array + min_index = i + for j in range(i+1, n): + if arr[j] < arr[min_index]: + min_index = j + # Swap the found minimum element with the first element + arr[i], arr[min_index] = arr[min_index], arr[i] + +# Taking user input for the list +user_input = input("Enter elements of the list separated by space: ") +input_list = list(map(int, user_input.split())) + +# Sorting the user input list using selection_sort function +selection_sort(input_list) + +# Displaying the sorted list +print("Sorted Array:", input_list)