快速排序

来源:互联网 发布:sql语句大全 创建用户 编辑:程序博客网 时间:2024/06/05 19:24
// SomeTest.cpp : Defines the entry point for the console application.
//


#include "stdafx.h"
#include <math.h>
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;


void QuickSort(int A[],int p,int r);
int Partition(int A[],int p,int r);


int _tmain(int argc, _TCHAR* argv[])
{
int data[11];//只使用1-10
srand((unsigned)time(NULL));//用当前时间,设置种子 
for (int i = 0;i < 11;i++)
{
data[i] = rand();


}
QuickSort(data, 1, 10);
for(int i = 1;i< 11;i++)
{
cout<<data[i]<<"   ";



getchar();
}


void QuickSort(int A[],int p,int r)
{
if ( p < r)
{
int q = Partition(A, p, r);
QuickSort(A, p, (q - 1));
QuickSort(A, q, r);
}


}
int Partition(int A[],int p,int r)
{
int x = A[r];
int i = p - 1;
for (int j = p; j < r;j++)
{
if (A[j] <= x)
{
i++;
int temp = A[j];
A[j] = A[i];
A[i] = temp;
}
}


int tempEx = A[i + 1];
A[i + 1] = A[r];
A[r] = tempEx;
return (i + 1);
}
原创粉丝点击