先将整个待排元素序列分割成若干子序列(由相隔某个“增量”的元素组成的)分别进行直接插入排序,然后依次缩减增量再进行排序,待整个序列中的元素基本有序(增量足够小)时,再对全体元素进行一次直接插入排序(增量为1)
希尔排序的时间复杂度跟选取步长序列有关,如上面的代码中,步长序列如果是 n/2i 的话,最坏的情况下的复杂度为 O(n2) ,步长序列如果是 (3k–1)/2 的话,最坏的情况下的复杂度为 O(n3/2) 。
void Shell_sort(int arr[], int len)
{
int increment = len / 2;
while(increment)
{
for(int i = increment; i < len; i ++)
{
int key = arr[i];
/*int j ; for(j = i; j >= increment; j -= increment) { if(arr[j-increment] > key ) arr[j] = arr[j-increment]; else break; }*/
int j = i;
while(j >= increment && arr[j-increment] > key)
{
arr[j] = arr[j-increment];
j -= increment;
}
arr[j] = key;
}
increment /= 2;
}
}
void Shell_sort1(int arr[], int len)
{
int increment = 0;
for(increment = len/2; increment > 0; increment /=2)
{
for(int i = increment; i < len; i++)
{
int key = arr[i];
int j = 0;
for(j = i; j >= increment; j -=increment)
{
if(arr[j-increment] > key)
arr[j] = arr[j-increment];
else
break;
}
arr[j] = key;
}
}
}
void Shell_sort2(int arr[], int len)
{
int index = log( 2*len + 1) / log(3.0);
//cout << index << endl;
int increment = ( pow(3.0, index) - 1 ) / 2;
//cout << increment << endl;
while(increment)
{
for(int i = increment; i < len; i ++)
{
int key = arr[i];
/*int j ; for(j = i; j >= increment; j -= increment) { if(arr[j-increment] > key ) arr[j] = arr[j-increment]; else break; }*/
int j = i;
while(j >= increment && arr[j-increment] > key)
{
arr[j] = arr[j-increment];
j -= increment;
}
arr[j] = key;
}
index -= 1;
increment = ( pow(3.0, index) - 1 ) / 2;
}
}
#include <iostream>
#include <cstring>
#include <ctime>
#include <cmath>
using namespace std;
#define ArraySize 100000
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
void Bubble_sort(int arr[], int len)
{
for(int i = 0; i < len; i ++)
{
for(int j = i + 1; j < len; j ++)
if(arr[i] > arr[j])
swap(arr[i], arr[j]);
}
}
void Bubble_sort1(int arr[], int len)
{
for(int i = 0; i < len; i ++)
{
for(int j = len - 1; j >= i; j --)
{
if(arr[i] > arr[j])
swap(arr[i], arr[j]);
}
}
}
void Bubble_sort2(int arr[], int len)
{
bool flag = true;
while(flag)
{
flag = false;
for(int i = 0; i < len; i ++)
for(int j = len - 1; j >= i; j --)
if(arr[i] > arr[j])
swap(arr[i], arr[j]);
}
}
void Slect_sort(int arr[], int len)
{
for(int i = 0; i < len; i ++)
{
int min_index = i ;
for(int j = i + 1; j < len; j ++)
{
if(arr[min_index] > arr[j])
min_index = j;
}
if(i != min_index)
swap(arr[i],arr[min_index]);
}
}
void Insert_sort(int arr[], int len)
{
for(int i= 1; i < len; i ++)
{
int key = arr[i];
int j = i;
while(j && arr[j - 1] > key)
{
arr[j] = arr[j - 1];
j --;
}
arr[j] = key;
}
}
void Shell_sort(int arr[], int len)
{
int increment = len / 2;
while(increment)
{
for(int i = increment; i < len; i ++)
{
int key = arr[i];
/*int j ; for(j = i; j >= increment; j -= increment) { if(arr[j-increment] > key ) arr[j] = arr[j-increment]; else break; }*/
int j = i;
while(j >= increment && arr[j-increment] > key)
{
arr[j] = arr[j-increment];
j -= increment;
}
arr[j] = key;
}
increment /= 2;
}
}
void Shell_sort1(int arr[], int len)
{
int increment = 0;
for(increment = len/2; increment > 0; increment /=2)
{
for(int i = increment; i < len; i++)
{
int key = arr[i];
int j = 0;
for(j = i; j >= increment; j -=increment)
{
if(arr[j-increment] > key)
arr[j] = arr[j-increment];
else
break;
}
arr[j] = key;
}
}
}
void Shell_sort2(int arr[], int len)
{
int index = log( 2*len + 1) / log(3.0);
//cout << index << endl;
int increment = ( pow(3.0, index) - 1 ) / 2;
//cout << increment << endl;
while(increment)
{
for(int i = increment; i < len; i ++)
{
int key = arr[i];
/*int j ; for(j = i; j >= increment; j -= increment) { if(arr[j-increment] > key ) arr[j] = arr[j-increment]; else break; }*/
int j = i;
while(j >= increment && arr[j-increment] > key)
{
arr[j] = arr[j-increment];
j -= increment;
}
arr[j] = key;
}
index -= 1;
increment = ( pow(3.0, index) - 1 ) / 2;
}
}
void Print_array(int arr[], int len)
{
for(int i = 0; i < len; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
int main(int argc, char const *argv[])
{
/* code */
int Array[ArraySize];
int Array1[ArraySize];
int Array2[ArraySize];
time_t begin , end;
srand(time(NULL));
for(int i = 0; i < ArraySize; i ++)
{
Array[i] = rand()%ArraySize;
//cout << Array[i] << " ";
}
memcpy(Array1, Array, ArraySize * sizeof(Array1[0]));
memcpy(Array2, Array, ArraySize * sizeof(Array2[0]));
// Print_array(Array, ArraySize);
/* begin = clock(); Bubble_sort2(Array, ArraySize); end = clock(); cout << "Bubble_sort runtime: " << double(end - begin) / CLOCKS_PER_SEC << "s" << endl; begin = clock(); Slect_sort(Array1, ArraySize); end = clock(); cout << "Slect_sort runtime: " << double(end - begin) / CLOCKS_PER_SEC << "s" << endl;*/
begin = clock();
Insert_sort(Array2, ArraySize);
end = clock();
cout << "Insert_sort runtime: " << double(end - begin) / CLOCKS_PER_SEC << "s" << endl;
begin = clock();
Shell_sort1(Array1, ArraySize);
end = clock();
cout << "Shell_sort1 runtime: " << double(end - begin) / CLOCKS_PER_SEC << "s" << endl;
begin = clock();
Shell_sort2(Array2, ArraySize);
end = clock();
cout << "Shell_sort2 runtime: " << double(end - begin) / CLOCKS_PER_SEC << "s" << endl;
//Print_array(Array2, ArraySize);
return 0;
}
运行结果如下,明显比插入排序快很多
Insert_sort runtime: 6.367s
Shell_sort1 runtime: 0.024s
Shell_sort2 runtime: 0.004s