diff --git a/README.md b/README.md index 9136105..8a36b82 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,8 @@ visualized interactive system of sorting algorithm 排序演算法視覺化交互系統 +[Demonstration](https://adyjs.github.io/sorting-algorithm-visualization/) + 以網頁前端的方式,讓使用者在網頁上操作互動行的系統, 配合著演算法種類、排序速度、排序狀態、等等, 一步一步的了解每種排序演算法的差異處 diff --git a/index.html b/index.html index 3c52a1a..cc76f82 100644 --- a/index.html +++ b/index.html @@ -26,7 +26,7 @@ - + @@ -75,7 +75,7 @@ - + @@ -83,7 +83,7 @@ - + \ No newline at end of file diff --git a/js/odd_even_sort.js b/js/odd_even_sort.js new file mode 100644 index 0000000..8eaf897 --- /dev/null +++ b/js/odd_even_sort.js @@ -0,0 +1,85 @@ +'strict' + +class OddEvenSort extends SortingBase{ + constructor(){ + super(); + } + + splitTime = 1000; + + setSplitTime(mspf){ + if(mspf <3){ + this.splitTime = 1; + return; + } + this.splitTime = mspf /3; + } + + static async run(one, sleep, endingPose){ + let temp; + let isAllSorted; + one.setSplitTime(one.milliSecPerFrame) + + isAllSorted = 0; + + while(1){ + for(let i=0 ; i one.blockBox[i+1].value){ + temp = one.blockBox[i]; + one.blockBox[i] = one.blockBox[i+1]; + one.blockBox[i+1] = temp; + isAllSorted++; + } + + one.checkCompareColoring(one.blockBox[i] , one.blockBox[i+1]); + one.fillStageWithBlocks(); + await sleep(one.splitTime); + + one.doneCompareColoring(one.blockBox[i] , one.blockBox[i+1]); + one.fillStageWithBlocks(); + await sleep(one.splitTime); + + } + }; + if(!isAllSorted){break;} + isAllSorted = 0; + for(let i=1 ; i one.blockBox[i+1].value){ + temp = one.blockBox[i]; + one.blockBox[i] = one.blockBox[i+1]; + one.blockBox[i+1] = temp; + isAllSorted++; + } + + one.checkCompareColoring(one.blockBox[i] , one.blockBox[i+1]); + one.fillStageWithBlocks(); + await sleep(one.splitTime); + + one.doneCompareColoring(one.blockBox[i] , one.blockBox[i+1]); + one.fillStageWithBlocks(); + await sleep(one.splitTime); + } + }; + if(!isAllSorted){break;} + isAllSorted = 0; + } + + endingPose(one, sleep) + + console.log('done') + } + +} \ No newline at end of file diff --git a/js/test.js b/js/test.js index 01d2d3b..4071ce7 100644 --- a/js/test.js +++ b/js/test.js @@ -30,40 +30,49 @@ window.onload = function(){ function algoChosen(){ - algo = algoSelector.value; - if(algo === null || algo === 'none'){ - clearInputDashBoard(); - allDisable([algoSelector]); - } - else{ - if(algo === 'BubbleSort'){ - one = new BubbleSort(); - classOne = BubbleSort; - } - else if(algo === 'SelectionSort'){ - one = new SelectionSort(); - classOne = SelectionSort; - } - else if(algo === 'ExchangeSort'){ - one = new ExchangeSort(); - classOne = ExchangeSort; - } - else if(algo === 'InsertionSort'){ - one = new InsertionSort(); - classOne = InsertionSort; + try{ + + algo = algoSelector.value; + if(algo === null || algo === 'none'){ + clearInputDashBoard(); + allDisable([algoSelector]); } - else if(algo === 'ShakerSort'){ - one = new ShakerSort(); - classOne = ShakerSort; + else{ + if(algo === 'BubbleSort'){ + one = new BubbleSort(); + classOne = BubbleSort; + } + else if(algo === 'SelectionSort'){ + one = new SelectionSort(); + classOne = SelectionSort; + } + else if(algo === 'ExchangeSort'){ + one = new ExchangeSort(); + classOne = ExchangeSort; + } + else if(algo === 'InsertionSort'){ + one = new InsertionSort(); + classOne = InsertionSort; + } + else if(algo === 'ShakerSort'){ + one = new ShakerSort(); + classOne = ShakerSort; + } + else if(algo === 'OddEvenSort'){ + one = new OddEvenSort(); + classOne = OddEvenSort; + } + else{ + window.alert('演算法系統錯誤,\n請按 F5 重新整理系統,再次使用。'); + // refresh(); + } + + allEnable([goBtn]) } - // else if(algo === 'MergeSort'){ - // one = new MergeSort(); - // classOne = MergeSort; - // } - allEnable([goBtn]) } - + catch(e){ + } } function clearInputDashBoard(){ @@ -125,7 +134,7 @@ function inputInvalid(entryNum , mspfNum){ } return false; } - + function prepareForRun(){ one.entryNum = isCountInputValid( parseInt(countInput.value, 10) ); one.milliSecPerFrame = isMspfInputValid( parseInt(mspfInput.value, 10));