diff --git a/README.md b/README.md index 570722e1..5fcc7a16 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,6 @@ [![GitHub](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/NITSkmOS/Algorithms/blob/master/LICENSE) [![OpenHub](https://www.openhub.net/p/NITSkmOS-algo/widgets/project_thin_badge?format=gif)](https://www.openhub.net/p/NITSkmOS-algo?ref=Thin+badge) - This repository contains examples of various algorithms written on different programming languages... ## Implemented Algorithms @@ -24,9 +23,8 @@ This repository contains examples of various algorithms written on different pro | [Shell Sort](https://en.wikipedia.org/wiki/Shellsort) | [:octocat:](shell_sort/C) | | | [:octocat:](shell_sort/Python) | | [Heap Sort](https://en.wikipedia.org/wiki/Heapsort) | | | | [:octocat:](heap_sort/python) | | [Maximum Subarray Problem](https://en.wikipedia.org/wiki/Maximum_subarray_problem) | | | | [:octocat:](/maximum_subarray/Python)| -| [Knapsack Problem](https://en.wikipedia.org/wiki/Knapsack_problem) | | | | [:octocat:](knapsack_problem/Python)| -| [Selecton Sort](https://en.wikipedia.org/wiki/Selection_sort) | [:octocat:](selection_sort/C) | | | | - +| [Knapsack Problem](https://en.wikipedia.org/wiki/Knapsack_problem) | | | | [:octocat:](knapsack_problem/Python)| +| [Selecton Sort](https://en.wikipedia.org/wiki/Selection_sort) | [:octocat:](selection_sort/C) | | | [:octocat:](selection_sort/Python) | ## Implemented Data Structures @@ -39,17 +37,15 @@ This repository contains examples of various algorithms written on different pro | [Binary Search Tree](https://en.wikipedia.org/wiki/Binary_search_tree) | | [:octocat:](binary_search_tree/Cpp) | | | | [Fenwick Tree](https://en.wikipedia.org/wiki/Fenwick_tree) | | | [:octocat:](fenwick_tree/java) | [:octocat:](fenwick_tree/Python) | - ## Sample Run -| Language | Steps | -| --------------- | ---------------------------------------------------------------------- | -| JavaScript |
node [filename.js]
| -| Python |
python [filename.py]
| -| C |
gcc [filename.c]
./a.out # unix
a.exe # windows
| -| CPP |
g++ [filename.cpp]
./a.out # unix
a.exe # windows
| -| Java |
javac [filename.java]
java [filename]
| - +| Language | Steps | +| --------------- | ------------------------------------------------------------------------ | +| JavaScript | ```node [filename.js]``` | +| Python | ```python [filename.py]``` | +| C | ```gcc [filename.c]```
```./a.out # unix```
```a.exe # windows```| +| CPP | ```g++ [filename.cpp]```
```./a.out # unix```
```a.exe # windows```| +| Java | ```javac [filename.java]```
```java [filename]``` | ## Contributing diff --git a/selection_sort/python/selection_sort.py b/selection_sort/python/selection_sort.py new file mode 100644 index 00000000..9f0840a9 --- /dev/null +++ b/selection_sort/python/selection_sort.py @@ -0,0 +1,34 @@ +def selection_sort(arr): # Function that performs the SelectionSort + """ + - The SelectionSort function takes in an array as argument + - Sorts the elements in array by making use of inner and outer loops + - Returns a sorted array as output + :papram arr: An array for sorting + """ + n = len(arr) # Length of arr is stored for further use + + for i in range(n): # Traverse through all array elements + low = i # Set the current element to low + for j in range(i+1, n): + """ + Traverse the array from i+1 to n-i as inner loop, + i.e. i is the counter for outer loop and j for + the inner loop + """ + if arr[j] < arr[low]: # Set low to current j, if it's value is smaller + low = j + arr[i], arr[low] = arr[low], arr[i] # Swap element at i with element low + + +def main(): + arr = [4, 3, 42, 82, 5, 2, 33] # Declare a sample array + print('Unsorted elements before using SelectionSort: {}'.format( + ' '.join(map(str, arr)))) # Print current state of array + + selection_sort(arr) + print('Sorted element using SelectionSort: {}'.format( + ' '.join(map(str, arr)))) # Print the state of array after selection sorting + + +if __name__ == '__main__': + main()