![]() |
#2
pangding2011-05-20 15:23
|

#include<iostream>
#include<iomanip>
using namespace std;
int P(int r[], int,int);
void Q(int r[], int,int );
#include<ctime>
#include<cstdlib>
int main()
{
const int M=100000;
int size;
int x=1;
int A[M];
srand(time(0));
for(int y=0;y<=M;y++)
{
A[y]= 1+rand()%100000;
}
cout<<"请输入要排序的范围数: ";
cin>>size;
//cout<<endl;
time_t first1,end1;
first1=clock();
Q(A, x, size);
end1=clock();
cout<<"所需的时间为: "<<double(end1-first1);
return 0;
}
void Q(int r[], int first, int end)
{
if (first<end)
{ //递归结束
int pivot=P(r, first, end); //一次划分
Q(r, first, pivot-1); //递归地对左侧子序列进行快速排序
Q(r, pivot+1, end); //递归地对右侧子序列进行快速排序
}
}
int P(int r[], int first, int end)
{
int i=first; //初始化
int j=end;
int temp;
while (i<j)
{
while (i<j && r[i]<= r[j])
j--; //右侧扫描
if (i<j)
{
temp=r[i]; //将较小记录交换到前面
r[i]=r[j];
r[j]=temp;
i++;
}
while (i<j && r[i]<= r[j])
i++; //左侧扫描
if (i<j)
{
temp=r[j];
r[j]=r[i];
r[i]=temp; //将较大记录交换到后面
j--;
}
}
return i; //i为轴值记录的最终位置
}
#include<iomanip>
using namespace std;
int P(int r[], int,int);
void Q(int r[], int,int );
#include<ctime>
#include<cstdlib>
int main()
{
const int M=100000;
int size;
int x=1;
int A[M];
srand(time(0));
for(int y=0;y<=M;y++)
{
A[y]= 1+rand()%100000;
}
cout<<"请输入要排序的范围数: ";
cin>>size;
//cout<<endl;
time_t first1,end1;
first1=clock();
Q(A, x, size);
end1=clock();
cout<<"所需的时间为: "<<double(end1-first1);
return 0;
}
void Q(int r[], int first, int end)
{
if (first<end)
{ //递归结束
int pivot=P(r, first, end); //一次划分
Q(r, first, pivot-1); //递归地对左侧子序列进行快速排序
Q(r, pivot+1, end); //递归地对右侧子序列进行快速排序
}
}
int P(int r[], int first, int end)
{
int i=first; //初始化
int j=end;
int temp;
while (i<j)
{
while (i<j && r[i]<= r[j])
j--; //右侧扫描
if (i<j)
{
temp=r[i]; //将较小记录交换到前面
r[i]=r[j];
r[j]=temp;
i++;
}
while (i<j && r[i]<= r[j])
i++; //左侧扫描
if (i<j)
{
temp=r[j];
r[j]=r[i];
r[i]=temp; //将较大记录交换到后面
j--;
}
}
return i; //i为轴值记录的最终位置
}
求解释。。。。。