快排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);
}

 

转载于:https://www.cnblogs.com/javaexam2/archive/2012/03/03/2632862.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值