sort.cpp

来自「这是用VC++编的算法分析中的实现!!有每个源程序」· C++ 代码 · 共 116 行

CPP
116
字号
// Sort.cpp: implementation of the CSort class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ArithmeticDemo.h"
#include "Sort.h"

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

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

CSort::CSort(int nArraySize)
{
	if(nArraySize<0)
	{
		m_nArraySize=0;
	}
	else
	{
		m_nArraySize=nArraySize;
	}

}

CSort::~CSort()
{

}

void CSort::QuickSort(int a[],int p,int r)
{
  if(p<r)
  {
    int q=Partition(a,p,r);
    QuickSort(a,p,q-1);
    QuickSort(a,q+1,r);
  }


}

int CSort::Partition(int a[],int p,int r)
{
  int i=p,
      j=r+1;
  int x=a[p];
  while(TRUE)
  {
    while(a[++i]<x&&i<r);
    while(a[--j]>x);
    if(i>=j) break;
    Swap(a,i,j);
  }
  a[p]=a[j];
  a[j]=x;

  return j;
}

void CSort::QuickSort(double a[],int p,int r)
{
  if(p<r)
  {
    int q=Partition(a,p,r);
    QuickSort(a,p,q-1);
    QuickSort(a,q+1,r);
  }


}

int CSort::Partition(double a[],int p,int r)
{
  int i=p,
      j=r+1;
  double x=a[p];
  while(TRUE)
  {
    while(a[++i]<x&&i<r);
    while(a[--j]>x);
    if(i>=j) break;
    Swap(a,i,j);
  }
  a[p]=a[j];
  a[j]=x;

  return j;
}


void CSort::Swap(int a[],int i,int j)
{
	int temp;
	temp=a[i];
	a[i]=a[j];
	a[j]=temp;

}

void CSort::Swap(double a[],int i,int j)
{
	double temp;
	temp=a[i];
	a[i]=a[j];
	a[j]=temp;

}

⌨️ 快捷键说明

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