├── .gitattributes
├── public
├── favicon.ico
├── robots.txt
├── manifest.json
└── index.html
├── screenshots
├── Screenshot-1.png
└── Screenshot-2.png
├── src
├── redux
│ ├── Store
│ │ └── index.js
│ ├── action-types
│ │ └── index.js
│ ├── Actions
│ │ └── index.js
│ └── Reducers
│ │ └── index.js
├── components
│ ├── Descreptions
│ │ ├── CodeComponent.jsx
│ │ ├── DescList
│ │ │ └── DescList.styles.js
│ │ ├── DescComponents
│ │ │ ├── FaithSort.jsx
│ │ │ ├── BubbleBogoSort.jsx
│ │ │ ├── OddEvenSort.jsx
│ │ │ ├── QuantumBogoSort.jsx
│ │ │ ├── BubbleSortOptimized.jsx
│ │ │ ├── QuickSortStable.jsx
│ │ │ ├── GnomeSort.jsx
│ │ │ ├── GnomeSortOptimized.jsx
│ │ │ ├── InsertionSortBinary.jsx
│ │ │ ├── DoubleSelectionSort.jsx
│ │ │ ├── IntelligentDesignSort.jsx
│ │ │ ├── OddEvenMergeSort.jsx
│ │ │ ├── BogoSort.jsx
│ │ │ ├── MergeSortBottomUp.jsx
│ │ │ ├── PancakeSort.jsx
│ │ │ ├── BozoSort.jsx
│ │ │ ├── PairwiseSortingNetwork.jsx
│ │ │ ├── CycleSort.jsx
│ │ │ ├── InsertionSort.jsx
│ │ │ ├── HeapSort.jsx
│ │ │ ├── ShellSort.jsx
│ │ │ ├── CircleSort.jsx
│ │ │ ├── TimSort.jsx
│ │ │ ├── CombSort.jsx
│ │ │ ├── GravitySort.jsx
│ │ │ ├── RadixSort.jsx
│ │ │ └── SelectionSort.jsx
│ │ └── Container
│ │ │ ├── HiddenDescList.styles.js
│ │ │ ├── HiddenDescList.jsx
│ │ │ ├── Description.styles.js
│ │ │ ├── Container.styles.js
│ │ │ └── index.jsx
│ ├── AlgorithmsContainer
│ │ ├── index.jsx
│ │ ├── AlgorithmsContainer.styles.js
│ │ └── Algorithm
│ │ │ ├── Algorithm.styles.js
│ │ │ └── index.jsx
│ ├── SortSettings
│ │ ├── index.jsx
│ │ └── SettingsHidden.jsx
│ ├── Footer
│ │ ├── index.jsx
│ │ └── Footer.styles.js
│ ├── Main
│ │ ├── Main.styles.js
│ │ └── index.jsx
│ ├── Sidebar
│ │ ├── HiddenSidebar.jsx
│ │ ├── HiddenSidebar.styles.js
│ │ ├── index.jsx
│ │ ├── Sidebar.styles.js
│ │ └── AddAlogoModal.styles.js
│ └── Header
│ │ ├── index.jsx
│ │ ├── InfoModal.styles.js
│ │ ├── Header.styles.js
│ │ └── InfoModal.jsx
├── setupTests.js
├── App.test.js
├── index.js
├── reportWebVitals.js
├── utils
│ ├── newElements.jsx
│ ├── SortGenerators
│ │ ├── bozoSort.js
│ │ ├── gnomeSort.js
│ │ ├── bogoSort.js
│ │ ├── bubbleBogoSort.js
│ │ ├── gnomeSortOptimized.js
│ │ ├── insertionSort.js
│ │ ├── combSort.js
│ │ ├── bubbleSort.js
│ │ ├── countingSort.js
│ │ ├── selectionSort.js
│ │ ├── oddEvenSort.js
│ │ ├── radixSortBase10.js
│ │ ├── bubbleSortOtimized.js
│ │ ├── radixSortBase2.js
│ │ ├── radixSortBase5.js
│ │ ├── shellSort.js
│ │ ├── insertionSortBinary.js
│ │ ├── pancakeSort.js
│ │ ├── quickSortRightPivot.js
│ │ ├── gravitySort.js
│ │ ├── quickSortMiddlePivot.js
│ │ ├── doubleSelectionSort.js
│ │ ├── oddEvenMergeSort.js
│ │ ├── pairwiseSortingNetwork.js
│ │ ├── circleSort.js
│ │ ├── hepSortMax.js
│ │ ├── mergeSortBottomUp.js
│ │ ├── quickSortLeftPivot.js
│ │ ├── bitonicSort.js
│ │ ├── cocktailShakerSort.js
│ │ ├── heapSortMin.js
│ │ ├── mergeSort.js
│ │ ├── flashSort.js
│ │ ├── quickSortStable.js
│ │ ├── bucketSort.js
│ │ ├── cycleSort.js
│ │ └── timSort.js
│ ├── index.js
│ └── getSortingMethod.js
├── hooks
│ ├── useIntersection.js
│ └── useSortingAlgo.jsx
├── Theme.jsx
└── App.js
├── .gitignore
└── package.json
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/copperhuh/SortDemon/HEAD/public/favicon.ico
--------------------------------------------------------------------------------
/public/robots.txt:
--------------------------------------------------------------------------------
1 | # https://www.robotstxt.org/robotstxt.html
2 | User-agent: *
3 | Disallow:
4 |
--------------------------------------------------------------------------------
/screenshots/Screenshot-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/copperhuh/SortDemon/HEAD/screenshots/Screenshot-1.png
--------------------------------------------------------------------------------
/screenshots/Screenshot-2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/copperhuh/SortDemon/HEAD/screenshots/Screenshot-2.png
--------------------------------------------------------------------------------
/src/redux/Store/index.js:
--------------------------------------------------------------------------------
1 | import { createStore } from "redux";
2 | import { reducer } from "../Reducers";
3 |
4 | export const store = createStore(reducer);
5 |
--------------------------------------------------------------------------------
/src/components/Descreptions/CodeComponent.jsx:
--------------------------------------------------------------------------------
1 | import { CopyBlock, obsidian } from "react-code-blocks";
2 |
3 | export default function CodeComponent(props) {
4 | return (
5 |
33 | Source:{" "} 34 | 35 | reddit 36 | 37 |
38 | > 39 | ); 40 | } 41 | -------------------------------------------------------------------------------- /src/utils/SortGenerators/countingSort.js: -------------------------------------------------------------------------------- 1 | import newElements from "../newElements"; 2 | 3 | export default function* countingSort(items, arrMax) { 4 | let arr = JSON.parse(JSON.stringify(items)); 5 | let n = arr.length; 6 | 7 | let count = Array.from({ length: arrMax }, (_, i) => 0); 8 | for (let i = 0; i < n; ++i) { 9 | count[arr[i].val - 1]++; 10 | 11 | arr[i].active = true; 12 | yield newElements(arr, arrMax); 13 | arr[i].active = false; 14 | } 15 | 16 | for (let i = 1; i < count.length; i++) { 17 | count[i] += count[i - 1]; 18 | 19 | arr[i].active = true; 20 | yield newElements(arr, arrMax); 21 | arr[i].active = false; 22 | } 23 | for (let i = 0; i < n; i++) { 24 | let num; 25 | for (let j = 0; j < count.length; j++) { 26 | if (count[j] > i) { 27 | num = j + 1; 28 | break; 29 | } 30 | } 31 | arr[i].val = num; 32 | 33 | arr[i].active = true; 34 | yield newElements(arr, arrMax); 35 | arr[i].active = false; 36 | } 37 | 38 | for (let i = 0; i < arr.length; i++) { 39 | arr[i].active = true; 40 | yield newElements(arr, arrMax); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/components/Main/Main.styles.js: -------------------------------------------------------------------------------- 1 | import styled from "styled-components"; 2 | 3 | const MainContainer = styled.div` 4 | display: flex; 5 | width: 100%; 6 | height: fit-content; 7 | min-height: 100vh; 8 | padding: 3rem 0; 9 | background-color: ${(props) => props.theme.colors.light}; 10 | position: relative; 11 | 12 | .algo-container { 13 | min-height: 100%; 14 | flex: 1; 15 | } 16 | .sidebar { 17 | width: fit-content; 18 | height: 100%; 19 | } 20 | .open-sidebar { 21 | height: fit-content; 22 | position: sticky; 23 | top: 2rem; 24 | right: 2rem; 25 | padding: 0.3rem 0.5rem; 26 | display: flex; 27 | align-items: center; 28 | justify-content: center; 29 | background: ${(props) => props.theme.colors.light}; 30 | color: ${(props) => props.theme.colors.main}; 31 | border: 1px solid ${(props) => props.theme.colors.main}; 32 | float: left; 33 | :hover { 34 | background: ${(props) => props.theme.colors.main}; 35 | color: ${(props) => props.theme.colors.light}; 36 | border: 1px solid ${(props) => props.theme.colors.light}; 37 | } 38 | } 39 | `; 40 | 41 | export default MainContainer; 42 | -------------------------------------------------------------------------------- /src/components/AlgorithmsContainer/Algorithm/Algorithm.styles.js: -------------------------------------------------------------------------------- 1 | import styled from "styled-components"; 2 | 3 | const AlgorithmStyled = styled.div` 4 | background-color: none; 5 | color: ${(props) => props.theme.colors.dark}; 6 | display: flex; 7 | min-height: 300px; 8 | min-width: 220px; 9 | flex-direction: column; 10 | align-items: center; 11 | justify-content: space-evenly; 12 | padding: 1% 0 5%; 13 | h1 { 14 | font-size: ${(props) => 15 | props.len < 4 ? "2rem" : props.len < 7 ? "1.3rem" : "1rem"}; 16 | text-align: center; 17 | } 18 | 19 | .nodes { 20 | height: 90%; 21 | width: 80%; 22 | display: flex; 23 | justify-content: flex-end; 24 | flex: 1; 25 | } 26 | .node-container { 27 | display: flex; 28 | flex-direction: column; 29 | justify-content: flex-end; 30 | padding: 0 0; 31 | margin: 0 auto; 32 | } 33 | .node-coloured { 34 | background: ${(props) => props.theme.colors.dark}; 35 | width: 100%; 36 | } 37 | 38 | .active { 39 | background: ${(props) => props.theme.colors.main}; 40 | } 41 | @media (max-width: 450px) { 42 | padding: 0; 43 | width: 100vw; 44 | } 45 | `; 46 | 47 | export default AlgorithmStyled; 48 | -------------------------------------------------------------------------------- /src/utils/SortGenerators/selectionSort.js: -------------------------------------------------------------------------------- 1 | import newElements from "../newElements"; 2 | 3 | export default function* selectionSort(items, arrMax) { 4 | let i = 0; 5 | let j = 0; 6 | let arr = JSON.parse(JSON.stringify(items)); 7 | 8 | while (i < arr.length) { 9 | j = i + 1; 10 | let min = i; 11 | 12 | arr[i].active = true; 13 | if (i + 1 !== arr.length) arr[j].active = true; 14 | arr[min].active = true; 15 | 16 | while (j < arr.length) { 17 | if (arr[min].val > arr[j].val) { 18 | if (min !== i && min !== j) arr[min].active = false; 19 | min = j; 20 | arr[min].active = true; 21 | } 22 | 23 | if (j !== i && j !== min) arr[j].active = false; 24 | j++; 25 | if (j !== arr.length) arr[j].active = true; 26 | 27 | yield newElements(arr, arrMax); 28 | } 29 | 30 | [arr[i].val, arr[min].val] = [arr[min].val, arr[i].val]; 31 | 32 | arr[min].active = false; 33 | arr[i].active = false; 34 | i++; 35 | if (i !== arr.length) arr[i].active = true; 36 | 37 | yield newElements(arr, arrMax); 38 | } 39 | for (let i = 0; i < arr.length; i++) { 40 | arr[i].active = true; 41 | yield newElements(arr, arrMax); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/utils/SortGenerators/oddEvenSort.js: -------------------------------------------------------------------------------- 1 | import newElements from "../newElements"; 2 | 3 | export default function* oddEvenSort(items, arrMax) { 4 | let arr = JSON.parse(JSON.stringify(items)); 5 | let n = arr.length; 6 | let isSorted = false; 7 | 8 | while (!isSorted) { 9 | isSorted = true; 10 | 11 | for (let i = 1; i <= n - 2; i = i + 2) { 12 | if (arr[i].val > arr[i + 1].val) { 13 | [arr[i].val, arr[i + 1].val] = [arr[i + 1].val, arr[i].val]; 14 | isSorted = false; 15 | } 16 | arr[i].active = true; 17 | arr[i + 1].active = true; 18 | yield newElements(arr, arrMax); 19 | arr[i].active = false; 20 | arr[i + 1].active = false; 21 | } 22 | 23 | for (let i = 0; i <= n - 2; i = i + 2) { 24 | if (arr[i].val > arr[i + 1].val) { 25 | [arr[i].val, arr[i + 1].val] = [arr[i + 1].val, arr[i].val]; 26 | isSorted = false; 27 | } 28 | arr[i].active = true; 29 | arr[i + 1].active = true; 30 | yield newElements(arr, arrMax); 31 | arr[i].active = false; 32 | arr[i + 1].active = false; 33 | } 34 | } 35 | 36 | for (let i = 0; i < arr.length; i++) { 37 | arr[i].active = true; 38 | yield newElements(arr, arrMax); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/components/Descreptions/Container/HiddenDescList.styles.js: -------------------------------------------------------------------------------- 1 | import styled from "styled-components"; 2 | 3 | const HiddenDescListStyled = styled.div` 4 | width: 100vw; 5 | height: 100%; 6 | background: rgba(0, 0, 0, 0.3); 7 | position: fixed; 8 | top: 0; 9 | left: 0; 10 | z-index: 2; 11 | .modal-bg { 12 | background: ${(props) => props.theme.colors.dark}; 13 | width: fit-content; 14 | height: 100%; 15 | overflow-y: auto; 16 | position: relative; 17 | &::-webkit-scrollbar { 18 | width: 10px; 19 | } 20 | &::-webkit-scrollbar-track { 21 | background: ${(props) => props.theme.colors.dark}; 22 | } 23 | &::-webkit-scrollbar-thumb { 24 | width: 5px; 25 | background: ${(props) => props.theme.colors.main}; 26 | border-radius: 5px; 27 | } 28 | } 29 | .close-modal { 30 | position: absolute; 31 | right: 1rem; 32 | top: 1rem; 33 | color: ${(props) => props.theme.colors.main}; 34 | background: none; 35 | border: none; 36 | :hover { 37 | color: ${(props) => props.theme.colors.light}; 38 | } 39 | } 40 | @media (max-width: 370px) { 41 | .modal-bg { 42 | width: 100%; 43 | } 44 | } 45 | `; 46 | 47 | export default HiddenDescListStyled; 48 | -------------------------------------------------------------------------------- /src/utils/SortGenerators/radixSortBase10.js: -------------------------------------------------------------------------------- 1 | import newElements from "../newElements"; 2 | 3 | export default function* radixSortBase10(items, arrMax) { 4 | let arr = JSON.parse(JSON.stringify(items)); 5 | let n = arr.length; 6 | 7 | for (let exp = 1; Math.floor(arrMax / exp) > 0; exp *= 10) { 8 | let output = new Array(n); 9 | let count = new Array(10); 10 | for (let i = 0; i < 10; i++) { 11 | count[i] = 0; 12 | 13 | arr[i].active = true; 14 | yield newElements(arr, arrMax); 15 | arr[i].active = false; 16 | } 17 | 18 | for (let i = 0; i < n; i++) count[Math.floor(arr[i].val / exp) % 10]++; 19 | 20 | for (let i = 1; i < 10; i++) count[i] += count[i - 1]; 21 | 22 | for (let i = n - 1; i >= 0; i--) { 23 | output[count[Math.floor(arr[i].val / exp) % 10] - 1] = arr[i].val; 24 | count[Math.floor(arr[i].val / exp) % 10]--; 25 | } 26 | 27 | for (let i = 0; i < n; i++) { 28 | arr[i].val = output[i]; 29 | 30 | arr[i].active = true; 31 | yield newElements(arr, arrMax); 32 | arr[i].active = false; 33 | } 34 | } 35 | 36 | for (let i = 0; i < arr.length; i++) { 37 | arr[i].active = true; 38 | yield newElements(arr, arrMax); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/utils/SortGenerators/bubbleSortOtimized.js: -------------------------------------------------------------------------------- 1 | import newElements from "../newElements"; 2 | 3 | export default function* bubbleSortOptimized(items, arrMax) { 4 | let i = 0; 5 | let j = 0; 6 | let arr = JSON.parse(JSON.stringify(items)); 7 | 8 | while (i < arr.length) { 9 | let wasSwapped = false; 10 | j = 0; 11 | 12 | arr[i].active = true; 13 | arr[j].active = true; 14 | 15 | while (j < arr.length - 1 - i) { 16 | if (arr[j].val > arr[j + 1].val) { 17 | [arr[j].val, arr[j + 1].val] = [arr[j + 1].val, arr[j].val]; 18 | wasSwapped = true; 19 | } 20 | 21 | if (j !== i) arr[j].active = false; 22 | j++; 23 | arr[j].active = true; 24 | if (j + 1 !== arr.length) arr[j + 1].active = true; 25 | 26 | yield newElements(arr, arrMax); 27 | } 28 | 29 | if (j + 1 !== arr.length) arr[j + 1].active = false; 30 | arr[j].active = false; 31 | arr[i].active = false; 32 | 33 | i++; 34 | 35 | if (i !== arr.length) arr[i].active = true; 36 | 37 | yield newElements(arr, arrMax); 38 | 39 | if (!wasSwapped) { 40 | break; 41 | } 42 | } 43 | 44 | for (let i = 0; i < arr.length; i++) { 45 | arr[i].active = true; 46 | yield newElements(arr, arrMax); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/utils/SortGenerators/radixSortBase2.js: -------------------------------------------------------------------------------- 1 | import newElements from "../newElements"; 2 | 3 | export default function* radixSortBase2(items, arrMax) { 4 | let arr = JSON.parse(JSON.stringify(items)); 5 | let n = arr.length; 6 | let base = 2; 7 | 8 | for (let exp = 1; Math.floor(arrMax / exp) > 0; exp *= base) { 9 | let output = new Array(n); 10 | let count = new Array(base); 11 | for (let i = 0; i < base; i++) { 12 | count[i] = 0; 13 | 14 | arr[i].active = true; 15 | yield newElements(arr, arrMax); 16 | arr[i].active = false; 17 | } 18 | 19 | for (let i = 0; i < n; i++) 20 | count[Math.floor(arr[i].val / exp) % base]++; 21 | 22 | for (let i = 1; i < 10; i++) count[i] += count[i - 1]; 23 | 24 | for (let i = n - 1; i >= 0; i--) { 25 | output[count[Math.floor(arr[i].val / exp) % base] - 1] = arr[i].val; 26 | count[Math.floor(arr[i].val / exp) % base]--; 27 | } 28 | 29 | for (let i = 0; i < n; i++) { 30 | arr[i].val = output[i]; 31 | 32 | arr[i].active = true; 33 | yield newElements(arr, arrMax); 34 | arr[i].active = false; 35 | } 36 | } 37 | 38 | for (let i = 0; i < arr.length; i++) { 39 | arr[i].active = true; 40 | yield newElements(arr, arrMax); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/utils/SortGenerators/radixSortBase5.js: -------------------------------------------------------------------------------- 1 | import newElements from "../newElements"; 2 | 3 | export default function* radixSortBase5(items, arrMax) { 4 | let arr = JSON.parse(JSON.stringify(items)); 5 | let n = arr.length; 6 | let base = 5; 7 | 8 | for (let exp = 1; Math.floor(arrMax / exp) > 0; exp *= base) { 9 | let output = new Array(n); 10 | let count = new Array(base); 11 | for (let i = 0; i < base; i++) { 12 | count[i] = 0; 13 | 14 | arr[i].active = true; 15 | yield newElements(arr, arrMax); 16 | arr[i].active = false; 17 | } 18 | 19 | for (let i = 0; i < n; i++) 20 | count[Math.floor(arr[i].val / exp) % base]++; 21 | 22 | for (let i = 1; i < base; i++) count[i] += count[i - 1]; 23 | 24 | for (let i = n - 1; i >= 0; i--) { 25 | output[count[Math.floor(arr[i].val / exp) % base] - 1] = arr[i].val; 26 | count[Math.floor(arr[i].val / exp) % base]--; 27 | } 28 | 29 | for (let i = 0; i < n; i++) { 30 | arr[i].val = output[i]; 31 | 32 | arr[i].active = true; 33 | yield newElements(arr, arrMax); 34 | arr[i].active = false; 35 | } 36 | } 37 | 38 | for (let i = 0; i < arr.length; i++) { 39 | arr[i].active = true; 40 | yield newElements(arr, arrMax); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/redux/Actions/index.js: -------------------------------------------------------------------------------- 1 | import { 2 | CHANGE_SIZE, 3 | CHANGE_SORT_TYPE, 4 | CHANGE_SPEED, 5 | SHUFFLE, 6 | CHANGE_SORTING_ONGOING, 7 | ADD_ALGORITHM, 8 | REMOVE_ALGORITHM, 9 | CHANGE_DESCRIPTION, 10 | SET_DESC_REF, 11 | } from "../action-types"; 12 | 13 | export const doShuffle = { type: SHUFFLE }; 14 | 15 | export const doChangeSortType = (sortType) => ({ 16 | type: CHANGE_SORT_TYPE, 17 | payload: sortType, 18 | }); 19 | 20 | export const doChangeSpeed = (speed) => ({ 21 | type: CHANGE_SPEED, 22 | payload: speed, 23 | }); 24 | 25 | export const doChangeSize = (size) => ({ 26 | type: CHANGE_SIZE, 27 | payload: size, 28 | }); 29 | 30 | export const doChangeSortingOngoing = (sortingOngoing) => ({ 31 | type: CHANGE_SORTING_ONGOING, 32 | payload: sortingOngoing, 33 | }); 34 | 35 | export const doAddAlgorithm = (algorithm) => ({ 36 | type: ADD_ALGORITHM, 37 | payload: algorithm, 38 | }); 39 | 40 | export const doRemoveAlgorithm = (idx) => ({ 41 | type: REMOVE_ALGORITHM, 42 | payload: idx, 43 | }); 44 | 45 | export const doChangeDescription = (name) => ({ 46 | type: CHANGE_DESCRIPTION, 47 | payload: name, 48 | }); 49 | 50 | export const doSetDescRef = (ref) => ({ 51 | type: SET_DESC_REF, 52 | payload: ref, 53 | }); 54 | -------------------------------------------------------------------------------- /src/components/Descreptions/Container/HiddenDescList.jsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import ReactDOM from "react-dom"; 3 | import DescList from "../DescList"; 4 | import CloseIcon from "@mui/icons-material/Close"; 5 | import HiddenDescListStyled from "./HiddenDescList.styles"; 6 | import { motion, AnimatePresence } from "framer-motion"; 7 | 8 | export default function HiddenDescList({ open, setOpen }) { 9 | const outside = React.useRef(null); 10 | 11 | function onClose(e) { 12 | if (outside.current === e.target) { 13 | setOpen(false); 14 | } 15 | } 16 | 17 | return ReactDOM.createPortal( 18 |9 | Bubble bogo sort is a version of{" "} 10 | Bubble sort in which you{" "} 11 | randomly choose two elements from 12 | the array, compare them and then switch them if needed. 13 |
14 | 15 | Note: The visualization of this algorithm may seem quite fast, 16 | but that's only because it doesn't show iterations, in which the 17 | two compared elements are not switched (which is especially 18 | common in the latter portion of the runtime) 19 | 20 |9 | An Odd Even Sort or brick sort is 10 | a simple sorting algorithm, which is developed for use on 11 | parallel processors with local interconnection.{" "} 12 |
13 |14 | This algorithm is divided into two phases - Odd and Even Phase. 15 | The algorithm runs until the array elements are sorted and in 16 | each iteration two phases occurs - Odd and Even Phases.{" "} 17 |
18 |19 | In the odd phase, we perform a bubble sort on odd indexed 20 | elements and in the even phase, we perform a bubble sort on even 21 | indexed elements. 22 |
23 |
27 |
28 | Time Complexity : O(n*n)
29 |
30 | Auxiliary Space : O(1)
31 |
32 | In Place: Yes
33 |
34 | Stable: Yes
35 |
36 |
63 | Source:{" "} 64 | 65 | GeeksforGeeks 66 | 67 | ,{" "} 68 | 69 | RIP Tutorial 70 | 71 |
72 | > 73 | ); 74 | } 75 | -------------------------------------------------------------------------------- /src/components/Sidebar/index.jsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { deCamelCase } from "../../utils"; 3 | import SidebarStyled from "./Sidebar.styles"; 4 | import DoDisturbAltIcon from "@mui/icons-material/DoDisturbAlt"; 5 | import AddIcon from "@mui/icons-material/Add"; 6 | import AddAlgoModal from "./AddAlgoModal"; 7 | import { connect } from "react-redux"; 8 | import { doChangeDescription, doRemoveAlgorithm } from "../../redux/Actions"; 9 | 10 | function Sidebar({ 11 | algorithms, 12 | removeAlgorithm, 13 | changeDescription, 14 | descRef, 15 | hiddenSidebarSetOpen, 16 | }) { 17 | const [open, setOpen] = React.useState(false); 18 | 19 | let elements = algorithms.map((el, id) => { 20 | return ( 21 |9 | QuantumBogoSort is a quantum 10 | sorting algorithm which can sort any list in O(1), using the 11 | "many worlds" interpretation of{" "} 12 | quantum mechanics. 13 |
14 |18 | Quantumly randomise the list, such that there is no way 19 | of knowing what order the list is in until it is 20 | observed. This will divide the universe into O(n!) 21 | universes; however, the division has no cost, as it 22 | happens constantly anyway. 23 |
24 |27 | If the list is not sorted, destroy the universe. (This 28 | operation is left as an exercise to the reader.) 29 |
30 |33 | All remaining universes contain lists which are sorted. 34 |
35 |39 | As described above, is not a StableSort. A stable version might 40 | be produced as follows: 41 |
42 |45 | Configure the quantum randomiser to produce random code, 46 | rather than shuffle lists. Instruct it to generate some 47 | code. 48 |
49 |52 | If the resulting code is not a stable QuantumBogoSort, 53 | destroy the universe. 54 |
55 |58 | All remaining universes now have a stable 59 | QuantumBogoSort algorithm. 60 |
61 |
64 | Auxilary Space: O(1)
65 |
66 | Auxilary Space: O(1)
67 |
68 | Stable: No, but can be easily
69 | implemented
70 |
71 | In Place: Somewhere
72 |
80 | Source: wik.c2 81 |
82 | > 83 | ); 84 | } 85 | -------------------------------------------------------------------------------- /src/components/Descreptions/DescComponents/BubbleSortOptimized.jsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import CodeComponent from "../CodeComponent"; 3 | 4 | export default function BubbleSortOptimized() { 5 | return ( 6 | <> 7 |9 | In normal BubbleSort, all the 10 | comparisons are made even if the array is already sorted. 11 |
12 |This increases the execution time.
13 |14 | To solve this, we can introduce an extra variable ' 15 | swapped'. The value of 'swapped' 16 | is set true if there occurs swapping of elements. Otherwise, it 17 | is set false. 18 |
19 |20 | After an iteration, if there is no swapping, the value of ' 21 | swapped' will be{" "} 22 | false. This means elements are 23 | already sorted and there is no need to perform further 24 | iterations. 25 |
26 |27 | We can optimize BubbleSort{" "} 28 | further, by shortening each inner loop's instance by one, 29 | since we know that with each instance the greatest element will 30 | be placed in its correct position at the end. 31 |
32 |33 | This will reduce the execution time and helps to optimize the 34 | bubble sort. 35 |
36 |
37 | Time Complexity:
38 | O(n*n)
39 |
40 | Auxiliary Space: O(1)
41 |
42 | In Place: Yes
43 |
44 | Stable: Yes
45 |
70 | Source:{" "} 71 | 72 | Programiz 73 | 74 |
75 | > 76 | ); 77 | } 78 | -------------------------------------------------------------------------------- /src/components/Descreptions/DescComponents/QuickSortStable.jsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import CodeComponent from "../CodeComponent"; 3 | 4 | export default function QuickSortStable() { 5 | return ( 6 | <> 7 |9 | Normal QuickSort is an unstable 10 | algorithm because we do swapping of elements according to 11 | pivot’s position (without considering their original positions).{" "} 12 |
13 |
14 | Quicksort can be stable but it typically isn’t implemented that
15 | way. Making it stable either requires order N storage (as in a
16 | naive implementation) or a bit of extra logic for an in-place
17 | version.
18 |
19 | In below implementation, we use extra space. The idea is to make
20 | two separate lists:{" "}
21 |
27 | Time Complexity: O(n log n)
28 |
29 | Auxiliary Space: O(n)
30 |
31 | Stable: Yes
32 |
33 | In Place: No
34 |
71 | Source:{" "} 72 | 73 | GeeksforGeeks 74 | 75 |
76 | > 77 | ); 78 | } 79 | -------------------------------------------------------------------------------- /src/components/SortSettings/SettingsHidden.jsx: -------------------------------------------------------------------------------- 1 | import { StyledSortSettingsHidden } from "./SortSettings.styles"; 2 | import PlayArrowIcon from '@mui/icons-material/PlayArrow'; 3 | import PauseIcon from '@mui/icons-material/Pause'; 4 | import RestartAltIcon from '@mui/icons-material/RestartAlt'; 5 | import ShuffleIcon from '@mui/icons-material/Shuffle'; 6 | import KeyboardArrowDownIcon from '@mui/icons-material/KeyboardArrowDown'; 7 | import React from "react"; 8 | import { connect } from "react-redux"; 9 | import { doChangeSortingOngoing } from "../../redux/Actions"; 10 | 11 | const SettingsHidden = ({toggleShow, shuffle, changeSortingOngoing}) => { 12 | 13 | const onStart = () => { 14 | changeSortingOngoing(true) 15 | } 16 | 17 | const onPause = () => { 18 | changeSortingOngoing(false) 19 | } 20 | 21 | const onReset = () => { 22 | changeSortingOngoing(false) 23 | shuffle() 24 | } 25 | 26 | return ( 27 |9 | Gnome Sort is based on the concept 10 | of a Garden Gnome sorting his flower pots. A garden gnome sorts 11 | the flower pots by the following method- 12 |
13 |
53 |
54 | Time Complexity : O(n*n)
55 |
56 | Auxiliary Space : O(1)
57 |
58 | In Place: Yes
59 |
60 | Stable: Yes
61 |
81 | Source:{" "} 82 | 83 | GeeksforGeeks 84 | 85 |
86 | > 87 | ); 88 | } 89 | -------------------------------------------------------------------------------- /src/components/Descreptions/DescComponents/GnomeSortOptimized.jsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import CodeComponent from "../CodeComponent"; 3 | 4 | export default function GnomeSortOptimized() { 5 | return ( 6 | <> 7 |9 | Gnome sort is a sorting algorithm 10 | which is similar to insertion sort 11 | , except that moving an element to its proper place is 12 | accomplished by a series of swaps, as in{" "} 13 | bubble sort.{" "} 14 |
15 |16 | It is conceptually simple, requiring no nested loops. The 17 | running time is O(n*n), but in practice the algorithm can run as 18 | fast as Insertion sort. 19 |
20 |21 | The algorithm always finds the first place where two adjacent 22 | elements are in the wrong order, and swaps them. It takes 23 | advantage of the fact that performing a swap can introduce a new 24 | out-of-order adjacent pair only right before or after the two 25 | swapped elements. It does not assume that elements forward of 26 | the current position are sorted, so it only needs to check the 27 | position directly before the swapped elements. 28 |
29 |30 | An optimized version uses variable "j" to allow the 31 | gnome to skip back to where it left off after moving to the 32 | left, avoiding some iterations and comparisons. 33 |
34 |35 | With this optimization, gnome sort{" "} 36 | becomes a variant of{" "} 37 | insertion sort. 38 |
39 |
40 | Time Complexity: O(n*n)
41 |
42 | Auxiliary Space: O(1)
43 |
44 | In Place: Yes
45 |
46 | Stable: Yes
47 |
77 | Source: en-academic 78 |
79 | > 80 | ); 81 | } 82 | -------------------------------------------------------------------------------- /src/components/Descreptions/DescComponents/InsertionSortBinary.jsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import CodeComponent from "../CodeComponent"; 3 | 4 | export default function InsertionSortBinary() { 5 | return ( 6 | <> 7 |9 | In a simple insertion sort{" "} 10 | algorithm, we maintain a sorted and an unsorted subarray of the 11 | given array. In each iteration, one value from the unsorted part 12 | is picked and is inserted in its correct position in the sorted 13 | part. To achieve this, for every element, we iterate over the 14 | sorted part of the array to find the position to insert the 15 | element. 16 |
17 |18 | Binary insertion sort is a sorting 19 | algorithm similar to insertion sort, but instead of using linear 20 | search to find the position where the element should be 21 | inserted, we use binary search. 22 | Thus, we reduce the number of 23 | comparisons for inserting one element{" "} 24 | from O(N) to O(log N).{" "} 25 |
26 |27 | We first use binary search on the sorted subarray to find the 28 | position of the element that is{" "} 29 | just greater than our key. Let’s 30 | call this position “pos.” We then right shift all elements from 31 | position pos to i-1 and then make Array[pos] = key.{" "} 32 |
33 |
34 | Time Complexity: O(n*n)
35 |
36 | Auxilary Space: O(1)
37 |
38 | Stable: Yes
39 |
40 | In Place: Yes
41 |
77 | Source:{" "} 78 | 79 | interviewkickstart 80 | 81 |
82 | > 83 | ); 84 | } 85 | -------------------------------------------------------------------------------- /src/components/Descreptions/DescComponents/DoubleSelectionSort.jsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import CodeComponent from "../CodeComponent"; 3 | 4 | export default function DoubleSelectionSort() { 5 | return ( 6 | <> 7 |9 | Selection Sort is not known to be 10 | a good algorithm because it is an in-place comparison sort based 11 | sorting algorithm. However, efforts have been made to improve 12 | the performance of the algorithm particularly where auxiliary 13 | memory is limited. With{" "} 14 | Double Selection Sort, the average 15 | number of comparisons is slightly reduced. 16 |
17 |18 | The double selection sort starts 19 | from two elements and searches the 20 | entire list until it finds the{" "} 21 | minimum value and{" "} 22 | maximum value. The sort places the 23 | minimum value in the first place{" "} 24 | and maximum value in the{" "} 25 | last place, selects the second and 26 | second last element and searches for the second smallest and 27 | largest element. This process continues until the complete list 28 | is sorted. In other words, a take on an elementary sorting 29 | algorithm that is designed to minimize the number of exchanges 30 | that are performed. It works by making N-1 passes over the 31 | shrinking unsorted portion of the array, each time selecting the 32 | smallest and largest value. Those values are then moved into 33 | their final sorted position with one exchange a piece 34 |
35 |
36 | Time Complexity: O(n*n)
37 |
38 | Auxilary Space: O(1)
39 |
40 | Stable: Yes
41 |
42 | In Place: Yes
43 |
78 | Source:{" "} 79 | 80 | Improving the performance of selection sort using a modified 81 | double-ended selection sorting 82 | 83 |
84 | > 85 | ); 86 | } 87 | -------------------------------------------------------------------------------- /src/components/Descreptions/DescComponents/IntelligentDesignSort.jsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | 3 | export default function IntelligentDesignSort() { 4 | return ( 5 | <> 6 |8 | Intelligent design sort is a 9 | sorting algorithm based on the theory of{" "} 10 | 11 | intelligent design 12 | 13 | . 14 |
15 |17 | The probability of the original input list being in the exact 18 | order it's in is 1/(n!). There is such a small 19 | likelihood of this that it's clearly absurd to say that this 20 | happened by chance, so it must have been consciously put in that 21 | order by an intelligent Sorter. Therefore it's safe to 22 | assume that it's already optimally Sorted in some way that 23 | transcends our naïve mortal understanding of "ascending 24 | order". Any attempt to change that order to conform to our 25 | own preconceptions would actually make it less sorted. 26 |
27 |29 | This algorithm is constant in time, and sorts the list in-place, 30 | requiring no additional memory at all. In fact, it 31 | doesn't even require any of that suspicious 32 | technological computer stuff. Praise the Sorter! 33 |
34 |Gary Rogers writes:
36 |37 |45 |38 | Making the sort constant in time denies the power of The 39 | Sorter. The Sorter exists outside of time, thus the sort is 40 | timeless. To require time to validate the sort dimishes the 41 | role of the Sorter. Thus... this particular sort is flawed, 42 | and can not be attributed to 'The Sorter'. 43 |
44 |
Heresy!
46 |In contrast, Krishna Kumar writes:
47 |48 |61 |49 | A corollary: All elements are created equal under the 50 | Sorter. 51 |
52 |53 | Proof: Take a random permutation of the input list; this 54 | second list is also Sorted by the same argument as above. 55 | This means that any two elements in the two lists with the 56 | same index are equal to each other. But the second list was 57 | a random permutation; which indicates that every element in 58 | the original list is equal to every other element. QED. 59 |
60 |
So it seems the Sorter is benevolent in His munificence!
62 |
63 | Auxilary Space: SUPERB
64 |
65 | Auxilary Space: AWE-INSPIRING
66 |
67 | Stable: THOROUGHLY
68 |
69 | In Place: CERTAINLY
70 |
72 | Source:{" "} 73 | 74 | dangermouse 75 | 76 |
77 | > 78 | ); 79 | } 80 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import { createGlobalStyle } from "styled-components"; 2 | import Header from "./components/Header"; 3 | import SortSettings from "./components/SortSettings"; 4 | import Theme from "./Theme"; 5 | import Main from "./components/Main"; 6 | import React from "react"; 7 | import { Provider } from "react-redux"; 8 | import { store } from "./redux/Store"; 9 | import Descriptions from "./components/Descreptions/Container"; 10 | import Footer from "./components/Footer"; 11 | 12 | const GlobalStyles = createGlobalStyle` 13 | *{ 14 | box-sizing: border-box; 15 | } 16 | html, body{ 17 | overflow-x: hidden; 18 | overflow-y: auto; 19 | max-width: 100%; 20 | scroll-behavior: smooth; 21 | box-sizing: border-box; 22 | } 23 | button{ 24 | user-select: none; 25 | } 26 | .open-modal-1, .open-modal-2{ 27 | overflow: hidden; 28 | padding-right: 10px !important; 29 | } 30 | button, a{ 31 | transition: all 0.2s; 32 | 33 | &:hover, &:focus, a{ 34 | transition: all 0.2s; 35 | } 36 | } 37 | body { 38 | margin: 0; 39 | padding: 0; 40 | height: 100vh; 41 | width: 100vw; 42 | font-family: 'Montserrat', sans-serif; 43 | background: ${(props) => props.theme.colors.dark}; 44 | overflow-x: hidden; 45 | 46 | &::-webkit-scrollbar{ 47 | width: 10px; 48 | } 49 | &::-webkit-scrollbar-track{ 50 | background: ${(props) => props.theme.colors.dark}; 51 | } 52 | &::-webkit-scrollbar-thumb{ 53 | width: 7px; 54 | background: ${(props) => props.theme.colors.main}; 55 | border-radius: 5px; 56 | } 57 | } 58 | #root{ 59 | height: 100%; 60 | width: 100%; 61 | } 62 | ::-moz-selection { 63 | background: ${(props) => props.theme.colors.main}; 64 | color: ${(props) => props.theme.colors.light}; 65 | } 66 | ::-webkit-selection { 67 | background: ${(props) => props.theme.colors.main}; 68 | color: ${(props) => props.theme.colors.light}; 69 | } 70 | ::selection { 71 | background: ${(props) => props.theme.colors.main}; 72 | color: ${(props) => props.theme.colors.light}; 73 | } 74 | 75 | @media (max-width: 720px) { 76 | html{ 77 | font-size: 14px; 78 | } 79 | } 80 | @media (max-width: 630px) { 81 | html{ 82 | font-size: 12px; 83 | } 84 | } 85 | `; 86 | function App() { 87 | const [themeVariation, setThemeVariation] = React.useState("light"); 88 | const [width, setWidth] = React.useState(window.innerWidth); 89 | 90 | React.useEffect(() => { 91 | const handleWindowResize = () => setWidth(window.innerWidth); 92 | window.addEventListener("resize", handleWindowResize); 93 | 94 | return () => window.removeEventListener("resize", handleWindowResize); 95 | }, []); 96 | 97 | const toggleTheme = () => { 98 | setThemeVariation((prevTheme) => 99 | prevTheme === "light" ? "dark" : "light" 100 | ); 101 | }; 102 | return ( 103 |9 | The odd-even mergesort algorithm was developed by K.E. Batcher. 10 | It is based on a merge algorithm that merges two sorted halves 11 | of a sequence to a completely sorted sequence. 12 |
13 |14 | In contrast to mergesort, this algorithm is not data-dependent, 15 | i.e. the same comparisons are performed regardless of the actual 16 | data. Therefore, odd-even mergesort can be implemented as a 17 | sorting network 18 |
19 |20 | Comparator networks are abstract 21 | devices built up of a fixed number of "wires", 22 | carrying values, and comparator modules that connect pairs of 23 | wires, swapping the values on the wires if they are not in a 24 | desired order. Such networks are typically designed to perform 25 | sorting on fixed numbers of values, in which case they are 26 | called sorting networks. 27 |
28 |29 | Sorting networks differ from general comparison sortsin that 30 | they are not capable of handling arbitrarily large inputs, and 31 | in that their sequence of comparisons is set in advance, 32 | regardless of the outcome of previous comparisons. In order to 33 | sort larger amounts of inputs, new sorting networks must be 34 | constructed. This independence of comparison sequences is useful 35 | for parallel execution and for implementation in hardware. 36 | Despite the simplicity of sorting nets, their theory is 37 | surprisingly deep and complex. 38 |
39 |40 | Odd–even mergesort, to work properly needs the input's 41 | length to be equal to{" "} 42 | some power of 2{" "} 43 |
44 |
45 | Time Complexity: O(2n log n log n)
46 |
47 | Auxilary Space: O(2n log n log n)
48 |
49 | Stable: No
50 |
51 | In Place: No
52 |
86 | Source:{" "} 87 | 88 | hs-flensburg 89 | 90 |
91 | > 92 | ); 93 | } 94 | -------------------------------------------------------------------------------- /src/utils/SortGenerators/timSort.js: -------------------------------------------------------------------------------- 1 | import newElements from "../newElements"; 2 | 3 | export default function* timSort(items, arrMax) { 4 | let base = JSON.parse(JSON.stringify(items)); 5 | let arr = JSON.parse(JSON.stringify(base)); 6 | 7 | let changes = []; 8 | 9 | let MIN_MERGE = 32; 10 | 11 | function minRunLength(n) { 12 | let r = 0; 13 | while (n >= MIN_MERGE) { 14 | r |= n & 1; 15 | n >>= 1; 16 | } 17 | return n + r; 18 | } 19 | 20 | function insertionSort(arr, left, right) { 21 | for (let i = left + 1; i <= right; i++) { 22 | let temp = arr[i].val; 23 | let j = i - 1; 24 | 25 | while (j >= left && arr[j].val > temp) { 26 | arr[j + 1].val = arr[j].val; 27 | 28 | arr[j + 1].active = true; 29 | arr[j].active = true; 30 | changes.push(JSON.parse(JSON.stringify(arr))); 31 | arr[j + 1].active = false; 32 | arr[j].active = false; 33 | 34 | j--; 35 | } 36 | arr[j + 1].val = temp; 37 | 38 | arr[j + 1].active = true; 39 | arr[i].active = true; 40 | changes.push(JSON.parse(JSON.stringify(arr))); 41 | arr[j + 1].active = false; 42 | arr[i].active = false; 43 | } 44 | } 45 | 46 | function merge(arr, l, m, r) { 47 | let len1 = m - l + 1, 48 | len2 = r - m; 49 | let left = new Array(len1); 50 | let right = new Array(len2); 51 | for (let x = 0; x < len1; x++) { 52 | left[x] = arr[l + x].val; 53 | } 54 | for (let x = 0; x < len2; x++) { 55 | right[x] = arr[m + 1 + x].val; 56 | } 57 | 58 | let i = 0; 59 | let j = 0; 60 | let k = l; 61 | 62 | while (i < len1 && j < len2) { 63 | if (left[i] <= right[j]) { 64 | arr[k].val = left[i]; 65 | 66 | arr[k].active = true; 67 | changes.push(JSON.parse(JSON.stringify(arr))); 68 | arr[k].active = false; 69 | 70 | i++; 71 | } else { 72 | arr[k].val = right[j]; 73 | 74 | arr[k].active = true; 75 | changes.push(JSON.parse(JSON.stringify(arr))); 76 | arr[k].active = false; 77 | 78 | j++; 79 | } 80 | k++; 81 | } 82 | 83 | while (i < len1) { 84 | arr[k].val = left[i]; 85 | 86 | arr[k].active = true; 87 | changes.push(JSON.parse(JSON.stringify(arr))); 88 | arr[k].active = false; 89 | 90 | k++; 91 | i++; 92 | } 93 | 94 | while (j < len2) { 95 | arr[k].val = right[j]; 96 | 97 | arr[k].active = true; 98 | changes.push(JSON.parse(JSON.stringify(arr))); 99 | arr[k].active = false; 100 | 101 | k++; 102 | j++; 103 | } 104 | } 105 | 106 | function sortT(arr, n) { 107 | let minRun = minRunLength(MIN_MERGE); 108 | 109 | for (let i = 0; i < n; i += minRun) { 110 | insertionSort(arr, i, Math.min(i + MIN_MERGE - 1, n - 1)); 111 | } 112 | 113 | for (let size = minRun; size < n; size = 2 * size) { 114 | for (let left = 0; left < n; left += 2 * size) { 115 | let mid = left + size - 1; 116 | let right = Math.min(left + 2 * size - 1, n - 1); 117 | 118 | if (mid < right) { 119 | merge(arr, left, mid, right); 120 | } 121 | } 122 | } 123 | } 124 | let w = JSON.parse(JSON.stringify(arr)); 125 | sortT(w, w.length); 126 | 127 | console.log(w); 128 | 129 | for (let el of changes) { 130 | yield newElements(el.slice(0, base.length), arrMax); 131 | } 132 | 133 | for (let i = 0; i < base.length; i++) { 134 | w[i].active = true; 135 | yield newElements(w.slice(0, base.length), arrMax); 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /src/components/Descreptions/DescComponents/BogoSort.jsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import CodeComponent from "../CodeComponent"; 3 | 4 | export default function BogoSort() { 5 | return ( 6 | <> 7 |9 | Bogo sort is an algorithm used to 10 | sort the elements of an array by randomly generating different 11 | permutations of an array and then checking whether it is sorted 12 | or not. It’s based on the generate-and-test paradigm. Other 13 | names for bogo sort include permutation sort, stupid sort, slow 14 | sort, shotgun sort, or monkey sort. 15 |
16 |
18 | Step 1: Check if the array is
19 | already sorted or not. If yes, then print the array, else
20 |
21 | Step 2: Generate a random
22 | permutation (not necessarily different from previously
23 | generated) and go to Step 1.
24 |
33 | It is not guaranteed that we get the sorted permutation at some 34 | point after shuffling the array a certain number of times. 35 | Although its probability is infinitesimally low, there’s a 36 | chance that we do not get the sorted order even after gazillions 37 | of shuffling.{" "} 38 |
39 |46 | There are n! permutations, only one of which is sorted. So, we 47 | would expect to get the correct answer after about O(n!) 48 | iterations. And each shuffle/check operation is itself O(n).{" "} 49 |
50 |57 | When the given array is already sorted, the program terminates 58 | just after checking if the array is sorted once, which takes 59 | O(n) time to execute. 60 |
61 |
62 | Auxilary Space: O(1)
63 |
64 | Stable: No
65 |
66 | In Place: Yes
67 |
95 | Source:{" "} 96 | 97 | interviewkickstart 98 | 99 |
100 | > 101 | ); 102 | } 103 | -------------------------------------------------------------------------------- /src/components/Descreptions/DescComponents/MergeSortBottomUp.jsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import CodeComponent from "../CodeComponent"; 3 | 4 | export default function MergeSortBottomUp() { 5 | return ( 6 | <> 7 |9 | Merge sort is an efficient sorting 10 | algorithm that falls under the Divide and Conquer paradigm and 11 | produces a stable sort. It operates by dividing a large array 12 | into two smaller subarrays and then recursively sorting the 13 | subarrays. 14 |
15 |16 | In a recursive approach, the problem is broken down into 17 | smaller, simple subproblems in a top-down manner until the 18 | solution becomes trivial. 19 |
20 |21 | We can also implement merge sort iteratively in a{" "} 22 | bottom-up manner. We start by 23 | sorting all subarrays of 1{" "} 24 | element; then merge results into subarrays of{" "} 25 | 2 elements, then merge results 26 | into subarrays of 4 elements. 27 | Likewise, perform successive merges until the array is 28 | completely sorted. 29 |
30 |
31 | Time Complexity: O(n log n)
32 |
33 | Auxilary Space: O(n)
34 |
35 | Stable: Yes
36 |
37 | In Place: No
38 |
96 | Source:{" "} 97 | 98 | techiedelight 99 | 100 |
101 | > 102 | ); 103 | } 104 | -------------------------------------------------------------------------------- /src/components/Descreptions/DescComponents/PancakeSort.jsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import CodeComponent from "../CodeComponent"; 3 | 4 | export default function PancakeSort() { 5 | return ( 6 | <> 7 |9 | Pancake sort is a sorting 10 | algorithm in which the only allowed operation is to{" "} 11 | "flip" one end of the 12 | list. Pancake sort is called so because it resembles sorting 13 | pancakes on a plate with a spatula, where you can only use the 14 | spatula to flip some of the top pancakes in the plate. 15 |
16 |17 | Unlike traditional sorting algorithms, which attempt to sort 18 | with the fewest comparisons, pancake sort tries to sort the 19 | sequence in as few reversals as possible. 20 |
21 |22 | The idea behind pancake sort is similar to Selection Sort. In 23 | every iteration, we find the maximum element in the sequence and 24 | place it at the end, and reduce the size of the sequence by one. 25 |
26 |Given an array of integers Arr:
27 |40 | Let the given array be Arr[] and size of the array be 41 | 'n' 42 |
43 |
60 |
64 |
66 | Time Complexity: O(n flips)
67 |
68 | Auxilary Space: O(1)
69 |
70 | Stable: No
71 |
72 | In Place: Yes
73 |
110 | Source:{" "} 111 | OpenGenus 112 |
113 | > 114 | ); 115 | } 116 | -------------------------------------------------------------------------------- /src/components/Descreptions/DescComponents/BozoSort.jsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import CodeComponent from "../CodeComponent"; 3 | 4 | export default function BozoSort() { 5 | return ( 6 | <> 7 |9 | BozoSort is a random sorting 10 | algorithms where the key idea is to swap{" "} 11 | any two elements of the list{" "} 12 | randomly and check if the list is 13 | sorted. It is similar to BogoSort{" "} 14 | with distinct differences. 15 |
16 |18 | The key idea of Bozosort is the put more intelligence in the 19 | algorithm than BogoSort. For this, in Bozosort, we randomly swap 20 | two elements at a time and check if the list is sorted. 21 |
22 |23 | If the correct sets of elements are swapped, the list is infact 24 | sorted. Following are the general steps: 25 |
26 |44 | It is not guaranteed that we get the sorted permutation at some 45 | point after switching the elements a certain number of times. 46 | Although its probability is infinitesimally low, there’s a 47 | chance that we do not get the sorted order even after gazillions 48 | of switches.{" "} 49 |
50 |57 | There are n! permutations, only one of which is sorted. So, we 58 | would expect to get the correct answer after about O(n!) 59 | iterations. And each shuffle/check operation is itself O(n).{" "} 60 |
61 |68 | When the given array is already sorted, the program terminates 69 | just after checking if the array is sorted once, which takes 70 | O(n) time to execute. 71 |
72 |
73 | Auxilary Space: O(1)
74 |
75 | Stable: No
76 |
77 | In Place: Yes
78 |
104 | Source:{" "} 105 | OpenGenus 106 |
107 | > 108 | ); 109 | } 110 | -------------------------------------------------------------------------------- /src/components/Descreptions/DescComponents/PairwiseSortingNetwork.jsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import CodeComponent from "../CodeComponent"; 3 | 4 | export default function PairwiseSortingNetwork() { 5 | return ( 6 | <> 7 |9 | The pairwise sorting network is a{" "} 10 | sorting network discovered and 11 | published by Ian Parberry in 1992 in Parallel Processing 12 | Letters. The pairwise sorting network has the same size (number 13 | of comparators) and depth as the odd-even mergesort network. The 14 | network was one of several known networks with a depth of O(2 15 | log n). 16 |
17 |18 | The sorting procedure implemented by the network is as follows: 19 |
20 |36 | The pairwise sorting network is very similar to the Batcher 37 | odd-even mergesort, but differs in the structure of operations. 38 | While Batcher repeatedly divides, sorts and merges increasingly 39 | longer subsequences, the pairwise method does all the 40 | subdivision first, then does all the merging at the end in the 41 | reverse sequence. In certain applications like encoding 42 | cardinality constraints, the pairwise sorting network is 43 | superior to the Batcher network. 44 |
45 |
46 | Time Complexity: O(2n log n log n)
47 |
48 | Auxilary Space: O(2n log n log n)
49 |
50 | Stable: No
51 |
52 | In Place: No
53 |
107 | Source:{" "} 108 | 109 | Wikipedia 110 | 111 |
112 | > 113 | ); 114 | } 115 | -------------------------------------------------------------------------------- /src/components/Descreptions/DescComponents/CycleSort.jsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import CodeComponent from "../CodeComponent"; 3 | 4 | export default function CycleSort() { 5 | return ( 6 | <> 7 |9 | Cycle sort is a comparison sorting 10 | algorithm that forces array to be factored into the number of 11 | cycles where each of them can be{" "} 12 | rotated to produce a sorted array. 13 | It is theoretically optimal in the sense that it reduces the 14 | number of writes to the original array. 15 |
16 |17 | Cycle sort forces an array to be factored into a number of 18 | cycles where every element can rotate in order to produce a 19 | sorted array. 20 |
21 |23 | Given an element A, we can find 24 | its index by counting the number of elements smaller than{" "} 25 | A. 26 |
27 |
44 |
48 |
50 | Time Complexity: O(n*n)
51 |
52 | Auxilary Space: O(1)
53 |
54 | Stable: No
55 |
56 | In Place: Yes
57 |
116 | Source:{" "} 117 | javatpoint 118 |
119 | > 120 | ); 121 | } 122 | -------------------------------------------------------------------------------- /src/components/Descreptions/DescComponents/InsertionSort.jsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import CodeComponent from "../CodeComponent"; 3 | 4 | export default function InsertionSort() { 5 | return ( 6 | <> 7 |9 | Insertion sort is a simple sorting 10 | algorithm that works similar to the way you sort playing cards 11 | in your hands. The array is virtually split into a{" "} 12 | sorted and an{" "} 13 | unsorted part. Values from the 14 | unsorted part are picked and placed at the correct position in 15 | the sorted part. 16 |
17 |
18 |
22 |
Suppose we need to sort the following array.
25 |
26 |
31 |
35 | The first element in the array is assumed to be sorted.
36 | Take the second element and store it separately in{" "}
37 | key.{" "}
38 |
40 | Compare key with the first element. If the
41 | first element is greater than key, then key
42 | is placed in front of the first element.
43 |
45 |
50 |
Now, the first two elements are sorted.
54 |55 | Take the third element and compare it with the elements 56 | on the left of it. Placed it just behind the element 57 | smaller than it. If there is no element smaller than it, 58 | then place it at the beginning of the array. 59 |
60 |
61 |
66 |
70 | Similarly, place every unsorted element at its correct 71 | position. 72 |
73 |
74 |
79 |
81 |
86 |
110 | Time Complexity: O(n*n)
111 |
112 | Auxilary Space: O(1)
113 |
114 | Stable: Yes
115 |
116 | In Place: Yes
117 |
119 | Source:{" "} 120 | 121 | Programiz 122 | 123 |
124 | > 125 | ); 126 | } 127 | -------------------------------------------------------------------------------- /src/components/Descreptions/DescComponents/HeapSort.jsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import CodeComponent from "../CodeComponent"; 3 | 4 | export default function HeapSort() { 5 | return ( 6 | <> 7 |9 | Heap sort is an efficient 10 | comparison-based sorting algorithm that creates a{" "} 11 | heap from the input array and then 12 | sorts the array by taking advantage of a heap's properties.{" "} 13 | 14 | Heap sort 15 | {" "} 16 | processes the elements by creating the min-heap or max-heap 17 | using the elements of the given array.{" "} 18 | Min-heap or{" "} 19 | max-heap represents the ordering 20 | of array in which the root element represents the minimum or 21 | maximum element of the array. 22 |
23 |
48 |
53 |
55 | Time Complexity: O(n log n)
56 |
57 | Auxilary Space: O(1)
58 |
59 | Stable: No
60 |
61 | In Place: Yes
62 |
137 | Source:{" "} 138 | Programiz 139 |
140 | > 141 | ); 142 | } 143 | -------------------------------------------------------------------------------- /src/components/Descreptions/DescComponents/ShellSort.jsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import CodeComponent from "../CodeComponent"; 3 | 4 | export default function ShellSort() { 5 | return ( 6 | <> 7 |9 | Shell sort is a highly efficient 10 | sorting algorithm and is based on{" "} 11 | insertion sort algorithm. This 12 | algorithm avoids large shifts as in case of insertion sort, if 13 | the smaller value is to the far right and has to be moved to the 14 | far left. 15 |
16 |17 | This algorithm uses insertion sort on a widely spread elements, 18 | first to sort them and then sorts the less widely spaced 19 | elements. This spacing is termed as{" "} 20 | interval. 21 |
22 |24 | Let us consider the following example to have an idea of how 25 | shell sort works. We take the same array we have used in our 26 | previous examples. For our example and ease of understanding, we 27 | take the interval of 4. Make a virtual sub-list of all values 28 | located at the interval of 4 positions. Here these values are{" "} 29 | {(35, 14)}, {(33, 19)}, {(42, 27)} and {(10, 44)} 30 |
31 |
32 |
36 |
38 | We compare values in each sub-list and swap them (if necessary) 39 | in the original array. After this step, the new array should 40 | look like this − 41 |
42 |
43 |
47 |
49 | Then, we take interval of 1 and this gap generates two sub-lists 50 | - {(14, 27, 35, 42)}, {(19, 10, 33, 44)} 51 |
52 |
53 |
57 |
59 | We compare and swap the values, if required, in the original 60 | array. After this step, the array should look like this − 61 |
62 |
63 |
67 |
69 | Finally, we sort the rest of the array using interval of value 70 | 1. Shell sort uses insertion sort to sort the array. 71 |
72 |Following is the step-by-step depiction −
73 |
74 |
78 |
80 | We see that it required only four swaps to sort the rest of the 81 | array. 82 |
83 |
84 | Time Complexity: O(n log n)
85 |
86 | Auxilary Space: O(1)
87 |
88 | Stable: Yes
89 |
90 | In Place: Yes
91 |
126 | Source:{" "} 127 | 128 | tutorialspoint 129 | 130 |
131 | > 132 | ); 133 | } 134 | -------------------------------------------------------------------------------- /src/components/Descreptions/DescComponents/CircleSort.jsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import CodeComponent from "../CodeComponent"; 3 | 4 | export default function CircleSort() { 5 | return ( 6 | <> 7 |9 | Circle sort is a sorting algorithm 10 | in which concentric circles are 11 | made on array and the elements lying on the same circle 12 | diametrically opposite to each other are compared and if the 13 | element in left side is found to be greater than that of right 14 | side then they are swapped. The above process is repeated in the{" "} 15 | recursive manner and the array is 16 | divided into sub arrays until we get the array of sorted pairs. 17 |
18 |19 | It's one of the fastest ways 20 | to sort an inverted array. 21 |
22 |37 | Assume the Unsorted input array is:{" "} 38 | [6,5,3,1,8,7,2,4]. 39 |
40 |41 | Based on the above algorithm we first compare the first and the 42 | last element and then the second and second the last element and 43 | so on until all the elements get compared, and then we divide 44 | the array int two sub-arrays and make the same comparison until 45 | we get the sorted array. 46 |
47 |48 | Step 1: Compare i th element with 49 | n-i th element and swap if there are not in the correct order 50 |
51 |
52 |
56 |
58 | Step 2: Split the array into two 59 | parts of equal size and follow the above step for each part 60 |
61 |
62 |
66 |
68 | Step 3: Split each part into 69 | another two parts and follow the same comparison 70 |
71 |
72 |
76 |
78 | Step 4: On splitting the array 79 | into equal parts, each part is of size 1 and hence, we have a 80 | sorted array 81 |
82 |
83 |
87 |
89 | Time Complexity: O(n log n)
90 |
91 | Auxiliary Space: O(1)
92 |
93 | Stable: No
94 |
95 | In Place: Yes
96 |
134 | Source:{" "} 135 | OpenGenus 136 |
137 | > 138 | ); 139 | } 140 | -------------------------------------------------------------------------------- /src/components/Descreptions/DescComponents/TimSort.jsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import CodeComponent from "../CodeComponent"; 3 | 4 | export default function TimSort() { 5 | return ( 6 | <> 7 |9 | Tim sort is an adaptive sorting 10 | algorithm that needs O(n log n){" "} 11 | comparisons to sort an array of n{" "} 12 | elements. It was designed and implemented by Tim Peters in 2002 13 | in a python programming language. It has been python's 14 | standard sorting algorithm since version 2.3. 15 |
16 |17 | The basic approach used in the Tim sort algorithm is - first 18 | sort small chunks by using the insertion sort and then merge all 19 | the big chunks using the merge function of the merge sort. 20 |
21 |40 | In Tim sort, first the array is divided into small chunks that 41 | are known as RUN. After dividing, 42 | each individual RUN is taken, and 43 | sorted using the insertion sort technique. After that, all 44 | sorted RUNs are merged using the merge() function of the merge 45 | sort algorithm. 46 |
47 |48 | In Tim sort, the advantage of using the insertion sort is that 49 | insertion sort works efficiently for the array with small size. 50 |
51 |
52 | Time Complexity: O(n log n)
53 |
54 | Auxilary Space: O(n)
55 |
56 | Stable: Yes
57 |
58 | In Place: Yes
59 |
151 | Source:{" "} 152 | javatpoint 153 |
154 | > 155 | ); 156 | } 157 | -------------------------------------------------------------------------------- /src/utils/getSortingMethod.js: -------------------------------------------------------------------------------- 1 | import bubbleSort from "./SortGenerators/bubbleSort"; 2 | import selectionSort from "./SortGenerators/selectionSort"; 3 | import insertionSort from "./SortGenerators/insertionSort"; 4 | import mergeSort from "./SortGenerators/mergeSort"; 5 | import mergeSortBottomUp from "./SortGenerators/mergeSortBottomUp"; 6 | import bubbleSortOptimized from "./SortGenerators/bubbleSortOtimized"; 7 | import cocktailShakerSort from "./SortGenerators/cocktailShakerSort"; 8 | import oddEvenSort from "./SortGenerators/oddEvenSort"; 9 | import gnomeSort from "./SortGenerators/gnomeSort"; 10 | import gnomeSortOptimized from "./SortGenerators/gnomeSortOptimized"; 11 | import combSort from "./SortGenerators/combSort"; 12 | import circleSort from "./SortGenerators/circleSort"; 13 | import quickSortRightPivot from "./SortGenerators/quickSortRightPivot"; 14 | import quickSortLeftPivot from "./SortGenerators/quickSortLeftPivot"; 15 | import quickSortMiddlePivot from "./SortGenerators/quickSortMiddlePivot"; 16 | import quickSortStable from "./SortGenerators/quickSortStable"; 17 | import doubleSelectionSort from "./SortGenerators/doubleSelectionSort"; 18 | import cycleSort from "./SortGenerators/cycleSort"; 19 | import heapSortMax from "./SortGenerators/hepSortMax"; 20 | import heapSortMin from "./SortGenerators/heapSortMin"; 21 | import insertionSortBinary from "./SortGenerators/insertionSortBinary"; 22 | import shellSort from "./SortGenerators/shellSort"; 23 | import countingSort from "./SortGenerators/countingSort"; 24 | import gravitySort from "./SortGenerators/gravitySort"; 25 | import bucketSort from "./SortGenerators/bucketSort"; 26 | import radixSortBase10 from "./SortGenerators/radixSortBase10"; 27 | import radixSortBase5 from "./SortGenerators/radixSortBase5"; 28 | import radixSortBase2 from "./SortGenerators/radixSortBase2"; 29 | import flashSort from "./SortGenerators/flashSort"; 30 | import bitonicSort from "./SortGenerators/bitonicSort"; 31 | import oddEvenMergeSort from "./SortGenerators/oddEvenMergeSort"; 32 | import pairwiseSortingNetwork from "./SortGenerators/pairwiseSortingNetwork"; 33 | import timSort from "./SortGenerators/timSort"; 34 | import pancakeSort from "./SortGenerators/pancakeSort"; 35 | import bogoSort from "./SortGenerators/bogoSort"; 36 | import bubbleBogoSort from "./SortGenerators/bubbleBogoSort"; 37 | import bozoSort from "./SortGenerators/bozoSort"; 38 | 39 | export default function getSortingMethod(sortingMethod) { 40 | switch (sortingMethod) { 41 | case "BubbleSort": 42 | return bubbleSort; 43 | case "SelectionSort": 44 | return selectionSort; 45 | case "InsertionSort": 46 | return insertionSort; 47 | case "MergeSort": 48 | return mergeSort; 49 | case "MergeSortBottomUp": 50 | return mergeSortBottomUp; 51 | case "BubbleSortOptimized": 52 | return bubbleSortOptimized; 53 | case "CocktailShakerSort": 54 | return cocktailShakerSort; 55 | case "OddEvenSort": 56 | return oddEvenSort; 57 | case "GnomeSort": 58 | return gnomeSort; 59 | case "GnomeSortOptimized": 60 | return gnomeSortOptimized; 61 | case "CombSort": 62 | return combSort; 63 | case "CircleSort": 64 | return circleSort; 65 | case "QuickSortRightPivot": 66 | return quickSortRightPivot; 67 | case "QuickSortLeftPivot": 68 | return quickSortLeftPivot; 69 | case "QuickSortMiddlePivot": 70 | return quickSortMiddlePivot; 71 | case "QuickSortStable": 72 | return quickSortStable; 73 | case "DoubleSelectionSort": 74 | return doubleSelectionSort; 75 | case "CycleSort": 76 | return cycleSort; 77 | case "HeapSortMax": 78 | return heapSortMax; 79 | case "HeapSortMin": 80 | return heapSortMin; 81 | case "InsertionSortBinary": 82 | return insertionSortBinary; 83 | case "ShellSort": 84 | return shellSort; 85 | case "CountingSort": 86 | return countingSort; 87 | case "GravitySort": 88 | return gravitySort; 89 | case "BucketSort": 90 | return bucketSort; 91 | case "RadixSortBase10": 92 | return radixSortBase10; 93 | case "RadixSortBase5": 94 | return radixSortBase5; 95 | case "RadixSortBase2": 96 | return radixSortBase2; 97 | case "FlashSort": 98 | return flashSort; 99 | case "BitonicSort": 100 | return bitonicSort; 101 | case "OddEvenMergeSort": 102 | return oddEvenMergeSort; 103 | case "PairwiseSortingNetwork": 104 | return pairwiseSortingNetwork; 105 | case "TimSort": 106 | return timSort; 107 | case "PancakeSort": 108 | return pancakeSort; 109 | case "BogoSort": 110 | return bogoSort; 111 | case "BozoSort": 112 | return bozoSort; 113 | case "BubbleBogoSort": 114 | return bubbleBogoSort; 115 | default: 116 | return bubbleSort; 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /src/components/Header/InfoModal.jsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import ReactDOM from "react-dom"; 3 | import CloseIcon from "@mui/icons-material/Close"; 4 | import InfoModalStyled from "./InfoModal.styles"; 5 | import { motion, AnimatePresence } from "framer-motion"; 6 | 7 | export default function InfoModal({ open, setOpen }) { 8 | const outside = React.useRef(null); 9 | 10 | function onClose(e) { 11 | if (outside.current === e.target) { 12 | console.log("c"); 13 | setOpen(false); 14 | } 15 | } 16 | 17 | return ReactDOM.createPortal( 18 |36 | SORT DEMON is a 37 | visualizer of sorting algorithms. It is meant to be 38 | used as a tool{" "} 39 | 40 | for learning how the algorithms act and how they 41 | contrast from one another 42 | 43 | . I did not design it 44 | to accurately show the relative speed of the 45 | algorithms, since I deemed it would make the faster 46 | algorithms{" "} 47 | less “readable”. 48 | Please keep that in mind while using{" "} 49 | SORT DEMON. 50 |
51 |53 | There can be up to 9{" "} 54 | algorithms running at the same time. If after adding 55 | a new algorithm,{" "} 56 | only its name appears, 57 | just click the RESET{" "} 58 | or SHUFFLE button to 59 | show it entirely. Also, note that{" "} 60 | removing an algorithm{" "} 61 | 62 | in the middle of the runtime 63 | 64 | can lead to some strange behavior that is also 65 | easily fixed with a{" "} 66 | RESET or{" "} 67 | SHUFFLE. Another thing 68 | - the more structurally{" "} 69 | complex algorithms{" "} 70 | (like the ones using recursion) have to run in their 71 | entirety before{" "} 72 | recreating their steps on the screen. I say that 73 | because that initial run in the background can lead 74 | to a slight lag when 75 | starting the visualization with a{" "} 76 | large array size and{" "} 77 | 78 | multiple of these complex algorithms 79 | 80 | . 81 |
82 |84 | I do not take credit 85 | for any of the{" "} 86 | descriptions’ contents{" "} 87 | - proper sources are linked at the bottom of each 88 | description. This is a{" "} 89 | personal project - the 90 | description functionality is just a{" "} 91 | feature that I thought 92 | would be nice to implement and thought that 93 | authentic articles would look better than some Lorem 94 | Ipsum boilerplate text. I{" "} 95 | strongly recommend 96 | everyone to visit the websites from which I go the 97 | articles - all of them are{" "} 98 | great resources for 99 | learning computer science-related topics. 100 |
101 |102 | Feel free to contact me if you have any feedback 103 | regarding the site. 104 |
105 |106 | My contact links are in the footer 107 |
108 |9 | Comb sort is a relatively simple 10 | sorting algorithm originally designed by Włodzimierz Dobosiewicz 11 | and Artur Borowy in 1980. Comb sort improves on{" "} 12 | bubble sort in the same way that{" "} 13 | shellsort improves on{" "} 14 | insertion sort. 15 |
16 |17 | The basic idea is to eliminate{" "} 18 | 19 | turtles 20 | 21 | , or small values near the end of the list, since in a bubble 22 | sort these slow the sorting down tremendously.{" "} 23 | 24 | Rabbits 25 | 26 | , large values around the beginning of the list, do not pose a 27 | problem in bubble sort. 28 |
29 |30 | In bubble sort, when any two 31 | elements are compared, they always have a{" "} 32 | 33 | gap 34 | {" "} 35 | (distance from each other) of 1. The basic idea of{" "} 36 | comb sort is that the gap can be 37 | much more than 1. The inner loop of bubble sort, which does the 38 | actual swap, is modified such that the gap between 39 | swapped elements goes down (for each iteration of outer loop) in 40 | steps of a "shrink factor 41 | " k: [ n/k, n/_k_2,{" "} 42 | n/_k_3, ..., 1 ]. 43 |
44 |45 | The gap starts out as the length 46 | of the list n being sorted{" "} 47 | 48 | divided by the shrink factor k 49 | {" "} 50 | and one pass of the aforementioned modified bubble sort is 51 | applied with that gap. Then the gap is divided by the shrink 52 | factor again, the list is sorted with this new gap, and the 53 | process repeats until the gap is 1. At this point, comb sort 54 | continues using a gap of 1 until the list is fully sorted. The 55 | final stage of the sort is thus equivalent to a bubble sort, but 56 | by this time most turtles have been dealt with, so a bubble sort 57 | will be efficient. 58 |
59 |
60 |
64 |
66 | The shrink factor has a great effect on the efficiency of comb 67 | sort.{" "} 68 | 69 | k = 1.3 70 | {" "} 71 | has been suggested as an ideal shrink factor by the authors of 72 | the original article after empirical testing on over 200,000 73 | random lists. A value too small slows the algorithm down by 74 | making unnecessarily many comparisons, whereas a value too large 75 | fails to effectively deal with turtles, making it require many 76 | passes with 1 gap size. 77 |
78 |79 | The pattern of repeated sorting passes with decreasing gaps is{" "} 80 | similar to Shellsort, but in 81 | Shellsort the array is sorted completely each pass before going 82 | on to the next-smallest gap. Comb sort's passes do not 83 | completely sort the elements. This is the reason that Shellsort 84 | gap sequences have a larger optimal shrink factor of about 2.2. 85 |
86 |
87 | Time Complexity: O(n*n)
88 |
89 | Auxiliary Space: O(1)
90 |
91 | In Place: Yes
92 |
93 | Stable: No
94 |
120 | Source:{" "} 121 | Wikipedia 122 |
123 | > 124 | ); 125 | } 126 | -------------------------------------------------------------------------------- /src/components/Descreptions/DescComponents/GravitySort.jsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import CodeComponent from "../CodeComponent"; 3 | 4 | export default function GravitySort() { 5 | return ( 6 | <> 7 |9 | Gravity sort (also called{" "} 10 | bead sort) is a natural sorting 11 | algorithm. Both digital and analog hardware implementations of 12 | bead sort can achieve a sorting time of O(n); however, the 13 | implementation of this algorithm tends to be significantly 14 | slower in software and can{" "} 15 | 16 | only be used to sort lists of positive integers 17 | 18 | . 19 |
20 |21 | This is a perfect example of an algorithm where{" "} 22 | 23 | the hardware implementation is significantly faster than the 24 | software implementation 25 | {" "} 26 | is to contrary to the common belief that software has to be 27 | faster than corresponding hardware (think of{" "} 28 | mechanical calculators) 29 |
30 |31 | This is because sorting step is done by Gravity (a natural 32 | physical force) and this step takes place in parrallel in one 33 | step. In software, this step takes a minimum of m clock cycles. 34 |
35 |37 | The bead sort operation can be compared to the manner in which 38 | beads slide on parallel poles, such as on an{" "} 39 | abacus. However, each pole may 40 | have a distinct number of beads. Initially, it may be helpful to 41 | imagine the beads suspended on vertical poles. 42 |
43 |
44 |
48 |
50 | Sorting of {(7, 2, 1, 4, 2)} using Gravity Sort. Beads fall down 51 | one by one if there is space below. 52 |
53 |
82 | Time Complexity: O(n)
83 |
84 | Auxilary Space: O(n*n)
85 |
124 | Source:{" "} 125 | OpenGenus,{" "} 126 | 127 | GeeksforGeeks 128 | 129 |
130 | > 131 | ); 132 | } 133 | -------------------------------------------------------------------------------- /src/components/Descreptions/DescComponents/RadixSort.jsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import CodeComponent from "../CodeComponent"; 3 | 4 | export default function RadixSort() { 5 | return ( 6 | <> 7 |9 | Radix sort is a sorting algorithm 10 | that sorts the elements by first grouping the individual digits 11 | of the same place value. Then, 12 | sort the elements according to their increasing/decreasing 13 | order. 14 |
15 |17 | Base specifies in which{" "} 18 | number system will our numbers be 19 | compared digit by digit. Radix sort with base 10 (below) - 20 | decimal system; Base 2 - binary; Base 5 - quinary 21 |
22 |23 | Suppose, we have an array of 8 elements. First, we will sort 24 | elements based on the value of the unit place. Then, we will 25 | sort elements based on the value of the tenth place. This 26 | process goes on until the last significant place. 27 |
28 |
29 | Let the initial array be{" "}
30 | [121, 432, 564, 23, 1, 45, 788]. It is sorted
31 | according to radix sort as shown in the figure below.
32 |
34 |
39 |
44 | Find the largest element in the array, i.e. max. Let{" "}
45 | X be the number of digits in{" "}
46 | max. X is calculated because
47 | we have to go through all the significant places of all
48 | elements.{" "}
49 |
51 | In this array{" "}
52 | [121, 432, 564, 23, 1, 45, 788], we have
53 | the largest number 788. It has 3 digits. Therefore, the
54 | loop should go up to hundreds place (3 times).
55 |
Now, go through each significant place one by one.
59 |60 | Use any stable sorting technique to sort the digits at 61 | each significant place. We have used counting sort for 62 | this.{" "} 63 |
64 |
65 | Sort the elements based on the unit place digits (
66 | X=0).
67 |
69 |
74 |
Now, sort the elements based on digits at tens place.
78 |
79 |
84 |
88 | Finally, sort the elements based on the digits at 89 | hundreds place. 90 |
91 |
92 |
97 |
101 | Time Complexity: O(n)
102 |
103 | Auxilary Space: O(n)
104 |
105 | Stable: Yes
106 |
107 | In Place: No
108 |
9 | Selection sort is a sorting algorithm that divides the input 10 | list into two parts: a sorted sublist of items which is built up 11 | from left to right at the front (left) of the list and a sublist 12 | of the remaining unsorted items that occupy the rest of the 13 | list. Initially, the sorted sublist is empty and the unsorted 14 | sublist is the entire input list. The algorithm proceeds by 15 | finding the smallest (or largest, depending on sorting order) 16 | element in the unsorted sublist, exchanging (swapping) it with 17 | the leftmost unsorted element (putting it in sorted order), and 18 | moving the sublist boundaries one element to the right. 19 |
20 |
24 | Set the first element as minimum.
25 |
27 |
32 |
36 | Compare minimum with the second element. If
37 | the second element is smaller than minimum,
38 | assign the second element as minimum.
39 | Compare minimum with the third element.
40 | Again, if the third element is smaller, then assign{" "}
41 | minimum to the third element otherwise do
42 | nothing. The process goes on until the last element.
43 |
45 |
50 |
54 | After each iteration, minimum is placed in
55 | the front of the unsorted list.
56 |
58 |
63 |
67 | For each iteration, indexing starts from the first 68 | unsorted element. Step 1 to 3 are repeated until all the 69 | elements are placed at their correct positions. 70 |
71 |
72 |
77 |
79 |
84 |
86 |
91 |
93 |
98 |
102 | Time Complexity: O(n*n)
103 |
104 | Auxilary Space: O(1)
105 |
106 | Stable: No
107 |
108 | In Place: Yes
109 |
132 | Source:{" "} 133 | 134 | Programiz 135 | 136 |
137 | > 138 | ); 139 | } 140 | --------------------------------------------------------------------------------