希尔排序Linux下C实现
希尔排序Linux下C实现
本次,我们谈论下希尔排序,希尔排序也叫递减增量排序算法。步长也是影响希尔排序的一个重要因素,我们这里主要用Marcin Ciura设计的步长。关键代码如下:
1、希尔排序头文件:shellSort.h
- #ifndef SHELLSORT_H
- #define SHELLSORT_H
- extern void shellSort(int * pArr, const int length);
- #endif
2、希尔排序源文件:shellSort.c
- #include "shellSort.h"
- void shellSort(int * pArr, const int length)
- {
- const int pInc[9]={1,4,10,23,57,132,301,701,1750};
- int len=sizeof(pInc)/sizeof(int);
- int i,k,j,tmp;
- int inc;
- k=0;
- while(*(pInc+k)<length && k<=len)
- {
- k++;
- }
- while(--k>=0)
- {
- inc=*(pInc+k);
- for(i=inc; i< length; i++)
- {
- tmp=*(pArr+i);
- j=i;
- while(j>=inc && *(pArr+j-inc)>tmp)
- {
- *(pArr+j)=*(pArr+j-inc);
- j=j-inc;
- }
- *(pArr+j)=tmp;
- }
- }
- }
3、main头文件:main.h
- #ifndef MAIN_H
- #define MAIN_H
- #include<stdio.h>
- #include "shellSort.h"
- int main(void);
- void showArr(const int *pArr, const int length);
- void initRandomArr(int *pArr, const int length);
- #endif
|
评论暂时关闭