introduction to algorithms sorting lesson_3 shell sorting
来源:互联网 发布:考研什么时候报班 知乎 编辑:程序博客网 时间:2024/06/07 02:46
作为插入排序的改进算法 希尔排序 shell sort
基本思想:对于排序数量较少的数据,运用直接插入排序可以很快,效率很高
基于这一点,希尔排序的想法是:将原数据分成若干数据量较少的组,分别进行排序,则这些数据基本有序,最后对全体进行一次插入排序即可。
假设有10数据,如:49 29 30 12 32 59 99 04 95 09
分组的步骤是:第一趟希尔插入排序,选取间隔为5的数据,则分组为<49 59> <29,99> < 30,04> ,<12 , 95 > <32 ,09>
第二趟希尔插入排序,间隔减少为3
第三趟希尔插入排序,间隔为1 则完成排序
代码:
/*********shell sorting***********/
#define getArraySize(arrayName) (sizeof(arrayName)/sizeof(arrayName[0]))void shellinsert(int data[],int len,int dk){int key;int i,j;for(i=dk;i<len;i++) //一次希尔插入排序{if (data[i]<data[i-dk]){key=data[i];for (j=i-dk;j>=0&&data[j]>key;j-= dk){data[j+dk]=data[j];}data[j+ dk]=key;}}}void shell(int data[],int lda,int dk[],int t)//增量多次希尔插入排序{ for(int k=0;k<t;k++) shellinsert(data,lda,dk[k]);}测试:
int _tmain(int argc, _TCHAR* argv[]){int data_test[10]={1,0,2,9,3,1,5,8,9,10};int dk[3]={5,3,1};shell(data_test,getArraySize(data_test),dk,getArraySize(dk));//getArraySize 获取数组长度prtarray(data_test,getArraySize(data_test));//打印数组system("pause");return 0;}希尔排序分析:
由于希尔排序与数据本身的排序有复杂的关系,增量序列也难以分析,目前还没有解决希尔排序的严格证明。
数据结构教材:通过实验的方法,希尔排序的时间复杂度 O(n^1.3)
0 0
- introduction to algorithms sorting lesson_3 shell sorting
- introduction to algorithms sorting lesson_4 quick sorting
- introduction to algorithms sorting lesson 1 insertion
- introduction to algorithms sorting lesson 2 merge sorting
- introduction to algorithms sorting lesson_5 Heap sorting(堆排序)
- introduction to algorithms sorting lesson_6 Counting sort (计数排序)
- Sorting Algorithms
- sorting algorithms
- Sorting Algorithms
- Sorting Algorithms ------[Insertion Sort]
- Sorting Algorithms ------ [MergeSort]
- Sorting Algorithms ------ [QuickSort]
- Sorting algorithms revisited
- Sorting Algorithms Review
- Sorting Algorithms Part1
- Sorting
- Sorting:
- Sorting
- CentOS RPM安装MySQL-5.6
- UVA LIVE-3263 - That Nice Euler Circuit
- json介绍
- 面向 iOS 开发的十款 XCode 插件
- spring整合Jersey 无法注入service的问题
- introduction to algorithms sorting lesson_3 shell sorting
- 百度图片api
- smail语法 详解
- 在线删除全部数据文件的恢复案例
- Python,异常 exception
- 迁徙至:xingxiudong.com
- ANDROID Porting系列四、建立公开密钥和签名生成
- openfire 修改服务器别名及出现问题
- FIFO、LRU、OPT这三种置换算法的缺页次数