快排c++实现

// 算法.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <ctime>
using namespace std;

void QuickSort(int e[], int first, int end);

int _tmain(int argc, _TCHAR* argv[])
{
	srand(unsigned(time(NULL)));//set 种子

	int t_nArray[10];
	//产生随即数组
	for(int i = 0; i < 10; i++)
	{
		t_nArray[i] = rand()%100;
	}
	//快速排序
	 QuickSort(t_nArray, 0, 9);
	//显示数组
	for(int i = 0; i < 10; i++)
	{
		cout << t_nArray[i] ;
		cout << "," ;
	}
	system("pause");
	return 0;
}

void QuickSort(int e[], int first, int end)
{
	int i=first,j=end;
	int temp=e[first];//记录第一个数据
	
	while(i<j)
  {
		while(i<j && e[j]>=temp)	//与first数据比较,右边下标逐渐左移
			j--;

		e[i]=e[j];		

		while(i<j && e[i]<=temp)	//与first数据比较,左边下标逐渐右移
			i++;		

		e[j]=e[i];
  }
	e[i]=temp;						//将first数据放置于i=j处

	if(first<i-1)
	QuickSort(e,first,i-1);
	if(end>i+1)		
	QuickSort(e,i+1,end);
}
 
相关文章
相关标签/搜索