Skip to content

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 3 commits into from
May 26, 2023
Merged
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
69 changes: 69 additions & 0 deletions en/Sorting Algorithms/Cycle Sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# 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.
```

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