diff --git a/src/App.tsx b/src/App.tsx index c7bbe49ecbe98c3d8a102e362382d34a76f10423..6175ccd851558a40f7ffe20dad91eab4714d9f35 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,7 @@ import * as React from 'react'; import logo from './logo.svg'; import './App.css'; -import { SelectionPreview, BubblePreview, InsertionPreview, ShellPreview, CountingPreview } from './components/Preview/Preview'; +import { SelectionPreview, BubblePreview, InsertionPreview, CombPreview, ShellPreview, CountingPreview } from './components/Preview/Preview'; //import CountingPreview from './components/Preview/Counting'; import { BrowserRouter as Router, @@ -32,6 +32,9 @@ function App() { <li> <Link to="/counting">Counting</Link> </li> + <li> + <Link to="/comb">Comb</Link> + </li> <li> <Link to="/shell">Shell</Link> </li> @@ -81,6 +84,13 @@ function App() { </header> </div> </Route> + <Route path="/comb"> + <div className="App"> + <header className="App-header"> + <CombPreview /> + </header> + </div> + </Route> <Route path="/"> <Home /> </Route> diff --git a/src/components/Preview/Preview.tsx b/src/components/Preview/Preview.tsx index 5f3afb42394b7eab31b133fb3216812c61d48678..112f50b21e4a42c80b1da7c2b7117194c6ecd0a9 100644 --- a/src/components/Preview/Preview.tsx +++ b/src/components/Preview/Preview.tsx @@ -1,6 +1,6 @@ import { equals } from 'ramda'; import { useState } from 'react'; -import { useSelectionSort, useBubbleSort, useInsertionSort, useShellSort, useCountingSort } from '../../hooks/algorithm'; +import { useSelectionSort, useBubbleSort, useInsertionSort, useCombSort, useShellSort, useCountingSort } from '../../hooks/algorithm'; import { calculateColumnWidth, getRandomArray, getArray } from '../useFunctions/useFunctions'; import { stringToArray } from '../../util/inputList'; import { nearlySortedRandomList, reversedNearlySortedRandomList } from '../../util/generator'; @@ -204,13 +204,15 @@ function Preview({ useSort } : PreviewProps) { const SelectionPreview = withSort(Preview, useSelectionSort); const InsertionPreview = withSort(Preview, useInsertionSort); const BubblePreview = withSort(Preview, useBubbleSort); -const ShellPreview = withSort(Preview, useShellSort); -const CountingPreview = withSort(Preview, useCountingSort); +const CombPreview = withSort(Preview, useCombSort); +const ShellPreview = withSort(Preview, useShellSort); +const CountingPreview = withSort(Preview, useCountingSort); export { SelectionPreview, InsertionPreview, BubblePreview, + CombPreview, ShellPreview, CountingPreview } diff --git a/src/hooks/algorithm.ts b/src/hooks/algorithm.ts index ab64691650934e1d4a3735dd92351faa5951d62a..764ed01d830bd453cd3e8139c891e6972e3ace4b 100644 --- a/src/hooks/algorithm.ts +++ b/src/hooks/algorithm.ts @@ -1,6 +1,6 @@ import * as React from 'react'; import { compose } from 'ramda'; -import { bubbleSortStep, insertionSortStep, selectionSortStep, shellSortStep, countingSortStep } from '../util/algorithm'; +import { bubbleSortStep, insertionSortStep, selectionSortStep, combSortStep, shellSortStep, countingSortStep } from '../util/algorithm'; import type { StepState } from '../util/algorithm'; @@ -47,7 +47,8 @@ function getBubbleInitialState(list: number[]) : StepState { flag: false, secondFlag: false, interval: 1, - temp: 0 + temp: 0, + gap: 0 } } @@ -65,7 +66,8 @@ function getInsertionInitialState(list: number[]) : StepState { flag: false, secondFlag: false, interval: 1, - temp: 0 + temp: 0, + gap: 0 } } @@ -83,7 +85,8 @@ function getSelectionInitialState(items: number[]): StepState { flag: false, secondFlag: false, interval: 1, - temp: 0 + temp: 0, + gap: 0 } } @@ -93,6 +96,25 @@ function useSelectionSort(list: number[], time: number, play: boolean) { return useSort(list, time, play, selectionSortStep, initialState, highlight) } +function getCombInitialState(items: number[]) : StepState { + return { + list: items, + index: 0, + keyIndex: 0, + flag: false, + secondFlag: false, + interval: 0, + temp: 0, + gap: Math.floor( items.length*10/13 ) + } +} + +function useCombSort(list: number[], time: number, play: boolean) { + let initialState = getCombInitialState(list); + const highlight = ({ index } : StepState) => [index]; + return useSort(list, time, play, combSortStep, initialState, highlight); +} + function getShellInitialState(items: number[]): StepState { return { list: items, @@ -101,7 +123,8 @@ function getShellInitialState(items: number[]): StepState { flag: false, secondFlag: false, interval: items.length/2, - temp: 0 + temp: 0, + gap: 0 } } @@ -111,12 +134,6 @@ function useShellSort(list: number[], time: number, play: boolean) { return useSort(list, time, play, shellSortStep, initialState, highlight) } -function useCountingSort(list: number[], time: number, play: boolean) { - let initialState = getCountingInitialState(list); - const highlight = ({ index, keyIndex }: StepState) => [index, keyIndex]; - return useSort(list, time, play, countingSortStep, initialState, highlight) -} - function getCountingInitialState(items: number[]) : StepState { return { list: items, @@ -125,14 +142,22 @@ function getCountingInitialState(items: number[]) : StepState { flag: false, secondFlag: false, interval: 1, - temp: 0 + temp: 0, + gap: 0 } } +function useCountingSort(list: number[], time: number, play: boolean) { + let initialState = getCountingInitialState(list); + const highlight = ({ index, keyIndex }: StepState) => [index, keyIndex]; + return useSort(list, time, play, countingSortStep, initialState, highlight) +} + export { useBubbleSort, useInsertionSort, useSelectionSort, + useCombSort, useShellSort, useCountingSort } diff --git a/src/util/algorithm.ts b/src/util/algorithm.ts index e03f87556c4351a289cc69baf53a19f17c468830..66c36f139f1a5df5c478d79effa9ac597069abc3 100644 --- a/src/util/algorithm.ts +++ b/src/util/algorithm.ts @@ -7,7 +7,8 @@ interface StepState { flag: boolean, secondFlag: boolean, interval: number, - temp: number + temp: number, + gap: number }; function bubbleSortStep( @@ -80,6 +81,28 @@ function selectionSortStep ( } } +function combSortStep( + state: StepState +) : StepState { + const { list, index, keyIndex: lastIndex, gap } = state; + + if(gap === 0){ + return state; + } + + if (index === list.length) { + return compose(recountGap, restartIndexToBegining) (state); + } else if ( list[index] < list[lastIndex] && (index-lastIndex) === gap) { + return restartIndexToKeyIndex( + swapWithKeyIndex(state) + ); + } else if ( index-lastIndex === gap) { + return restartIndexToKeyIndex(state); + } else { + return moveForward(state); + } +} + function shellSortStep ( state : StepState ) : StepState { @@ -116,6 +139,12 @@ function countingSortStep(state: StepState) : StepState { } } +function recountGap(state: StepState) : StepState { + const {gap} = state; + const newGap = Math.floor( gap*10/13 ); + return { ...state, gap: newGap} +} + function checkInterval(state: StepState) : StepState { const { list, @@ -202,6 +231,13 @@ function restartIndexToKeyIndex(state: StepState) : StepState { } } +function restartIndexToBegining(state: StepState) : StepState { + return { ...state, + keyIndex: 0, + index: 0, + } +} + function decreaseLastIndex(state: StepState) : StepState { return { ...state, keyIndex: state.keyIndex-1 } } @@ -234,6 +270,7 @@ export { bubbleSortStep, insertionSortStep, selectionSortStep, + combSortStep, shellSortStep, countingSortStep } diff --git a/src/util/inputList.ts b/src/util/inputList.ts index b857445ccd677174deacdaefd1a6d03fb47aa7f2..b35037770394473dd93e855b25d2deb244315367 100644 --- a/src/util/inputList.ts +++ b/src/util/inputList.ts @@ -1,5 +1,5 @@ function stringToArray(list: String) : number[]{ - return removeNonAlphabet(list).split(" ").map(x => +x).filter(x => !isNaN(x)); + return removeNonAlphabet(list).split(" ").map(x => +x); } function removeNonAlphabet(list: String) : String{