希爾排序
static void shellSort1(Integer[] sortList) {
int i, j, step;
int len = sortList.length;
// 步長除以2
for (step = len / 2; step > 0; step /= 2)
/**
* 分別對每個分組進行直接插入排序
*/
for (i = 0; i < step; i++)
{
for (j = i + step; j < len; j += step)
if (sortList[j] < sortList[j - step]) {
int temp = sortList[j];
int k = j - step;
while (k >= 0 && sortList[k] > temp) {
sortList[k + step] = sortList[k];
k -= step;
}
sortList[k + step] = temp;
}
}
}
int i, j, step;
int len = sortList.length;
// 步長除以2
for (step = len / 2; step > 0; step /= 2)
/**
* 分別對每個分組進行直接插入排序
*/
for (i = 0; i < step; i++)
{
for (j = i + step; j < len; j += step)
if (sortList[j] < sortList[j - step]) {
int temp = sortList[j];
int k = j - step;
while (k >= 0 && sortList[k] > temp) {
sortList[k + step] = sortList[k];
k -= step;
}
sortList[k + step] = temp;
}
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.