希尔排序

来源:岁月联盟 编辑:猪蛋儿 时间:2011-12-30
#include "stdafx.h" 
#include <iostream> 
using namespace std; 
 
//希尔算法(升序) 
void InsertSort(int a[], int n) 

    int h,i,j,temp; 
    for (h=n/2; h>0; h=h/2) 
    { 
        for (i=h; i<n; i++) 
        { 
            temp = a[i]; 
            for (j=i-h; j>=0 && temp < a[j]; j-=h) 
            { 
                a[j+h] = a[j]; 
            } 
            a[j+h] = temp; 
        } 
    } 
     

 
//输出数组  www.2cto.com
void Print_Arry(int a[], int n) 

    for (int i=0; i<n; i++) 
    { 
        cout<< a[i]<<" "; 
    } 
    cout<<endl; 

 
int _tmain(int argc, _TCHAR* argv[]) 

    int a[9] = {7,3,5,8,9,1,2,4,6}; 
    InsertSort(a, 9); 
    Print_Arry(a, 9); 
    return 0; 


摘自 学无止境

图片内容