【算法】Shell排序--C++源代码(VS2015)
2017-11-01 21:42
357 查看
#include <iostream>
#include <string.h>
using namespace std;
int interval[] = { 1,3,5,7,9 };
void Print(int* arr, int size)
{
int i;
for (i = 0; i < size; i++)
{
printf("%d ,", *(arr + i));
}
printf("\n");
}
/*
相当于多个选择排序
*/
void ShellSort(int arr[], int size)
{
int step, stepsp = sizeof(interval) / sizeof(int) - 1;
int i, j, tmp;
while (stepsp >= 0)
{
step = interval[stepsp--];
for (i = step; i < size; i++)
{
tmp = arr[i];
for (j = i; j >= step && arr[j - step] > tmp; j -= step)
{
arr[j] = arr[j - step];
}
arr[j] = tmp;
}
}
}
int main()
{
int data[] = { 81,94,11,96,12,35,17,95,28,58,41,75,15 };
int size = sizeof(data) / sizeof(int);
ShellSort(data, size);
Print(data, size);
system("pause");
return 0;
}
#include <string.h>
using namespace std;
int interval[] = { 1,3,5,7,9 };
void Print(int* arr, int size)
{
int i;
for (i = 0; i < size; i++)
{
printf("%d ,", *(arr + i));
}
printf("\n");
}
/*
相当于多个选择排序
*/
void ShellSort(int arr[], int size)
{
int step, stepsp = sizeof(interval) / sizeof(int) - 1;
int i, j, tmp;
while (stepsp >= 0)
{
step = interval[stepsp--];
for (i = step; i < size; i++)
{
tmp = arr[i];
for (j = i; j >= step && arr[j - step] > tmp; j -= step)
{
arr[j] = arr[j - step];
}
arr[j] = tmp;
}
}
}
int main()
{
int data[] = { 81,94,11,96,12,35,17,95,28,58,41,75,15 };
int size = sizeof(data) / sizeof(int);
ShellSort(data, size);
Print(data, size);
system("pause");
return 0;
}
相关文章推荐
- 【算法】分而治之(DivideAndConquer) -- C++源代码(VS2015)
- 【算法】快速排序--C++源代码(VS2015)
- 【算法】斐波那契数列--C++源代码(VS2015)
- 【LeetNode2-1-8】Three sum--C++源代码(VS2015)
- 【LeetNode2-1-9】Three sum closest--C++源代码(VS2015)
- 【LeetNode2-1-10】Four sum--C++源代码(VS2015)
- 【设计】单例模式--C++源代码(VS2015)
- 【LeetNode2-1-1】Remove duplicates from sorted array I--C++源代码(VS2015)
- 【LeetNode2-1-2】Remove duplicates from sorted array II--C++源代码(VS2015)
- 【基础】简析浅拷贝与深拷贝--C++源代码(VS2015)
- 【基础】随机数生成--C++源代码(VS2015)
- 【LeetNode2-1-4】Search in rotated sorted array II--C++源代码(VS2015)
- 【LeetNode2-1-5】Median of two sorted arrays--C++源代码(VS2015)
- 【基础】大端小端(Big-endian&Little-endian)--C++源代码(VS2015)
- 【LeetNode2-1-6】Longest consecutive sequence--C++源代码(VS2015)
- 【设计】工厂模式--C++源代码(VS2015)
- 【LeetNode2-1-7】Two sum--C++源代码(VS2015)
- 图像处理之其他杂项(一)之MeanShift的目标跟踪算法opencv c++代码 VS2015+opencv3.2
- VS2015如何新建C++或者C语言版的lib文件
- vs2015中复制C++ DLL 和.pdb文件到C#工程中bin目录的设置方法