希尔排序

来源:互联网 发布:cnpm 安装node sass 编辑:程序博客网 时间:2024/05/28 23:11

#include <iostream>

#include <limits>
#include<stdlib.h>
using namespace std;


//希尔排序,分成d组 
void shellInsert(int A[],int len,int d){
for(int i=0;i<d;i++){
for(int j=i+d;j<len;j+=d){//插入排序 
int temp=A[j];
int index=j-d;
while(index>=i&&A[index]>temp){
A[index+d]=A[index];
index-=d; 

A[index+d]=temp;
}
}
return;



void shellSort(int A[],int len){
if(A==NULL||len<=0)
return;
int d=len/2;
while(d>=1){
shellInsert(A,len,d);
d/=2;
}
return;


int main(int argc, char** argv) {
int A[9];
A[0]=0;
A[1]=3;
A[2]=4;
A[3]=-1;
A[4]=9;
A[5]=7;
A[6]=8;
A[7]=8;
A[8]=80;
        A[9]=-90;
shellSort(A,10);
for(int i = 0;i<10;i++){
cout<<A[i]<<",";
}
cout<<endl; 
return 0;

}

0 0
原创粉丝点击