shellsort.cpp

来自「包含8种常用的排序。如快速排序」· C++ 代码 · 共 51 行

CPP
51
字号
#include <iostream.h>
#include <stdio.h>
#include <time.h>
#include<stdlib.h>
#include <algorithm>
using namespace std;
#define N 10000
int a[N];

void ShellSort()
{
	int i,j,d,k,m;
	d=N/2;
	while(d>0)
	{
		for(i=0;i<d;i++)
		{
			for(j=i;j<N;j=j+d)
			{
				m=a[j];
				for(k=j-d;k>=0;k=k-d)
				{
					if (m>a[k]) break;
					a[k+d]=a[k];
				}
				a[k+d]=m;
			}
		}
		d=d/2;
	}
	for(i=0;i<N-1;i++)
	{cout<<a[i]<<"->";}
    cout<<a[i]<<"\n";

}

void main()
{  
   int k;
   clock_t start=clock();/*程序开始运行的时间*/

   //srand((unsigned)time(NULL));//随时间的不同产生不同的随机数
   for(k=0;k<N;k++)
		a[k]=rand()%N+1;
   ShellSort();

   clock_t end=clock();/*程序结束运行的时间*/
   cout<<"整个程序运行的时间为:";
   cout<<(end-start)<<"毫秒\n";
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?