//header in use
#include <stdio.h>
#include <tchar.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
using namespace std;
#define MAX 20
int _tmain(int argc, _TCHAR* argv[])
{
int shellbox[MAX];
void shellsort(int *, int);
srand(time(NULL)); //using rand() and time() to build ramdom number vector
for(int i = 0; i != MAX; i++)
{
shellbox[i] = rand() % 1000;
cout<<shellbox[i]<<' ';
}
shellsort(shellbox, MAX);
cout << endl;
for(int i = 0; i != MAX; i++)
{
cout<<shellbox[i]<<' ';
}
return 0;
}
void shellsort(int *shell, int max)
{
int n = (int)max/2;
int length = 0; //small vector length in all shell box
int end = 0;
int temp;
while(!(n==0)){ // shell sort start
length=n*int((max - 1)/n);
///////////////////////////////////////////////////////////////////
for(int i = 0; i != n; i++){
end = i +length;
for(int j = end; j > 0;){
/////////////////////////////////////////////////////////////////// sort from i to end
for(int c = i; c < j;){ // c is the change, i is the start, j is the end
if(shell[c] > shell[c+n]){
temp = shell[c];
shell[c] = shell[c+n];
shell[c+n] = temp;
}
c = c +n;
}
///////////////////////////////////////////////////////////////////
j = j - n;
}
}
///////////////////////////////////////////////////////////////////
n=int(n/2);
}
return;
}
自己实现的shell排序,底层排序用的是冒泡法。
the shell sort from Data Srtuctures and Algorithm Analysis in C++
/**
* Shellsort, using Shell's (poor) increments.
*/
template <class Comparable>
void shellsort( vector<Comparable> & a )
{
for( int gap = a.size( ) / 2; gap > 0; gap /= 2 )
for( int i = gap; i < a.size( ); i++ )
{
Comparable tmp = a[ i ];
int j = i;
for( ; j >= gap && tmp < a[ j - gap ]; j -= gap )
a[ j ] = a[ j - gap ];
a[ j ] = tmp;
}
}
分享到:
相关推荐
shell sort, 希尔排序, 又称贝壳排序, C++程序
ShellSort-ShellSort
数据结构课程排序算法中的经典shell排序
linux shell sort从入门到精通
Python手撕算法ShellSort
var shellsort = require ( 'shellsort' ) ; // Ascending order shellsort ( [ 3 , 1 , 4 , 1 , 5 , 9 , 2 , 6 , 5 , 4 ] ) ; // => [1,1,2,3,4,4,5,5,6,9] shellsort ( [ 9 , 2 , 8 , 6 , 1 , 3 ] ) ; // => [1,2...
4.6shellsort.zip
排序算法 排序算法_基于C语言实现的排序算法之ShellSort实现
7-2算法ShellSort1
shellsort希尔排序算法增加最佳组合1
基于python的排序算法-希尔排序Shell Sort
壳排序Java中的Shell Sort实现
void ShellSort(int* iArray,int length) { //初始化jump等于length int jump = length; //标记本趟检测是否进行了交换, // 若进行了 则还有下次从头开始的检测, // 否则停止,继续改变jump的值 做另一趟排序...
本文实例讲述了PHP排序算法之希尔排序(Shell Sort)。分享给大家供大家参考,具体如下: 基本思想: 希尔排序是指记录按下标的一定增量分组,对每一组使用 直接插入排序 ,随着增量逐渐减少,每组包含的关键字越来越...
使用MATLAB实现一个简单的希尔排序的例子
严蔚敏数据结构与算法 课本算法实现
严蔚敏数据结构与算法 课本算法实现
ShellSort
壳排序ShellSort for Java int