例子-台湾敏捷方法AgileMethod.ppt

例子-台湾敏捷方法AgileMethod

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Shell Sort Design Sketch * Shell sort 依increment將數列分成數個子數列,分別做insertion sort 從子數列第二個元素起,向右隔increment個元素,逐一insert至左邊已sort之子數列 0 1 0 1 0 1 9 6 3 4 1 2 0 1 2 … n-2 n-1 0 1 0 1 0 1 1 6 3 4 9 2 0 1 2 … n-2 n-1 0 1 0 1 0 1 1 2 3 4 9 6 0 1 2 … n-2 n-1 0 0 0 0 0 0 1 2 3 4 6 9 0 1 2 … n-2 n-1 餘數 數值 索引 increment = 1 increment = 2 定increment(此為2) 將數列分成increment個子數列 將第0子數列(灰色) 做 insertion sort (得 1, 3, 9) 直到做完所有子數列 遞減 increment 為 increment/2, 並依上述處理,直到increment為1 到此即完成sort 數列 : : Shell Sort Design Sketch (cont.) (下面是開發者看到 design sketch 時的 內心思考,不寫出文件的) 1. increment 2 時,array分為兩個 子陣列: k=1: {9 3 1} k=2: {6 4 2} 分別做 insertion sort 2. 遞迴做 shell sort,每次increment 減半, 直到1 (即退化為 insertion sort) * Shell Sort pseudo code public int[] shellSort (int increment) 1.for 子陣列 k from 1 upto increment, 分別做insertion sort for i from 1+k upto N-1 by increment, 逐一插至左邊 array使變大之 array保持已sort for j from i downto 1+k by increment if array[j] 左邊元素 then swap二元素 else 已插至定位,離開迴圈 end if end for end for end for 2.遞迴做shell sort,每次increment 減半,直到1 3. return array * Shell Sort source code public int[] shellSort (int increment) { /*1.for 子陣列 k from 1 upto increment, 分別做insertion sort*/ for (int k=1; k=increment; k++){ /* for i from 1+k upto N-1 by increment, 逐一插至左邊array 使變大之 array保持已sort*/ for (int i=1+k; i=array.length-1; i=i+increment;){ /* for j from i downto 1+k by increment */ for (int j=i; j=k+1; j=j-increment;){ /*if array[j]左邊元素 then swap二元素*/ if (array[j]array[j-increment]) swap(j,j-

文档评论(0)

1亿VIP精品文档

相关文档