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

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. mysql基本知识

    最大连接数show variables max_connections; select VARIABLE_VALUE from information_schema.GLOBAL_VARIABLES ...

  2. iis7.5 aspx,ashx的mime类型

    映射aspx: 打开IIS管理器,找到“处理程序映射”,在列表右击选择“添加脚本映射”即可.eg:*.aspx,将该类型的页面的处理程序映射为“%windir%\Microsoft.NET\Frame ...

  3. MVC 弹出提示框

    第一种弹框成功后要刷新界面 [HttpPost] public ActionResult Add(Maticsoft.Model.Project.ProjectMoneyPlan model) { m ...

  4. UBUNTU下如何开启SSHD服务

    “人生本来就有很多事是徒劳无功的.”就像青春,虽然徒劳,却在人的心里住得最久,挥之不去,怀念那段青涩年华,怀念它的... sudo apt-get install openssh-server Ubu ...

  5. Sersync实时同步企业应用配置实战

    一.实验环境 CentOS版本: 6.6(2.6.32.-504.el6.x86_64) Rsync版本:  Rsync-3.0.6(系统自带) Sersync版本:sersync2.5.4_64bi ...

  6. Word分栏

    情景描述 Word分栏在小论文的撰写过程中是很常用的技术.但是,我们经常会遇到很难过的情况: 一段文字本来是连续分布的,可是当选择了分两栏         之后,开始部分在左边一栏,中间在右边一栏. ...

  7. NGUI系列教程一

    NGUI是Unity的一个插件,使用它来制作你的游戏UI必定将事半功倍.为什么这么说呢?首先我们说说GUI与NGUI的区别,GUI是Unity自带的绘制界面工具,它的成像原理是基于表层的,所以执行效率 ...

  8. Automotive Security的一些资料和心得(7):AUTOSAR和Security

    1. 密码模块[1] 密码模块在Services Layer Configurable and common access to 密码子程序 硬件支持密码模块 2. 应用 应用和密码子程序分离 Cry ...

  9. A Neural Network in 11 lines of Python

    A Neural Network in 11 lines of Python A bare bones neural network implementation to describe the in ...

  10. U3D版本《暗黑世界V1.0》编译——图文教程!

    原地址:http://blog.csdn.net/uxqclm/article/details/11970773 欢迎来到9秒:www.9miao.com 说明: A. 工具准备:          ...