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

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. 踩过的坑系列之InputStream.read(byte[])方法

    项目之前都是好好的,最近现场那边出现一个问题,报错不是合法的json字符串,这个json字符串是通过http请求访问获得的. 通过直接在浏览器上直接访问http这个请求,发现返回的json也是完全正确 ...

  2. sea.js说明文档

    Sea.js 手册与文档 首页 | 索引 目录 模块定义 define id dependencies factory exports require require.async require.re ...

  3. SSH+Ajax实现用户名重复检查(二)

    1.另外一种更常用的js表达方式: var user = { inintEvent: function(){ $("input[name='user.User_LogName']" ...

  4. 深入理解javascript中的闭包!(转)

    1.闭包的经典错误 假如页面上有若干个div,我们想给它每个绑定一个onclick方法,于是有了下面的代码. function A(){ var divs=document.getElementsBy ...

  5. UILocalNotification本地通知

    // 执行通知一定要退出应用或挂起应用(进入后台)才能收到通知. 1.在iOS8及其以后版本中使用本地消息需要先获得用户的许可,否则无法成功注册本地消息.因此,我们将询问用户许可的代码片段添加到了ap ...

  6. GC的代机制

    1.代为CLR垃圾回收器采用的一种机制,基于以下几点假设:对象越新,生存期越短:对象越老,生存期越长: 2.托管堆仅支持3代(GC.MaxGeneration,第0代.第1代.第2代),CLR初始化会 ...

  7. Unity之串口通信(基于三姿态传感器)

    原地址:http://www.cnblogs.com/alongu3d/archive/2013/05/02/3054962.html /******************************* ...

  8. POJ2201+RMQ

    /* RMQ */ #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algo ...

  9. 总结: Struts2 + Spring 的线程安全问题

    1. 首先Struts2 本身是安全的 其原理大概是:Strtus2会获取到用户的http请求,然后负责给每个请求实例化一个Action 对象,但是大家注意,这里的action对象和Struts1里面 ...

  10. spring+mybatis 框架搭建

    注意<!-- 中间的字要保持与左右留出一个空格,否则会报错说出现两杠线 --> 1.导入jar包 aopalliance-1.0.jarasm-3.3.1.jarcglib-2.2.2.j ...