C - Shell Sort (one of the simplest)

青旅半醒 2022-11-01 10:49 113阅读 0赞

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net

  1. /*
  2. * Shell Sort:
  3. *
  4. * The basic idea of this sorting algorithm is that in early stages, far-apart
  5. * elements are compared, rather than adjacent ones as in simpler interchange sorts.
  6. * This tends to eliminate large amounts of disorder quickly, so later stages have
  7. * less work to do. The interval between compared elements is gradually decreased to
  8. * one, at which point the sort effectively becomes an adjacent interchange method.
  9. *
  10. * There are three nested loops. The outermost controls the gap between compared
  11. * elements, shrinking it from n/2 by a factor of two each pass until it becomes zero.
  12. * The middle loop steps along the elements. The innermost loop compares each pair of
  13. * elements that is separated by gap and reverses any that are out of order. Since gap
  14. * is eventually reduced to one, all elements are eventually ordered correctly.
  15. * ShellSort.c - by FreeMan
  16. */
  17. void ShellSort(int v[], int n)
  18. {
  19. int gap, i, j, temp;
  20. for (gap = n / 2; gap > 0; gap /= 2)
  21. {
  22. for (i = gap; i < n; i++)
  23. {
  24. for (j = i - gap; j >= 0 && v[j] > v[j + gap]; j -= gap) {
  25. temp = v[j];
  26. v[j] = v[j + gap];
  27. v[j + gap] = temp;
  28. }
  29. }
  30. }
  31. }

发表评论

表情:
评论列表 (有 0 条评论,113人围观)

还没有评论,来说两句吧...

相关阅读

    相关 shell_sort

    尔排序是直接插入排序的改进版。首先设置步长len,然后分组,下标之差为步长整数倍的分为一组。然后去len/2作为步长,直至len=1,此时就是直接 插入排序了。 代码...

    相关 Shell Sort

    在希尔排序建议的增量序列情况下(h=N/2  Hk):最好时间复杂度和平均时间复杂度都是![这里写图片描述][20160427091828581],最坏时间复杂度为![这