1£©Ê²Ã´ÊÇÏ£¶ûÅÅÐò
Ï£¶ûÅÅÐò(Shell Sort)ÊDzåÈëÅÅÐòµÄÒ»ÖÖ¡£Ò²³ÆËõСÔöÁ¿ÅÅÐò£¬ÊÇÖ±½Ó²åÈëÅÅÐòËã·¨µÄÒ»ÖÖ¸ü¸ßЧµÄ¸Ä½ø°æ±¾¡£Ï£¶ûÅÅÐòÊÇ·ÇÎȶ¨ÅÅÐòËã·¨¡£¸Ã·½·¨ÒòDL£®ShellÓÚ1959ÄêÌá³ö¶øµÃÃû¡£
Ï£¶ûÅÅÐòÊǰѼǼ°´Ï±êµÄÒ»¶¨ÔöÁ¿·Ö×飬¶Ôÿ×éʹÓÃÖ±½Ó²åÈëÅÅÐòËã·¨ÅÅÐò£»Ëæ×ÅÔöÁ¿Öð½¥¼õÉÙ£¬Ã¿×é°üº¬µÄ¹Ø¼ü´ÊÔ½À´Ô½¶à£¬µ±ÔöÁ¿¼õÖÁ1ʱ£¬Õû¸öÎļþÇ¡±»·Ö³ÉÒ»×飬Ëã·¨±ãÖÕÖ¹
2£©»ù±¾Ë¼Ïë
ÏÈÈ¡Ò»¸öСÓÚnµÄÕûÊýd1×÷ΪµÚÒ»¸öÔöÁ¿£¬°ÑÎļþµÄÈ«²¿¼Ç¼·Ö×é¡£ËùÓоàÀëΪd1µÄ±¶ÊýµÄ¼Ç¼·ÅÔÚͬһ¸ö×éÖС£ÏÈÔÚ¸÷×éÄÚ½øÐÐÖ±½Ó²åÈëÅÅÐò£»È»ºó£¬È¡µÚ¶þ¸öÔöÁ¿d2<d1Öظ´ÉÏÊöµÄ·Ö×éºÍÅÅÐò£¬Ö±ÖÁËùÈ¡µÄÔöÁ¿ =1( < ¡<d2<d1)£¬¼´ËùÓмǼ·ÅÔÚͬһ×éÖнøÐÐÖ±½Ó²åÈëÅÅÐòΪֹ¡£
¸Ã·½·¨ÊµÖÊÉÏÊÇÒ»ÖÖ·Ö×é²åÈë·½·¨
±È½ÏÏà¸ô½ÏÔ¶¾àÀ루³ÆΪÔöÁ¿£©µÄÊý£¬Ê¹µÃÊýÒƶ¯Ê±ÄÜ¿ç¹ý¶à¸öÔªËØ£¬Ôò½øÐÐÒ»´Î±È½Ï¾Í¿ÉÄÜÏû³ý¶à¸öÔªËؽ»»»¡£Ëã·¨ÏȽ«ÒªÅÅÐòµÄÒ»×éÊý°´Ä³¸öÔöÁ¿d·Ö³ÉÈô¸É×飬ÿ×éÖмǼµÄϱêÏà²îd.¶Ôÿ×éÖÐÈ«²¿ÔªËؽøÐÐÅÅÐò£¬È»ºóÔÙÓÃÒ»¸ö½ÏСµÄÔöÁ¿¶ÔËü½øÐУ¬ÔÚÿ×éÖÐÔÙ½øÐÐÅÅÐò¡£µ±ÔöÁ¿¼õµ½1ʱ£¬Õû¸öÒªÅÅÐòµÄÊý±»·Ö³ÉÒ»×飬ÅÅÐòÍê³É¡£
Ò»°ãµÄ³õ´ÎÈ¡ÐòÁеÄÒ»°ëΪÔöÁ¿£¬ÒÔºóÿ´Î¼õ°ë£¬Ö±µ½ÔöÁ¿Îª1¡£
Ï£¶ûÅÅÐòÊÇ°´ÕÕ²»Í¬²½³¤¶ÔÔªËؽøÐвåÈëÅÅÐò£¬µ±¸Õ¿ªÊ¼ÔªËغÜÎÞÐòµÄʱºò£¬²½³¤×î´ó£¬ËùÒÔ²åÈëÅÅÐòµÄÔªËظöÊýºÜÉÙ£¬ËٶȺܿ죻µ±ÔªËØ»ù±¾ÓÐÐòÁË£¬²½³¤ºÜС£¬²åÈëÅÅÐò¶ÔÓÚÓÐÐòµÄÐòÁÐЧÂʺܸߡ£
#include<stdio.h>
void shellsort(int *a, int n) { int i, j, k, t; k = n / 2; while(k > 0) { for(i = k; i < n; i++){ t = a[i]; j = i - k; while(j >= 0 && t < a[j]) { a[j + k] = a[j]; j = j - k; } a[j + k] = t; } k /= 2; }
} int main() { int a[] = {8,10,3,5,7,4,6,1,9,2}; int N; int k; N = sizeof(a) / sizeof(a[0]); shellsort(a, N); for(k = 0;k < N; k++) printf("a[%d] = %d\n",k,a[k]); return 0; } |