shellsort.cpp

来自「包含八种排序算法的界面演示多线程程序,以线段形式表示数据大小,排序一清二楚」· C++ 代码 · 共 54 行

CPP
54
字号
// ShellSort.cpp: implementation of the CShellSort class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "sort.h"
#include "ShellSort.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CShellSort::CShellSort()
{
	name=_TEXT("Shell");
}

CShellSort::~CShellSort()
{

}

void CShellSort::Sort()
{
	for (int i=n/2;i>2;i/=2)
		InsertSort(i);
	InsertSort(1);
	State2=2;
}

void CShellSort::InsertSort(int step)
{
	for (int i=0;i<step;i++)
		for (int j=i+step;j<n;j+=step)
			for (int k=j;(k-step>=0&&p[k]<p[k-step]);k-=step)
			{
				if (State2==-1)
				{
					State2=1;
					return;
				}
				int temp=p[k];
				p[k]=p[k-step];
				p[k-step]=temp;
				if (CanSleep) Sleep();
			}
}

⌨️ 快捷键说明

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