Demo entry 6730955

吕永强

   

Submitted by ahnulyq on Apr 10, 2018 at 03:14
Language: C. Code size: 844 Bytes.

/*
*希尔排序算法
* Time: 2018年4月9日 By 楷谐之力
*/
#include<stdio.h>  
#include<stdlib.h>  
#define N 8  
void printAaary(int *a, int n)  //打印数组  
{
	int i = 0;
	for (i = 0; i<n; i++)
		printf("%5d", a[i]);
	printf("\n");
}
void shell_sort(int a[], int n)   //希尔排序算法  
{
	int i, gap = n / 2, j;   //gap表示分割步长   
	int k;
	while (gap>0)
	{
		for (i = gap; i<n; i++)
		{
			int t = a[i];    //临时变量,保存a[i]的值   
			j = i - gap;
			while (j >= 0 && a[j]>t)//插入排序算法   
			{
				a[j + gap] = a[j];
				j = j - gap;
			}
			a[j + gap] = t;
		}
		printf("gap=%d:", gap);
		for (k = 0; k<n; k++)
			printf("%d  ", a[k]);
		printf("\n");
		gap /= 2;
	}
}
int main()
{
	int a[N] = { 695, 458, 362, 789, 12, 15, 163, 23 };
	printf("\n排序前:\n");
	printAaary(a, N);
	shell_sort(a, N);
	printf("\n排序后:\n");
	printAaary(a, N);
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).