(1)IT重點:20200623ITLast.odt 下載 (16 KB) | created 23 六月, 2020
(1-1)在notepad輸入如下JS程式碼並存檔為d:sort.htm
var a = [9, 5, 8, 7, 6 , 4 ]; function bsort(){ var size=a.length; document.write("待排陣列計有"+size+"個元素 "); for (i=0;i<= size-2;i++){ for (j=0;j<=size-2-i;j++){ if (a[j]>a[j+1]) { document.write("交換前 at j=" + j + "(" + a[j] + "," + a[j+1] + ")"); t=a[j]; a[j]=a[j+1]; a[j+1]=t; document.write("-->交換後 a[]="+a +" "); } else document.write("無交換 at j=" + j + "(" + a[j] + "," + a[j+1] + ") "); } document.write(" 第"+ i +"回合結果: a[]="+a +" "); } }
document.write("氣泡排序法排序前 a[]="+a+" "); bsort();
(1-2)用firefox開啟d:sort.htm,瀏覽結果如下:
氣泡排序法排序前 a[]=9,5,8,7,6,4
待排陣列計有6個元素 交換前 at j=0(9,5)-->交換後 a[]=5,9,8,7,6,4 交換前 at j=1(9,8)-->交換後 a[]=5,8,9,7,6,4 交換前 at j=2(9,7)-->交換後 a[]=5,8,7,9,6,4 交換前 at j=3(9,6)-->交換後 a[]=5,8,7,6,9,4 交換前 at j=4(9,4)-->交換後 a[]=5,8,7,6,4,9
第0回合結果: a[]=5,8,7,6,4,9
無交換 at j=0(5,8) 交換前 at j=1(8,7)-->交換後 a[]=5,7,8,6,4,9 交換前 at j=2(8,6)-->交換後 a[]=5,7,6,8,4,9 交換前 at j=3(8,4)-->交換後 a[]=5,7,6,4,8,9
第1回合結果: a[]=5,7,6,4,8,9
無交換 at j=0(5,7) 交換前 at j=1(7,6)-->交換後 a[]=5,6,7,4,8,9 交換前 at j=2(7,4)-->交換後 a[]=5,6,4,7,8,9
第2回合結果: a[]=5,6,4,7,8,9
無交換 at j=0(5,6) 交換前 at j=1(6,4)-->交換後 a[]=5,4,6,7,8,9
第3回合結果: a[]=5,4,6,7,8,9
交換前 at j=0(5,4)-->交換後 a[]=4,5,6,7,8,9
第4回合結果: a[]=4,5,6,7,8,9
(2-1)在notepad輸入如下JS程式碼並存檔在桌面為ssort.htm
var b = [9, 5, 8, 1, 3 ];
function ssort(){ var size=b.length; document.write("待排陣列計有"+size+"個元素 ");
for (i=0;i var mini=i; for (j=i+1;j if (b[j] } document.write(" i=" + i+ "交換前b[" + i + "]=" + b[i]+ ", b[" + mini+"]" + "=" + b[mini] + " mini=" +mini);
t=b[mini]; b[mini]=b[i]; b[i]=t; document.write(" 第"+i +"回合結果:b[]="+b +" "); } }
document.write("選擇排序法排序前 b[]="+b +" "); ssort(); document.write(" 排序後 b[]="+b);
(2-2)用firefox開啟d:ssort.htm,瀏覽結果如下:
選擇排序法排序前 b[]=9,5,8,1,3
待排陣列計有5個元素
i=0交換前b[0]=9, b[3]=1 mini=3 第0回合結果:b[]=1,5,8,9,3
i=1交換前b[1]=5, b[4]=3 mini=4 第1回合結果:b[]=1,3,8,9,5
i=2交換前b[2]=8, b[4]=5 mini=4 第2回合結果:b[]=1,3,5,9,8
i=3交換前b[3]=9, b[4]=8 mini=4 第3回合結果:b[]=1,3,5,8,9
排序後 b[]=1,3,5,8,9
REF1: 20191116用dev C++來編寫氣泡、選擇、插入排序法
REF2: 20191111用python設計氣泡排序法(採升序排列,ascending)
REF3:20191230用JavaScript設計插入與選擇排序法 |