Skip to content

Commit e4c8539

Browse files
committed
Changes to be committed:
modified: index.html 1. add new algorithm , odd-even sorting algorithm 2. add odd-even-sort.js file in bottom of index.html new file: js/odd_even_sort.js 1. add new odd-even sorting algorithm content modified: js/test.js 1. add try catch syntax in algoChosen() function 2. add OddEvenSort value 3. add , if algoChosen() error, then window alert pops up, notices user to press F5 to refresh page.
1 parent 9dc1250 commit e4c8539

File tree

3 files changed

+128
-29
lines changed

3 files changed

+128
-29
lines changed

index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<option class="options" value="ExchangeSort">Exchange Sort</option>
2727
<option class="options" value="InsertionSort">Insertion Sort</option>
2828
<option class="options" value="ShakerSort">Shaker Sort</option>
29+
<option class="options" value="OddEvenSort">Odd-Even Sort</option>
2930
</select>
3031
</div>
3132
</div>
@@ -74,14 +75,15 @@
7475

7576
</main>
7677

77-
78+
7879

7980
<script type="text/javascript" src="./js/main.js"></script>
8081
<script type="text/javascript" src="./js/bubble_sort.js"></script>
8182
<script type="text/javascript" src="./js/selection_sort.js"></script>
8283
<script type="text/javascript" src="./js/exchange_sort.js"></script>
8384
<script type="text/javascript" src="./js/insertion_sort.js"></script>
8485
<script type="text/javascript" src="./js/shaker_sort.js"></script>
86+
<script type="text/javascript" src="./js/odd_even_sort.js"></script>
8587
<script type="text/javascript" src="./js/test.js"></script>
8688
</body>
8789
</html>

js/odd_even_sort.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'strict'
2+
3+
class OddEvenSort extends SortingBase{
4+
constructor(){
5+
super();
6+
}
7+
8+
splitTime = 1000;
9+
10+
setSplitTime(mspf){
11+
if(mspf <3){
12+
this.splitTime = 1;
13+
return;
14+
}
15+
this.splitTime = mspf /3;
16+
}
17+
18+
static async run(one, sleep, endingPose){
19+
let temp;
20+
let isAllSorted;
21+
one.setSplitTime(one.milliSecPerFrame)
22+
23+
isAllSorted = 0;
24+
25+
while(1){
26+
for(let i=0 ; i<one.entryNum ; i=i+2){
27+
28+
if( (i+1) < one.entryNum){
29+
30+
one.compareColoring(one.blockBox[i] , one.blockBox[i+1]);
31+
one.fillStageWithBlocks();
32+
await sleep(one.splitTime);
33+
34+
if(one.blockBox[i].value > one.blockBox[i+1].value){
35+
temp = one.blockBox[i];
36+
one.blockBox[i] = one.blockBox[i+1];
37+
one.blockBox[i+1] = temp;
38+
isAllSorted++;
39+
}
40+
41+
one.checkCompareColoring(one.blockBox[i] , one.blockBox[i+1]);
42+
one.fillStageWithBlocks();
43+
await sleep(one.splitTime);
44+
45+
one.doneCompareColoring(one.blockBox[i] , one.blockBox[i+1]);
46+
one.fillStageWithBlocks();
47+
await sleep(one.splitTime);
48+
49+
}
50+
};
51+
if(!isAllSorted){break;}
52+
isAllSorted = 0;
53+
for(let i=1 ; i<one.entryNum ; i=i+2){
54+
if( (i+1) < one.entryNum){
55+
56+
one.compareColoring(one.blockBox[i] , one.blockBox[i+1]);
57+
one.fillStageWithBlocks();
58+
await sleep(one.splitTime);
59+
60+
if(one.blockBox[i].value > one.blockBox[i+1].value){
61+
temp = one.blockBox[i];
62+
one.blockBox[i] = one.blockBox[i+1];
63+
one.blockBox[i+1] = temp;
64+
isAllSorted++;
65+
}
66+
67+
one.checkCompareColoring(one.blockBox[i] , one.blockBox[i+1]);
68+
one.fillStageWithBlocks();
69+
await sleep(one.splitTime);
70+
71+
one.doneCompareColoring(one.blockBox[i] , one.blockBox[i+1]);
72+
one.fillStageWithBlocks();
73+
await sleep(one.splitTime);
74+
}
75+
};
76+
if(!isAllSorted){break;}
77+
isAllSorted = 0;
78+
}
79+
80+
endingPose(one, sleep)
81+
82+
console.log('done')
83+
}
84+
85+
}

js/test.js

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,36 +30,48 @@ window.onload = function(){
3030

3131
function algoChosen(){
3232

33-
algo = algoSelector.value;
34-
if(algo === null || algo === 'none'){
35-
clearInputDashBoard();
36-
allDisable([algoSelector]);
37-
}
38-
else{
39-
if(algo === 'BubbleSort'){
40-
one = new BubbleSort();
41-
classOne = BubbleSort;
42-
}
43-
else if(algo === 'SelectionSort'){
44-
one = new SelectionSort();
45-
classOne = SelectionSort;
46-
}
47-
else if(algo === 'ExchangeSort'){
48-
one = new ExchangeSort();
49-
classOne = ExchangeSort;
50-
}
51-
else if(algo === 'InsertionSort'){
52-
one = new InsertionSort();
53-
classOne = InsertionSort;
33+
try{
34+
35+
algo = algoSelector.value;
36+
if(algo === null || algo === 'none'){
37+
clearInputDashBoard();
38+
allDisable([algoSelector]);
5439
}
55-
else if(algo === 'ShakerSort'){
56-
one = new ShakerSort();
57-
classOne = ShakerSort;
40+
else{
41+
if(algo === 'BubbleSort'){
42+
one = new BubbleSort();
43+
classOne = BubbleSort;
44+
}
45+
else if(algo === 'SelectionSort'){
46+
one = new SelectionSort();
47+
classOne = SelectionSort;
48+
}
49+
else if(algo === 'ExchangeSort'){
50+
one = new ExchangeSort();
51+
classOne = ExchangeSort;
52+
}
53+
else if(algo === 'InsertionSort'){
54+
one = new InsertionSort();
55+
classOne = InsertionSort;
56+
}
57+
else if(algo === 'ShakerSort'){
58+
one = new ShakerSort();
59+
classOne = ShakerSort;
60+
}
61+
else if(algo === 'OddEvenSort'){
62+
one = new OddEvenSort();
63+
classOne = OddEvenSort;
64+
}
65+
else{
66+
window.alert('演算法系統錯誤,\n請按 F5 重新整理系統,再次使用。');
67+
// refresh();
68+
}
69+
70+
allEnable([goBtn])
5871
}
59-
60-
allEnable([goBtn])
6172
}
62-
73+
catch(e){
74+
}
6375
}
6476

6577
function clearInputDashBoard(){
@@ -121,7 +133,7 @@ function inputInvalid(entryNum , mspfNum){
121133
}
122134
return false;
123135
}
124-
136+
125137
function prepareForRun(){
126138
one.entryNum = isCountInputValid( parseInt(countInput.value, 10) );
127139
one.milliSecPerFrame = isMspfInputValid( parseInt(mspfInput.value, 10));

0 commit comments

Comments
 (0)