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

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.

# Sort an array a[0...n-1].
gaps = [701, 301, 132, 57, 23, 10, 4, 1] foreach (gap in gaps)
{
# Do an insertion sort for each gap size.
for (i = gap; i < n; i += 1)
{
temp = a[i]
for (j = i; j >= gap and a[j - gap] > temp; j -= gap)
{
a[j] = a[j - gap]
}
a[j] = temp
} }

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

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

#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>
#include <algorithm> //just for sort() and binary_search() using namespace std; //method 1 数组方式 ok
void shellsort1_1(int *data, size_t size)
{
for (int gap = size / ; gap > ; gap /= )
for (size_t i = gap; i < size; ++i)
{
int Temp = data[i];
int j = ;
for( j = i -gap; j >= && data[j] > Temp; j -=gap)
{
data[j+gap] = data[j];
}
data[j+gap] = Temp;
}
} //method 2 ok
void shellsort1_2(vector<int> &squeue_)
{
vector<int>::size_type size = squeue_.size(); for (int gap = size / ; gap > ; gap /= )
for (size_t i = gap; i < size; ++i)
{
int j = ;
int temp = squeue_[i]; // data[i]; for( j = i -gap; j >= && squeue_[j] > temp; j -=gap)
{
squeue_[j+gap] = squeue_[j];
}
squeue_[j+gap] = temp;//squeue_[i];
}
} //method 3 ok
void shellsort1_3(vector<string> &squeue_)
{
vector<string>::size_type size = squeue_.size(); for (int gap = size / ; gap > ; gap /= )
for (size_t i = gap; i < size; ++i)
{
int j = ;
string temp = squeue_[i]; for( j = i -gap; j >= && squeue_[j] > temp; j -=gap)
{
squeue_[j+gap] = squeue_[j];
}
squeue_[j+gap] = temp;//squeue_[i];
}
} //method 4 ok
void shellsort2(vector<string> &gaps)
{
size_t gap = ;
size_t j = ;
string temp("");
size_t count = gaps.size(); for (vector<string>::iterator it = gaps.begin(); it != gaps.end(); ++it, gap +=)//for_each (gap in gaps)
{
// Do an insertion sort for each gap size.
for (size_t i = gap ; i < count; i += )
{
temp = gaps[i];
for (j = i; j >= gap && gaps[j - gap] > temp; j -= gap)
{
gaps[j] = gaps[j - gap];
}
gaps[j] = temp;
}
}
}

//c 库的sort
int index = 1;
int list[9] = { 5, 2, 3, 9, 4, 6, 7, 8, 1}; int callbackFunc_Compare(const void* a , const void *b)
{
    printf("index = %d,   a= %d, b = %d \n", index++, *(int*)a, *(int*)b);     for (int i = 0; i < 9; i++)
    {
        printf("%d ",list[i]);
    }
    printf("\n");     return *(int*)a - *(int*)b;
} int _tmain(int argc, _TCHAR* argv[])
{
//--------int
int i_List[] ={, ,, , , , , , , , , , , };
int count = sizeof(i_List)/; //除以4,因为一个int占4字节,最好别用这种形式,获取个数,用vector吧!
vector<int> iVec(i_List, i_List + count);//数组的begin 到end赋值到这个vector中,函数原型是 vector<_Iter>(_Iter_First,_Iter_Last); //--------string 字符 ,关于中文,unicode,要指定编码格式,
vector<string> str_Vec(),str_Vec2();
str_Vec.push_back("M1");
str_Vec.push_back("N1");
str_Vec.push_back("B1");
str_Vec.push_back("V1");
str_Vec.push_back("C1");
str_Vec.push_back("X1");
str_Vec.push_back("Z1");
str_Vec.push_back("A1");
str_Vec.push_back("A100");
str_Vec.push_back("A102");
str_Vec.push_back("A109"); str_Vec2 = str_Vec; //method 1 数组
shellsort1_1(i_List, count); //method 2 vector<int>
shellsort1_2(iVec); //method 3 vector<string>
shellsort1_3(str_Vec); //method 4 vector<string>
shellsort2(str_Vec);   //利用sort(),最简单,因为是模版所以很简单-----另我们可以重载sort自己做compare()方法!
std::sort(iVec.begin(), iVec.end());
std::sort(str_Vec2.begin(), str_Vec2.end());   //c库利用回调
  qsort(list, 9, sizeof(list[0]), callbackFunc_Compare );
  
//二分查找
std::binary_search(iVec.begin(), iVec.end(),34); //http://www.cplusplus.com/reference/algorithm/binary_search/ return ;
}

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

/*
* a[] is an array to be sorted
* n1 is the T array length
* inc[] is the array to indecate the increasement
* n2 is the inc array length
*/
template<typename T>
void shellsort(T a[],int n1,int inc[],int n2)
{
for(int i=;i<n2;++i)
{
for(int j=inc[i];j<n1;++j)
{
T tmp = a[j];
int k;
for(k=j;k>=inc[i];k-=inc[i])
{
if(tmp<a[k-inc[i]])
a[k]=a[k-inc[i]];
else
break;
}
a[k]=tmp;
}
}
}

两种应该掌握的排序方法--------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++实现一个单例模板类

    单例模式在项目开发中使用得比较多,一个单例的模板类显得很有必要,避免每次都要重复定义一个单例类型 //非多线程模式下的一个单例模板类的实现 // template_singleton.h #inclu ...

  2. 关于css中overflow:hidden的使用

    overflow:hidden有两个用处经常用到: 1.通过设定自身的高度,加上overflow:hidden可以隐藏超过容器本身的内容:     但是,小编在以往的使用中,发现了一个问题,只要父级容 ...

  3. 查找计算机IP及占用端口

    1. 在电脑启动搜索框,输入cmd回车打开命令提示符窗口. 输入ipconfig,就可以查看电脑的子网淹没,默认网关,IP等信息. 2. 查看本机开放的端口,即已被占用的端口号. 命令: netsta ...

  4. Project not selected to build for this solution configuration.

    Project not selected to build for this solution configuration.   When you upgrade your older solutio ...

  5. 树莓派 raspberry 入门之安装操作系统以及配置

    最近新入手一树莓派,型号是2代B,屏幕是微雪的7 inch c型 显示屏.下面来教大家怎么点亮树莓派. 第一步,装好显示器,显示器的电源接在树莓派的usb口上,HDMI口不多说,连上.然后装好鼠标.键 ...

  6. 数组有N+M个数字, 数字的范围为1 ... N, 打印重复的元素, 要求O(M + N), 不可以用额外的空间

    数组有N+M个数字, 数字的范围为1 ... N, 打印重复的元素, 要求O(M + N), 不可以用额外的空间 1.题目中要求我们不能使用额外的空间,那么我们能采用在原数组上做文章,这里的重点是如何 ...

  7. Kinetic使用注意点--blob

    new Blob(config) 参数: config:包含所有配置项的对象. { points: "存放路径点的数组,可以用一层数组[a,b,c,d].二层数组[[a,b],[c,d]]或 ...

  8. 开发软件设计模型 visual studio UML

    http://www.ibm.com/developerworks/cn/rational/rationaledge/content/feb05/bell/ http://msdn.microsoft ...

  9. DB天气app冲刺第三天

    昨天很郁闷而且烦躁的的过了一天 什么也没弄.今天其实也没有怎么做..进度非常慢.. 因为个人的问题 所以这两天的效率非常慢. 但今天还是做了一些东西.把listview做出来了.做出了一个按钮的效果. ...

  10. RandomAccessFile类的使用(随机读取java中的文件)

    package coreJava; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; ...