希尔排序

希尔排序(shell)是对插入排序的一个改装,它每次排序把序列的元素按照某个增量分成几个子序列,对这几个子序列进行插入排序,然后不断的缩小增量扩大每个子序列的元素数量,直到增量为一的时候子序列就和原先的待排列序列一样了,此时只需要做少量的比较和移动就可以完成对序列的排序了.

#include <stdio.h>;

void Shell_Sort(int a[], int n) 
{ 
    int h,i,j,temp; 
    for (h=n/2; h>0; h=h/2) 
    { 
        for (i=h; i<n; i++) 
        { 
            temp = a[i]; 
            for (j=i-h; j>=0 && temp < a[j]; j-=h) 
            { 
                a[j+h] = a[j]; 
            } 
            a[j+h] = temp; 
        } 
    } 
     
} 


int main(void)
{
    int arr[]={1,5,2,4,3,8,6,7,9};
	int count=sizeof(arr)/sizeof(int);
    
    Shell_Sort(arr,count);

	int k;
	for(k=0;k<count;k++)
	{
		printf("%d",arr[k]);
	}
    return 0;
}

 

PHP版本

<?php
      $arr=array(1,5,2,4,3,8,6,7,9);
      print "排序前 ";
      print_r($arr);
      echo "<br>";
      $arr=shell_sort($arr);
      print "排序后 ";
      print_r($arr);
      
   
      function shell_sort($array)
      {
          $count = count($array);
          for ($h=intval($count/2); $h>0; $h=intval($h/2)) 
          { 
            for ($i=$h; $i<$count; $i++) 
            { 
              $temp = $array[$i]; 
              for ($j=$i-$h; $j>=0 && $temp < $array[$j]; $j-=$h) 
              { 
                $array[$j+$h] = $array[$j]; 
              } 
              $array[$j+$h] = $temp; 
            } 
          } 
          return $array;
      }
      
      ?>


 


 希尔排序演示不错

http://www.tyut.edu.cn/kecheng1/site01/suanfayanshi/shell_sort.asp

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值