第1次测试:
数据量:一万
生成随机数赋值耗时:0毫秒
最大快速排序耗时:15毫秒
第2次测试:
数据量:十万
生成随机数赋值耗时:0毫秒
最大快速排序耗时:156毫秒
第3次测试:
数据量:一百万
生成随机数赋值耗时:47毫秒
最大快速排序耗时:1887毫秒
第4次测试:
数据量:一千万
生成随机数赋值耗时:344毫秒
最大快速排序耗时:25131毫秒
第5次测试:
数据量:三千万
生成随机数赋值耗时:968毫秒
最大快速排序耗时:116641毫秒
第6次测试:
数据量:五千万
生成随机数赋值耗时:1903毫秒
最大快速排序耗时:288758毫秒
第7次测试:
数据量:一亿
生成随机数赋值耗时:3416毫秒
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
#include <stdlib.h>
{
int temp = *a;
*a = *b;
*b = temp;
}
int partition(int *p, int start, int end)
{
int x = p[end];
int i = start - 1;
for (int j = start; j < end; j++)
{
if (p[j] <= x)
{
i = i + 1;
if (i != j)
swap(p[i], p[j]);
}
}
swap(p[i + 1], p[end]);//此p[end] != x.因为它已经改变了。否则就会错误。
return i + 1;
}
void quicksort(int *A, int p, int r)
{
int q;
if (p < r)
{
q = partition(A, p, r);
quicksort(A, p, q - 1);
quicksort(A, q + 1, r);
}
}
int main()
{
//****************************************
DWORD start_time, end_time;
unsigned int total_1, total_2;
//**************主程序插入下***************
typedef unsigned int intdata;
//const unsigned int LENGTH = 1000000;
//int A[LENGTH];
DWORD LENGTH = 100000000;
int *A = NULL;
A = (int *)malloc(LENGTH * sizeof(int));
if (NULL == A)
{
free(A);
printf("内存分配失败,程序退出!\n");
system("pause");
return -1;
}
srand((unsigned)time(0));
start_time = GetTickCount();
for (intdata i = 0; i < LENGTH; i++)
{
*(A + i) = rand();
}
end_time = GetTickCount();
total_1 = end_time - start_time;
start_time = GetTickCount();
quicksort(A, 0, LENGTH - 1);
end_time = GetTickCount();
total_2 = end_time - start_time;
cout << endl << "生成随机数耗时:" << total_1 << "毫秒" << endl;
cout << "快速排序时间为:" << total_2 << "毫秒!" << endl;
//**************主程序插入上***************
free(A);
for (int i = 0; i < 10; i++)
{
cout << endl;
}
system("pause");
return 0;
}
数据量:一万
生成随机数赋值耗时:0毫秒
最大快速排序耗时:15毫秒
第2次测试:
数据量:十万
生成随机数赋值耗时:0毫秒
最大快速排序耗时:156毫秒
第3次测试:
数据量:一百万
生成随机数赋值耗时:47毫秒
最大快速排序耗时:1887毫秒
第4次测试:
数据量:一千万
生成随机数赋值耗时:344毫秒
最大快速排序耗时:25131毫秒
第5次测试:
数据量:三千万
生成随机数赋值耗时:968毫秒
最大快速排序耗时:116641毫秒
第6次测试:
数据量:五千万
生成随机数赋值耗时:1903毫秒
最大快速排序耗时:288758毫秒
第7次测试:
数据量:一亿
生成随机数赋值耗时:3416毫秒
最大快速排序耗时:818865毫秒
完整代码:
#include <windows.h> // 用来计时 GetTickCount函数#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
#include <stdlib.h>
using namespace std;
{
int temp = *a;
*a = *b;
*b = temp;
}
int partition(int *p, int start, int end)
{
int x = p[end];
int i = start - 1;
for (int j = start; j < end; j++)
{
if (p[j] <= x)
{
i = i + 1;
if (i != j)
swap(p[i], p[j]);
}
}
swap(p[i + 1], p[end]);//此p[end] != x.因为它已经改变了。否则就会错误。
return i + 1;
}
void quicksort(int *A, int p, int r)
{
int q;
if (p < r)
{
q = partition(A, p, r);
quicksort(A, p, q - 1);
quicksort(A, q + 1, r);
}
}
int main()
{
//****************************************
DWORD start_time, end_time;
unsigned int total_1, total_2;
//**************主程序插入下***************
typedef unsigned int intdata;
//const unsigned int LENGTH = 1000000;
//int A[LENGTH];
DWORD LENGTH = 100000000;
int *A = NULL;
A = (int *)malloc(LENGTH * sizeof(int));
if (NULL == A)
{
free(A);
printf("内存分配失败,程序退出!\n");
system("pause");
return -1;
}
srand((unsigned)time(0));
start_time = GetTickCount();
for (intdata i = 0; i < LENGTH; i++)
{
*(A + i) = rand();
}
end_time = GetTickCount();
total_1 = end_time - start_time;
start_time = GetTickCount();
quicksort(A, 0, LENGTH - 1);
end_time = GetTickCount();
total_2 = end_time - start_time;
cout << endl << "生成随机数耗时:" << total_1 << "毫秒" << endl;
cout << "快速排序时间为:" << total_2 << "毫秒!" << endl;
//**************主程序插入上***************
free(A);
for (int i = 0; i < 10; i++)
{
cout << endl;
}
system("pause");
return 0;
}