c 希尔排序算法

by pxz


发布于: 2013-04-15 所属分类: 后端 c 算法 标签:  189


<pre><code>#include <stdio.h> //将数组arr从 d到arrlen 进行插入排序 void ShellPass(int arr[], int arrlen, int d) { int i,j; int t; for (i=d; i<arrlen; i++) { if (arr[i]<arr[i-d]) { t = arr[i]; j = i-d; do{ arr[j+d] = arr[j]; j = j-d; }while(j>=0 &amp;&amp; t<arr[j]); arr[j+d] = t; } } } //希尔排序 void ShellShort(int arr[], int arrlen) { int increment = arrlen; do{ increment = increment/3 + 1; printf("increment = %d\n", increment); ShellPass(arr, arrlen, increment); }while(increment>1); } int printarr(int arr[], int arrlen) { int i; for (i=0; i<arrlen; i++) { printf("%3d", arr[i]); } printf("\n"); return 1; } int main() { int arr[] = {51,66,98,5,0,22,36,99,65}; int arrlen = sizeof(arr)/sizeof(int); printarr(arr, arrlen); ShellShort(arr, arrlen); printarr(arr, arrlen); } </code></pre>

发表评论

暂无评论

搜索
用户登陆