-
-
Notifications
You must be signed in to change notification settings - Fork 774
Feat: add cycle sort #212
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
Merged
Merged
Feat: add cycle sort #212
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,83 @@ | ||
# Cycle Sort | ||
|
||
#### Problem Statement | ||
|
||
Given an unsorted array of n elements, write a function to sort the array | ||
|
||
#### Approach | ||
|
||
- If the element is already at its correct position do nothing | ||
- Otherwise, find the correct position of a by counting the total number of elements that are less than current element | ||
- Insert current element into its correct position | ||
- Set replaced element as new current element and find its correct position | ||
- Continue process until array is sorted | ||
|
||
#### Time Complexity | ||
|
||
`O(n^2)` Worst case performance | ||
|
||
`O(n^2)` Best-case performance | ||
|
||
`O(n^2)` Average performance | ||
|
||
#### Space Complexity | ||
|
||
`O(n)` Worst case | ||
|
||
#### Application of algorithm | ||
|
||
- Cycle sort algorithm is useful for situations where memory write or element swap operations are costly. | ||
|
||
#### Example | ||
|
||
A single cycle of sorting array | b | d | e | a | c | | ||
|
||
``` | ||
1. Select element for which the cycle is run, i.e. "b". | ||
|
||
|b|d|e|a|c| | ||
|
||
b - current element | ||
|
||
2. Find correct location for current element and update current element. | ||
|
||
|b|b|e|a|c| | ||
|
||
d - current element | ||
|
||
3. One more time, find correct location for current element and update current element. | ||
|
||
|b|b|e|d|c| | ||
|
||
a - current element | ||
|
||
4. Current element is inserted into position of initial element "b" which ends the cycle. | ||
|
||
|a|b|e|d|c| | ||
|
||
a - current element | ||
|
||
5. New cycle should be started for next element. | ||
``` | ||
|
||
#### Code Implementation Links | ||
|
||
- [C](https://github.com/TheAlgorithms/C/blob/master/sorting/cycle_sort.c) | ||
- [C#](https://github.com/TheAlgorithms/C-Sharp/blob/master/Algorithms/Sorters/Comparison/CycleSorter.cs) | ||
- [C++](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/cycle_sort.cpp) | ||
- [F#](https://github.com/TheAlgorithms/F-Sharp/blob/main/Algorithms/Sort/Cycle_Sort.fs) | ||
- [Go](https://github.com/TheAlgorithms/Go/blob/master/sort/cyclesort.go) | ||
- [Java](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/CycleSort.java) | ||
- [Javascript](https://github.com/TheAlgorithms/JavaScript/blob/master/Sorts/CycleSort.js) | ||
- [Python](https://github.com/TheAlgorithms/Python/blob/master/sorts/cycle_sort.py) | ||
- [Rust](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/cycle_sort.rs) | ||
- [TypeScript](https://github.com/TheAlgorithms/TypeScript/blob/master/sorts/cycle_sort.ts) | ||
|
||
#### Video Explanation | ||
|
||
[A video explaining the Cycle Sort Algorithm](https://www.youtube.com/watch?v=gZNOM_yMdSQ) | ||
|
||
#### The Algorithms Page | ||
|
||
[Cycle Sort](https://the-algorithms.com/algorithm/cycle-sort) | ||
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.