Fakultas Ilmu Komputer UI

Skip to content
Snippets Groups Projects
Commit c5122db2 authored by Tsaqif's avatar Tsaqif
Browse files

delete unused files

parent e2a43cb4
No related branches found
No related tags found
1 merge request!37Brick sort
import { useCountingSort } from '../../hooks/counting';
import './Preview.css';
interface props { list: number[] };
function CountingPreview({ list }: props) {
const { items, highlight } = useCountingSort(list, 50);
return (
<div className="preview-box">
{items.map((height, index) =>
<div
style={{height: height*10}}
className={"item "+(index === highlight ? "highlight" : "")}
/>)}
</div>
)
};
export default CountingPreview;
\ No newline at end of file
import * as React from 'react';
interface StepState {
list: number[],
index: number,
lastIndex: number,
counter: number,
numOfSwaps: number,
lastSwapIndex: number
};
function countingSortStep(state: StepState) : StepState {
const {list,index,lastIndex,counter,numOfSwaps,lastSwapIndex} = state;
if (numOfSwaps === list.length) {
return state;
}
if (index === lastIndex) {
return nextCount(state);
} else if (list[index] === counter) {
return moveForward(swapToLastIndex(state));
} else {
return moveForward(state);
}
}
function swapToLastIndex(state: StepState) : StepState {
const { list, index, lastIndex, lastSwapIndex} = state;
const newList = list.slice(0, lastSwapIndex)
.concat(list[index])
.concat(list.slice(lastSwapIndex, index))
.concat(list.slice(index + 1, lastIndex));
return { ...state,
list: newList,
numOfSwaps: state.numOfSwaps + 1,
lastSwapIndex: state.lastSwapIndex + 1,
}
}
function moveForward(state: StepState) : StepState {
return { ...state, index: state.index+1 }
}
function nextCount(state: StepState) : StepState {
return { ...state, counter: state.counter + 1, index: state.lastSwapIndex}
}
function useCountingSort(list: number[], time: number) {
const [items, setItems] = React.useState(list);
const [highlight, setHighlight] = React.useState(0);
const [stepState, setStepState] = React.useState<StepState>({
list: items,
index: 0,
lastIndex: items.length,
counter: 0,
numOfSwaps: 0,
lastSwapIndex: 0,
});
React.useEffect(() => {
const timeout = setTimeout(() => {
setStepState(
countingSortStep(stepState)
);
setItems(stepState.list);
setHighlight(stepState.index);
}, time);
return () => { clearTimeout(timeout) }
}, [stepState, time])
return {
items,
highlight
}
}
export { useCountingSort };
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment