先了解下什么都有什么排序算法

https://en.wikipedia.org/wiki/Sorting_algorithm

http://zh.wikipedia.org/zh/%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95

http://student.zjzk.cn/course_ware/data_structure/web/paixu/paixu8.7.1.1.htm

希尔排序 O(n1.25)

二叉排序树排序 (Binary tree sort) — O(n log n)期望时间; O(n2)最坏时间; 需要 O(n) 額外空間

基数排序O(n)

总结:若是数据量特别大的话,希尔排序会比快速排序慢点,但若是中小数据的比较,希尔排序更快速。

而且希尔排序实现简单。

有两种排序我们应该掌握:

一个是希尔排序(小量数据),

一个是二叉排序树排序(又称为二分查找法、快速排序)(大量数据)

希尔排序的wiki中列出的表    http://en.wikipedia.org/wiki/Shellsort

最近的Marcin Ciura's gap sequence的伪代码如下:

Using Marcin Ciura's gap sequence, with an inner insertion sort.

  1. # Sort an array a[0...n-1].
  2. gaps = [701, 301, 132, 57, 23, 10, 4, 1]
  3.  
  4. foreach (gap in gaps)
  5. {
  6. # Do an insertion sort for each gap size.
  7. for (i = gap; i < n; i += 1)
  8. {
  9. temp = a[i]
  10. for (j = i; j >= gap and a[j - gap] > temp; j -= gap)
  11. {
  12. a[j] = a[j - gap]
  13. }
  14. a[j] = temp
  15. }
  16.  
  17. }

http://sun.aei.polsl.pl/~mciura/publikacje/shellsort.pdf 他的文档中列出了从10~1亿 的数据量的时间复杂度,而且有实验数据和图表。

下面是自己写的代码shellsort1_1至1_3是增量为count/2,   shellsort2_1至2_2增量为1

  1. #include "stdafx.h"
  2. #include <string>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm> //just for sort() and binary_search()
  6.  
  7. using namespace std;
  8.  
  9. //method 1 数组方式 ok
  10. void shellsort1_1(int *data, size_t size)
  11. {
  12. for (int gap = size / ; gap > ; gap /= )
  13. for (size_t i = gap; i < size; ++i)
  14. {
  15. int Temp = data[i];
  16. int j = ;
  17. for( j = i -gap; j >= && data[j] > Temp; j -=gap)
  18. {
  19. data[j+gap] = data[j];
  20. }
  21. data[j+gap] = Temp;
  22. }
  23. }
  24.  
  25. //method 2 ok
  26. void shellsort1_2(vector<int> &squeue_)
  27. {
  28. vector<int>::size_type size = squeue_.size();
  29.  
  30. for (int gap = size / ; gap > ; gap /= )
  31. for (size_t i = gap; i < size; ++i)
  32. {
  33. int j = ;
  34. int temp = squeue_[i]; // data[i];
  35.  
  36. for( j = i -gap; j >= && squeue_[j] > temp; j -=gap)
  37. {
  38. squeue_[j+gap] = squeue_[j];
  39. }
  40. squeue_[j+gap] = temp;//squeue_[i];
  41. }
  42. }
  43.  
  44. //method 3 ok
  45. void shellsort1_3(vector<string> &squeue_)
  46. {
  47. vector<string>::size_type size = squeue_.size();
  48.  
  49. for (int gap = size / ; gap > ; gap /= )
  50. for (size_t i = gap; i < size; ++i)
  51. {
  52. int j = ;
  53. string temp = squeue_[i];
  54.  
  55. for( j = i -gap; j >= && squeue_[j] > temp; j -=gap)
  56. {
  57. squeue_[j+gap] = squeue_[j];
  58. }
  59. squeue_[j+gap] = temp;//squeue_[i];
  60. }
  61. }
  62.  
  63. //method 4 ok
  64. void shellsort2(vector<string> &gaps)
  65. {
  66. size_t gap = ;
  67. size_t j = ;
  68. string temp("");
  69. size_t count = gaps.size();
  70.  
  71. for (vector<string>::iterator it = gaps.begin(); it != gaps.end(); ++it, gap +=)//for_each (gap in gaps)
  72. {
  73. // Do an insertion sort for each gap size.
  74. for (size_t i = gap ; i < count; i += )
  75. {
  76. temp = gaps[i];
  77. for (j = i; j >= gap && gaps[j - gap] > temp; j -= gap)
  78. {
  79. gaps[j] = gaps[j - gap];
  80. }
  81. gaps[j] = temp;
  82. }
  83. }
  84. }

  85. //c 库的sort
    int index = 1;
    int list[9] = { 5, 2, 3, 9, 4, 6, 7, 8, 1};
  86.  
  87. int callbackFunc_Compare(const void* a , const void *b)
    {
        printf("index = %d,   a= %d, b = %d \n", index++, *(int*)a, *(int*)b);
  88.  
  89.     for (int i = 0; i < 9; i++)
        {
            printf("%d ",list[i]);
        }
        printf("\n");
  90.  
  91.     return *(int*)a - *(int*)b;
    }
  92.  
  93. int _tmain(int argc, _TCHAR* argv[])
  94. {
  95. //--------int
  96. int i_List[] ={, ,, , , , , , , , , , , };
  97. int count = sizeof(i_List)/; //除以4,因为一个int占4字节,最好别用这种形式,获取个数,用vector吧!
  98. vector<int> iVec(i_List, i_List + count);//数组的begin 到end赋值到这个vector中,函数原型是 vector<_Iter>(_Iter_First,_Iter_Last);
  99.  
  100. //--------string 字符 ,关于中文,unicode,要指定编码格式,
  101. vector<string> str_Vec(),str_Vec2();
  102. str_Vec.push_back("M1");
  103. str_Vec.push_back("N1");
  104. str_Vec.push_back("B1");
  105. str_Vec.push_back("V1");
  106. str_Vec.push_back("C1");
  107. str_Vec.push_back("X1");
  108. str_Vec.push_back("Z1");
  109. str_Vec.push_back("A1");
  110. str_Vec.push_back("A100");
  111. str_Vec.push_back("A102");
  112. str_Vec.push_back("A109");
  113.  
  114. str_Vec2 = str_Vec;
  115.  
  116. //method 1 数组
  117. shellsort1_1(i_List, count);
  118.  
  119. //method 2 vector<int>
  120. shellsort1_2(iVec);
  121.  
  122. //method 3 vector<string>
  123. shellsort1_3(str_Vec);
  124.  
  125. //method 4 vector<string>
  126. shellsort2(str_Vec);
  127.  
  128.   //利用sort(),最简单,因为是模版所以很简单-----另我们可以重载sort自己做compare()方法!
  129. std::sort(iVec.begin(), iVec.end());
  130. std::sort(str_Vec2.begin(), str_Vec2.end());
  131.  
  132.   //c库利用回调
      qsort(list, 9, sizeof(list[0]), callbackFunc_Compare );
      
  133. //二分查找
  134. std::binary_search(iVec.begin(), iVec.end(),34); //http://www.cplusplus.com/reference/algorithm/binary_search/
  135.  
  136. return ;
  137. }

模板的版本  =》来自  基本排序算法之1——希尔排序shellsort

  1. /*
  2. * a[] is an array to be sorted
  3. * n1 is the T array length
  4. * inc[] is the array to indecate the increasement
  5. * n2 is the inc array length
  6. */
  7. template<typename T>
  8. void shellsort(T a[],int n1,int inc[],int n2)
  9. {
  10. for(int i=;i<n2;++i)
  11. {
  12. for(int j=inc[i];j<n1;++j)
  13. {
  14. T tmp = a[j];
  15. int k;
  16. for(k=j;k>=inc[i];k-=inc[i])
  17. {
  18. if(tmp<a[k-inc[i]])
  19. a[k]=a[k-inc[i]];
  20. else
  21. break;
  22. }
  23. a[k]=tmp;
  24. }
  25. }
  26. }

两种应该掌握的排序方法--------1.shell Sort的更多相关文章

  1. 两种应该掌握的排序方法--------2.quick Sort

    介绍 http://zh.wikipedia.org/wiki/%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F 用些里面的c++ 实现,感觉这个空间复杂度比较小.还挺好 in ...

  2. 调用init方法 两种方式 一个是浏览器方法 一个是 xml中手工配置(load-on-startup)

    调用init方法 两种方式 一个是浏览器方法 一个是 xml中手工配置(load-on-startup)

  3. GIT将本地项目上传到Github(两种简单、方便的方法)

    GIT将本地项目上传到Github(两种简单.方便的方法) 一.第一种方法: 首先你需要一个github账号,所有还没有的话先去注册吧! https://github.com/ 我们使用git需要先安 ...

  4. php 两种获取分类树的方法

    php 两种获取分类树的方法 1. /** * 获取分类树 * @param array $array 数据源 * @param int $pid 父级ID * @param int $level 分 ...

  5. 两种读取.xml文件的方法

    这里介绍两种读取配置文件(.xml)的方法:XmlDocument及Linq to xml 首先简单创建一个配置文件: <?xml version="1.0" encodin ...

  6. Python 列表排序方法reverse、sort、sorted操作方法

    python语言中的列表排序方法有三个:reverse反转/倒序排序.sort正序排序.sorted可以获取排序后的列表.在更高级列表排序中,后两中方法还可以加入条件参数进行排序. reverse() ...

  7. [转]两种Sigma-Delta ADC SNR仿真方法

    假设现有一组Sigma-Delta ADC输出序列,下面将介绍两种计算出相应SNR的方法.其中由cadence导出数据的CIW窗口命令为:ocPrint(?output "输出目录/输出文件 ...

  8. Android中两种设置全屏的方法

    设置全屏的两种方法: 第一种:在配置文件里面配置: <?xml version="1.0" encoding="utf-8"?><manife ...

  9. Gradle实现的两种简单的多渠道打包方法

    本来计划今天发Android的官方技术文档的翻译--<Gradle插件用户指南>的第五章的,不过由于昨天晚上没译完,还差几段落,所以只好推后了. 今天就说一下使用Gradle进行类似友盟这 ...

随机推荐

  1. 如何将C++中的SOCKADDR_IN*参数类型转换成C#中的参数类型

    将C++中的参数类型SOCKADDR_IN*映射为C#中的IntPtr参数类型的示例代码如下: IntPtr ptrSockaddr = new IntPtr(); //ip地址 sockaddr_i ...

  2. T-SQL

    今天继续数据库知识的梳理.接下来的主要内容是T-SQL,针对的数据库是SQL Server 2008. 几个术语 数据定义语言(DDL,Data Definition Language):用来建立数据 ...

  3. html常用单词和各种少见标签

    常用单词: 空格  align="left"valign="top"align="center"valign="middle&qu ...

  4. Yii 获取验证码与Yii常用的URL

    $this->createAction('captcha')->getVerifyCode(); //获取当前验证码的值 当前页面url  echo Yii::app()->requ ...

  5. Android开发系列之SQLite

    上篇博客提到过SQLite,它是嵌入式数据库,由于其轻巧但功能强大,被广泛的用于嵌入式设备当中.后来在智能手机.平板流行之后,它作为文件型数据库,几乎成为了智能设备单机数据库的必选,可以随着安卓app ...

  6. 大型B/S系统技术总结(不断更新)

    看了<淘宝技术这十年>和<大型网站系统与Java中间件实践>这些书,对大型B/S系统的构建越来越感兴趣,于是尝试收集和总结一些常用的技术手段.不过大型网站的架构是根据业务需求不 ...

  7. 2016030206 - mysql常用命令

    参考地址如下: http://www.cnblogs.com/linjiqin/archive/2013/03/01/2939384.html http://www.cnblogs.com/zhang ...

  8. 4、Hbase

    1).逻辑模型 Hbase 以表的形式存储数据,每个表由行和列组成,每个列属于一个特定的列族. 表中由行和列确定的存储单元称为一个元素,每个元素保存了同一份数据的多个版本,由时间戳来标识.行健是数据行 ...

  9. 如何在一整张背景图中,加隐形的a标签

    很常见的一个需求,就上图每个国家图标都得加上各自指定的a标签 这时,我们就可以去加上隐藏且定位准确的几个a标签 这个时候,主要用到的就是text-indent和overflow 这两个属性的配合.te ...

  10. mongodb Install the MongoDB service

    在用到mongodb时,首先要运行mongod.exe以启动mongo,这样就会出现命令框( command prompt),为了避免出现这种情况.要以服务的形式来启动mongo,这样就不会出现命令框 ...