插入排序-希尔排序

来源:互联网 发布:修真淘宝大户 编辑:程序博客网 时间:2024/05/16 09:42
#include "stdafx.h"
#include<iostream>;
using namespace std;
void print(int a[], int n){
for (int j = 0; j < n; j++){
cout << a[j] <<"  ";
}
cout << endl; 
}


void ShellInsertSort(int a[], int n, int dk){
for (int i = dk; i < n; i++){
if (a[i] < a[i - dk]){
int j = i - dk;
int x = a[i];
a[i] = a[j];
while (x < a[j])
{
a[j + dk] = a[j];
j -= dk;
}
a[j + dk] = x;
}
print(a, n);
}
}


void ShellSort(int a[], int n){
int dk = n / 2;   
while (dk >= 1)
{
ShellInsertSort(a, n, dk);
dk /= 2;
}
}


int _tmain(int argc, _TCHAR* argv[])
{
int a[9] = { 3, 1, 5, 7, 2, 4, 9, 6,11 };
ShellSort(a, 9);
print(a, 9);
return 0;
}
0 0
原创粉丝点击