-
Notifications
You must be signed in to change notification settings - Fork 302
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
Changes from all commits
8b14887
cc39a18
93edc39
f00d9de
1fbb1f5
98443df
95252c7
ee3be2a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | | | | | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
||
|
@@ -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) | | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]``` | | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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/. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
## Contributing | ||
|
||
|
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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Redundant change