您的位置:首页 > 编程语言 > C语言/C++

快排c++实现

2012-03-03 10:32 169 查看
// 算法.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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: