quicksort.cpp

来自「归并排序」· C++ 代码 · 共 45 行

CPP
45
字号
#include "QuickSort.h"

template<class T>
int Partition(T a[],int low,int high)
{
	T temp;
	temp=a[low];
	int i,j;
	i=low+1;
	j=high;
	while(i<j)
	{
		while(i<=j && a[i]<temp)
		  i++;
		while(i<=j && a[j]>temp)
		  j--;
		if(i<j)
	  	Swap(a[i],a[j]);
	}
	a[low]=a[j];
	a[j]=temp;
	return j;	
}

template<class T>
void Swap(T a,T b)
{
	T temp;
	temp=a;
	a=b;
	b=temp;
}

template<class T>
void QuickSort(T a[],int low,int high)
{
	int j;
	if (low<high)
	{
		j= Partition(a,low,high);
		QuickSort(a,low,j-1);
		QuickSort(a,j+1,high);
	}
}

⌨️ 快捷键说明

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