//header in use
#include <stdio.h>
#include <tchar.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
using namespace std;
#define MAX 20
int _tmain(int argc, _TCHAR* argv[])
{
int sortbox[MAX];
long start, end;
int Z = 0;
void QuickSort(int *, int, int);
srand(time(NULL)); //using rand() and time() to build ramdom number vector
start = clock();
while(Z != 1000){
for(int i = 0; i != MAX; i++){
sortbox[i] = rand() % 1000;
// cout<<sortbox[i]<<' ';
}
QuickSort(sortbox, 0, MAX-1);
Z++;
}
end = clock() - start;
cout << endl;
for(int i = 0; i != MAX; i++)
{
cout<<sortbox[i]<<' ';
}
cout << endl << end;
return 0;
}
void QuickSort(int *box,int l, int r) //
{
int s = 0;
int partition(int *, int, int );
if(l<r){
s = partition(box, l, r);
QuickSort(box, l, s-1);
QuickSort(box, s+1, r);
}
return;
}
int partition(int *box, int l, int r)
{
int p = box[l];
int i = l;
int j = r + 1;
void swap(int &, int &); //referance! Use it carefully
////////////////////////////////////////////////////////////////
while(j>=i){
do i++; while(i <= r && box[i] < p);
do j--; while(j >=l && box[j] > p);
swap(box[i], box[j]);
}
swap(box[i], box[j]);
swap(box[l], box[j]);
/*///////////////////////////////////////////////////////////////
cout << endl;
for(int i = 0; i != MAX; i++)
{
cout<<box[i]<<' ';
}
*/
return j;
}
void swap(int &a, int &b)
{
int tmp = a;
a = b;
b= tmp;
return;
}
自己写的快速排序,随机生成数列循环一千次并记录时间,时间很不均匀,差别很大。本来希望实现选择前中后的中位数来进行但是出了问题,就没有写入了。
分享到:
相关推荐
Use QuickSort algorithm to sort the array that has n elements that are constructed by the random() function. Requirements: The template should be used for all kinds of data type, such as: integer, ...
quick sort 快速排序的代码实现~
数据结构,排序算法,快速排序算法的C语言实现, quick sort C qsort.c an c implementation of quick sort
python编写 快速排序 Quick Sort
各种数据结构、算法及实用的C#源代码.C#,单向链表(Simply Linked List)快速排序(Quick Sort)算法与源代码.单向链表(单链表)是链表的一种,其特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部...
C#,双向链表(Doubly Linked List)快速排序(Quick Sort)算法与源代码。双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始...
python 一行代码实现的快速排序 quick sort
快速排序(Quick Sort)
基于python的排序算法-快速排序Quick Sort
快速排序(Quick Sort)源码及运行示例
快速排序(Quick Sort)作者原版论文,快速排序的作者C.A.R Hoare 发表的原著论文。
算法分析与设计教学课件:Chapter 7 Quick Sort.pptx
最差情况下达到O(NlogN)
非常通俗易懂的merge归并,和quick快速排序算法讲解。并含代码。
搜索算法源码:合并排序,快速排序,shell排序,插入排序,堆排序,冒泡排序,桶排序
快速排序算法图解,用图片的形式,清晰解构算法原理和实现过程,值得推荐。
实现快速排序,Implement Quicksort and answer the following questions. (1) How many comparisons will Quicksort do on a list of n elements that all have the same value? (2) What are the maximum and ...
实现确定性和随机化的两种快速排序算法,用实验数据分析算法的时间效率和稳定性。 (对相同的输入,随机算法均要运行多次,并用曲线图和表格的形式比较实验结果)。