日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

希尔排序Linux下C实现

發布時間:2024/4/15 linux 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 希尔排序Linux下C实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

本次,我們談論下希爾排序,希爾排序也叫遞減增量排序算法。步長也是影響希爾排序的一個重要因素,我們這里主要用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?
  • 4、main源文件:main.c

  • #include?"main.h"??
  • ?
  • int?main(void)?
  • {?
  • ? ? ? ? printf("Input?array?length:\n");?
  • ? ? ? ??int?length;?
  • ? ? ? ? scanf("%d",?&length);?
  • ? ? ? ??if(length<0)?
  • ? ? ? ? {?
  • ? ? ? ? ? ? ? ? printf("Array?length?must?be?larger?0\n");?
  • ? ? ? ? ? ? ? ??return?1;?
  • ? ? ? ? }?
  • ? ? ? ??int?arr[length];?
  • ? ? ? ? initRandomArr(arr,?length);?
  • ? ? ? ? printf("Get?random?array?:\n");?
  • ? ? ? ? showArr(arr,?length);?
  • ? ? ? ? shellSort(arr,?length);?
  • ? ? ? ? printf("shell?sort?result:\n");?
  • ? ? ? ? showArr(arr,?length);?
  • ? ? ? ??return?0;?
  • }?
  • void?showArr(const?int?*pArr,?const?int?length)?
  • {?
  • ? ? ? ??int?i;?
  • ? ? ? ??for(i=0;?i<length;?i++)?
  • ? ? ? ? {?
  • ? ? ? ? ? ? ? ? printf("%d?",?*(pArr+i));?
  • ? ? ? ? }?
  • ? ? ? ? printf("\n");?
  • }?
  • void?initRandomArr(int?*pArr,?const?int?length)?
  • {?
  • ? ? ? ??int?i;?
  • ? ? ? ? srand(time(NULL));?
  • ? ? ? ??for(i=0;?i<?length;?i++)?
  • ? ? ? ? {?
  • ? ? ? ? ? ? ? ? *(pArr+i)=rand()%1000;?
  • ? ? ? ? }?
  • }?
  • 5、編譯:

  • [root@localhost?shellSort]$?gcc?-c?shellSort.c?
  • [root@localhost?shellSort]$?gcc?-c?main.c?
  • [root@localhost?shellSort]$?gcc?-o?main?main.o?shellSort.o
  • 執行可執行文件main如下:? ?

  • [root@localhost?shellSort]$?./main? ?
  • Input?array?length:?
  • 12?
  • Get?random?array?:?
  • 518?713?265?31?350?931?592?872?489?927?640?106? ?
  • shell?sort?result:?
  • 31?106?265?350?489?518?592?640?713?872?927?931? ?
  • 希爾排序是個不穩定的排序,性能優于直接插入排序。

    轉載于:https://my.oschina.net/fenglinwansu/blog/81637

    總結

    以上是生活随笔為你收集整理的希尔排序Linux下C实现的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。