From 6d82f8714a9bf1d4e2dcaeec35cd6a0f636a23dd Mon Sep 17 00:00:00 2001 From: Bartlomiej Szmolke Date: Wed, 24 May 2023 11:37:36 +0200 Subject: [PATCH 1/3] Feat: add cycle sort --- en/Sorting Algorithms/Cycle Sort.md | 83 +++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 en/Sorting Algorithms/Cycle Sort.md diff --git a/en/Sorting Algorithms/Cycle Sort.md b/en/Sorting Algorithms/Cycle Sort.md new file mode 100644 index 00000000..162d8512 --- /dev/null +++ b/en/Sorting Algorithms/Cycle Sort.md @@ -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) + From d6a9c47e0c71fb24797b1907e8d8ff0b06656946 Mon Sep 17 00:00:00 2001 From: BSzmolke <40246238+BSzmolke@users.noreply.github.com> Date: Wed, 24 May 2023 15:33:23 +0200 Subject: [PATCH 2/3] remove redundant links --- en/Sorting Algorithms/Cycle Sort.md | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/en/Sorting Algorithms/Cycle Sort.md b/en/Sorting Algorithms/Cycle Sort.md index 162d8512..37f6dc2f 100644 --- a/en/Sorting Algorithms/Cycle Sort.md +++ b/en/Sorting Algorithms/Cycle Sort.md @@ -60,19 +60,6 @@ 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) From 8d1d450f3cfaa0b6ece1a78b99255d1a5d3ebe84 Mon Sep 17 00:00:00 2001 From: David Leal Date: Thu, 25 May 2023 19:58:25 -0600 Subject: [PATCH 3/3] chore: apply suggestions from code review --- en/Sorting Algorithms/Cycle Sort.md | 1 - 1 file changed, 1 deletion(-) diff --git a/en/Sorting Algorithms/Cycle Sort.md b/en/Sorting Algorithms/Cycle Sort.md index 37f6dc2f..840aab13 100644 --- a/en/Sorting Algorithms/Cycle Sort.md +++ b/en/Sorting Algorithms/Cycle Sort.md @@ -67,4 +67,3 @@ a - current element #### The Algorithms Page [Cycle Sort](https://the-algorithms.com/algorithm/cycle-sort) -