Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

selection_sort.py: Add SelectionSort Algo #447

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant change

This repository contains examples of various algorithms written on different programming languages...

## Implemented Algorithms
Expand All @@ -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) | | | |

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant change

| [Knapsack Problem](https://en.wikipedia.org/wiki/Knapsack_problem) | | | | [:octocat:](knapsack_problem/Python)|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant change

| [Selecton Sort](https://en.wikipedia.org/wiki/Selection_sort) | [:octocat:](selection_sort/C) | | | [:octocat:](selection_sort/Python) |

## Implemented Data Structures

Expand All @@ -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) |


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant change

## Sample Run

| Language | Steps |
| --------------- | ---------------------------------------------------------------------- |
| JavaScript | <pre>node [filename.js]</pre> |
| Python | <pre>python [filename.py]</pre> |
| C | <pre>gcc [filename.c]<br>./a.out # unix<br>a.exe # windows</pre> |
| CPP | <pre>g++ [filename.cpp]<br>./a.out # unix<br>a.exe # windows</pre> |
| Java | <pre>javac [filename.java]<br>java [filename]</pre> |

| Language | Steps |
| --------------- | ------------------------------------------------------------------------ |
| JavaScript | ```node [filename.js]``` |
| Python | ```python [filename.py]``` |
| C | ```gcc [filename.c]```<br>```./a.out # unix```<br>```a.exe # windows```|
| CPP | ```g++ [filename.cpp]```<br>```./a.out # unix```<br>```a.exe # windows```|
| Java | ```javac [filename.java]```<br>```java [filename]``` |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes needs to be ignored as it is being used as HTML tags and then being used for the website https://nitskmos.github.io/Algorithms/.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

capture
Is this what you want it to look like?


## Contributing

Expand Down
34 changes: 34 additions & 0 deletions selection_sort/python/selection_sort.py
Original file line number Diff line number Diff line change
@@ -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
sangamcse marked this conversation as resolved.
Show resolved Hide resolved

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
de-sh marked this conversation as resolved.
Show resolved Hide resolved
low = j
arr[i], arr[low] = arr[low], arr[i] # Swap element at i with element low

de-sh marked this conversation as resolved.
Show resolved Hide resolved

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()