generated from MIT-Emerging-Talent/Algorithms-And-Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0215bd6
commit 254b1bb
Showing
2 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |