From 318a0fc377936cd37c8958214f0acc6af7dfa30e Mon Sep 17 00:00:00 2001 From: UdeeshaRukshan Date: Sat, 21 Oct 2023 22:04:36 +0530 Subject: [PATCH] Selection sort algorith using python programming --- Python/SelectionSort.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Python/SelectionSort.py diff --git a/Python/SelectionSort.py b/Python/SelectionSort.py new file mode 100644 index 00000000..f54e9be3 --- /dev/null +++ b/Python/SelectionSort.py @@ -0,0 +1,20 @@ +def selection_sort(arr): + # Get the length of the array + n = len(arr) + + # Loop through the array + for i in range(n): + # Assume the current index is the minimum + min_idx = i + + # Loop through the unsorted part of the array + for j in range(i+1, n): + # If a smaller element is found, update the minimum index + if arr[j] < arr[min_idx]: + min_idx = j + + # Swap the minimum element with the first element of the unsorted part + arr[i], arr[min_idx] = arr[min_idx], arr[i] + + # Return the sorted array + return arr \ No newline at end of file