📄 sort.cpp
字号:
// 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -