shell排序C实现

来源:互联网 发布:电脑系统优化 编辑:程序博客网 时间:2024/06/01 08:13

shell排序实际上是步长变化的插入排序,中间过程与插入排序没多大区别。

#include <stdio.h>#include <stdlib.h>#define LEN 100int arr[LEN];void shellSort(int *arr,int len){  int step;  int i,j;  for(step=len/2;step>=1;step=step/2)  {      for(i=step;i<len;i++)      {          int temp=arr[i];          j=i-step;          while(j>=0&&arr[j]>temp)          {              arr[j+step]=arr[j];              j-=step;          }          j+=step;          arr[j]=temp;      }  }}int main(){    int i;    for(i=0;i<LEN;i++)        arr[i]=rand();    shellSort(arr,LEN);    for(i=0;i<LEN;i++)        printf("%d\n",arr[i]);    return 0;}


0 0
原创粉丝点击