From 254b1bb127d40d76eb9c40f79abd408375f3eac6 Mon Sep 17 00:00:00 2001 From: Kaung Hein Htet Date: Mon, 22 Jan 2024 11:53:30 +0630 Subject: [PATCH] solved selection sort --- src/sorting/python/selection_sort/README.md | 86 +++++++++++++++++++ .../selection_sort/src/selection_sort.py | 19 ++++ 2 files changed, 105 insertions(+) diff --git a/src/sorting/python/selection_sort/README.md b/src/sorting/python/selection_sort/README.md index e69de29..b198acb 100644 --- a/src/sorting/python/selection_sort/README.md +++ b/src/sorting/python/selection_sort/README.md @@ -0,0 +1,86 @@ +# Selection Sort Algorithm + +## Overview + +This repository contains a Python implementation of the Selection Sort algorithm. Selection Sort is a simple sorting algorithm that divides the input array into a sorted and an unsorted region. The algorithm repeatedly selects the minimum element from the unsorted region and swaps it with the first element of the unsorted region, effectively expanding the sorted region. + +### selection_sort.py + +This module defines a function for sorting a list in accending order. + +## Function Signature + +```python +def selectionsort_v1(array: List[int]) -> List[int]: + # Implementation details... +``` + +#### Parameters: + +list (int): A list containing unordered integers + + +#### Returns: + +list (int): A list containing ascending ordered integers + +## Behavior: + +The method takes the smallest (or largest) element from the unsorted portion of the list and swaps it with the first member in the unsorted section. This method is repeated for the remaining unsorted list sections until it is completely sorted. + + +## Implementation + +The selection sort algorithm is implemented in the function `selectionsort_v1` defined in the file `selection_sort.py`. + + + +## Usage + +To use the selection sort algorithm, follow these steps: + +* Import the selectionsort_v1 function from the selection_sort module: +```python +from selection_sort import selectionsort_v1 +``` +* Create an unsorted list of integers: +```python +unsorted_array = [64, 25, 12, 22, 11] +``` + +* Call the selectionsort_v1 function, passing the unsorted list as an argument: +```python +sorted_array = selectionsort_v1(unsorted_array) +``` + +The function will return a new list containing the sorted elements. + + +Example + +```python +from selection_sort import selectionsort_v1 + +# Create an unsorted list +unsorted_array = [64, 25, 12, 22, 11] + +# Apply selection sort +sorted_array = selectionsort_v1(unsorted_array) + +# Display the sorted list +print("Sorted Array:", sorted_array) +``` + +#### Output: + +Sorted Array: [11, 12, 22, 25, 64] + + +## Contributing + +If you would like to contribute to this project, please follow the contribution guidelines outlined in the CONTRIBUTING.md file. + +## License + +This project is licensed under the MIT License - see the LICENSE.md file for details. + diff --git a/src/sorting/python/selection_sort/src/selection_sort.py b/src/sorting/python/selection_sort/src/selection_sort.py index e69de29..fae1182 100644 --- a/src/sorting/python/selection_sort/src/selection_sort.py +++ b/src/sorting/python/selection_sort/src/selection_sort.py @@ -0,0 +1,19 @@ +def selectionsort_v1(list): + # traverse through all array elements + for i in range(len(list)): + + # find the minimum element in the remaining unsorted array + min_idx = i + + # iterate through the remaining elements to find the minimum + for j in range(i + 1, len(list)): + # Compare the current minimum with the current element + if list[min_idx] > list[j]: + # Update the index of the minimum element if a smaller element is found + min_idx = j + + # Swap the found minimum element with the first element + list[i], list[min_idx] = list[min_idx], list[i] + + # Return the sorted array + return list \ No newline at end of file