一、基本思想
分組插入排序
二、代碼實現
#include "stdafx.h"
#include<stdlib.h>
#include<iostream>
using namespace std;
void swap(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
void shellSort(int a[], int n)
{
for (int gap = n / 2; gap > 0; gap /= 2)
for (int i = gap; i < n; i++)
for (int j = i - gap; j >= 0 && a[j] > a[j + gap]; j -= gap)
swap(a[j], a[j + gap]);
}
int main(int argc,char* argv[]){
int n;
int a[10000];
while(cin>>n)
{
for(int i=0;i<n;i++)
{
cin>>a[i];
}
shellSort(a,n);
for(int i=0;i<n;i++)
{
cout<<a[i];
}
cout<<endl;
}
system("pause");
return 0;
}
三、性能分析
時間複雜度o(n)-o(n^2) 平均o(n^1.5)
空間複雜度:o(1)
不穩定