您的位置:首页 > 运维架构 > Shell

直接插入排序、折半插入排序、shell插入排序

2013-10-13 20:23 459 查看

直接插入排序:

/*直接插入排序(升序排列)
2013/10/13
B区自习室(正在学习C/C++常用算法手册)
Created By qianshou
*/
#include
#include
#define SIZE 10
using namespace std;
void InsertSort(int *a,int len)
{
int i,j,t,k;
for(i=1;i=0&&a[j]>t;j--)//i前面所有比t大的数据都往后移动
{
a[j+1]=a[j];
}
a[j+1]=t;					//将t的值放置到合适位置
cout<<"第"<


/*直接插入排序(降序排列)
2013/10/13
B区自习室(正在学习C/C++常用算法手册)
Created By qianshou
*/
#include
#include
#define SIZE 10
using namespace std;
void InsertSort(int *a,int len)
{
int i,j,t,k;
for(i=1;i=0&&a[j]


折半插入排序:

/*选择排序法(升序排列)
2013/10/13
B区自习室(正在学习C/C++常用算法手册)
Created By qianshou
*/
#include
#include
#define SIZE 10
using namespace std;
void SelecttionSort(int *a,int len)
{
int i,j,k,h,temp;
for(i=0;i

/*折半插入排序(降序排列)
2013/10/13
B区自习室(正在学习C/C++常用算法手册)
Created By qianshou
*/
#include
#include
#define SIZE 10
using namespace std;
void BInsertSort(int *a,int len)
{
int i,j,temp,low,high,m,h;
for(i=1;ilow-1;j--)					     //向后移动数据
{
a[j+1]=a[j];
}
a[low]=temp;
cout<<"第"<


shell插入排序:

/*shell插入排序(升序排列)
2013/10/13
B区自习室(正在学习C/C++常用算法手册)
Created By qianshou
*/
#include
#include
#define SIZE 10
using namespace std;
void ShellSort(int *a,int len)
{
int i,j,h;
int r,temp;
int x=0;
for(r=len/2;r>=1;r/=2)				//不断缩小分组的间隔,直到间隔为1时,即为直接插入排序
{
for(i=r;i=0&&a[j]>temp)		//以r为间隔为一组的数据做插入排序
{
a[j+r]=a[j];
j-=r;
}
a[j+r]=temp;
}
cout<<"间隔为:"<

/*shell插入排序(降序排列)
2013/10/13
B区自习室(正在学习C/C++常用算法手册)
Created By qianshou
*/
#include
#include
#define SIZE 10
using namespace std;
void ShellSort(int *a,int len)
{
int i,j,h;
int r,temp;
int x=0;
for(r=len/2;r>=1;r/=2)				//不断缩小分组的间隔,直到间隔为1时,即为直接插入排序
{
for(i=r;i=0&&a[j]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ 排序 算法
相关文章推荐