常見排序算法的實現(二)-shell排序
shell排序是對插入排序的一個改裝,它每次排序把序列的元素按照某個增量分成幾個子序列,對這幾個子序列進行插入排序,然后不斷的縮小增量擴大每個子序列的元素數量,直到增量為一的時候子序列就和原先的待排列序列一樣了,此時只需要做少量的比較和移動就可以完成對序列的排序了.
//?shell排序
void?ShellSort(int?array[],?int?length)


{
????int?temp;

????//?增量從數組長度的一半開始,每次減小一倍
????for?(int?increment?=?length?/?2;?increment?>?0;?increment?/=?2)
????????for?(int?i?=?increment;?i?<?length;?++i)

????????
{
????????????temp?=?array[i];
????????????//?對一組增量為increment的元素進行插入排序
????????????for?(int?j?=?i;?j?>=?increment;?j?-=?increment)

????????????
{
????????????????//?把i之前大于array[i]的數據向后移動
????????????????if?(temp?<?array[j?-?increment])

????????????????
{
????????????????????array[j]?=?array[j?-?increment];
????????????????}
????????????????else

????????????????
{
????????????????????break;
????????????????}
????????????}
????????????//?在合適位置安放當前元素
????????????array[j]?=?temp;
????????}
}
動畫演示:
http://202.113.89.254/DataStructure/DS/web/flashhtml/shell.htm





































動畫演示:
http://202.113.89.254/DataStructure/DS/web/flashhtml/shell.htm
posted on 2006-07-03 16:07 那誰 閱讀(1070) 評論(0) 編輯 收藏 引用 所屬分類: 算法與數據結構