快速排序的三种写法的效率比较
2015-05-12 21:51
357 查看
分类: C++ C语言 数据结构 2012-09-02 20:26 459人阅读 评论(0) 收藏 举报
数据结构算法null
最近在复习排序和查找算法的时候,回想算法课程和数据结构课程上面各种写法,总结一下,顺便比较了一下它们之间的效率,
另《外数据结构》书本上阐述,如果比较的枢纽值不是第一个或者最后一个而是 a[low] ,a[high],a[(high+low)/2] 的中间值,效率还会好很多,但是我在实现的过程中,总是搬移到了错误的位置,没有实现
[cpp] view plaincopy
#include <iostream.h>
#include <windows.h>
#include <ctime>
#include <math.h>
#include <cstdlib>
#include <stdio.h>
void QuickSort( int a[],int low,int high); //比较经典的一种,将往中间扫面时找到的满足条件的交换
void QuickSort3( int a[],int low,int high); //枢纽暂存,每次找到一个比枢纽大或者小的,就放到上一次搬离的位置,最后把枢纽放回到low处(低地址必须小于高地址,不能等于)
void QuickSort2( int a[],int low,int high); //只有一个while,指针都从头部开始,,快指针每次都向后移动,遇到一个比枢纽大的就和慢指针交换值
//产生随机数
int randArr(int * pint , int size);
int size = 0;
int main()
{
int a[] ={4,-2,3,19,0,-4,99,7,2,-5,0,-11,2,2,56,-8,0,17,200,5,1,3,5,4,6,-5,29,-1,8};
int b[] ={ 34,51,38,65,119,76,16,27};
//int b[] ={76, 119 };
int tsize=150000;
int *pint = new int[tsize];
int *pint2 = new int[tsize];
int *pint3 = new int[tsize];
int id = 5;
if(! randArr(pint,tsize) )
return 0;
// memcpy(pint ,a,sizeof(int) * tsize);
memcpy(pint2,pint,sizeof(int) * tsize);
memcpy(pint3,pint,sizeof(int) * tsize);
size = tsize;
printf("=====before====\n");
for(id = 0 ; id< 10;id++)
{
printf("%3d ", pint[id]);
}printf("=====before====\n");
int start = GetTickCount();
QuickSort(pint,0,size -1);
cout<<"time QuickSort used="<< GetTickCount() - start << endl;
for(id = 0 ; id< 10;id++)
{
printf("%3d ", pint[id]);
}printf("======QuickSort===\n\n");
start = GetTickCount();
QuickSort2(pint2,0,size -1);
cout<<"time QuickSort2 used="<< GetTickCount() - start << endl;
for(id = 0 ; id< 10;id++)
{
printf("%3d ", pint2[id]);
}printf("======QuickSort2===\n\n");
QuickSort3(pint3,0,size -1);
cout<<"time QuickSort3 used="<< GetTickCount() - start << endl;
for(id = 0 ; id< tsize;id++)
{
if(pint[id] != pint2[id])
{
printf("Confliction!! %d",id);
break;
}
}printf("======QuickSort3===\n\n");
return 0;
}
void QuickSort(int a[],int l,int h)
{
int po;
int high = h , low = l;
if(low < high )
{
po = a[l];
low++;
while(1)
{
while(low <= high && a[high] >= po) high--;
while(low <= high && a[low] <= po) low++;
if(low < high)
{
a[low] ^= a[high];
a[high] ^= a[low];
a[low] ^= a[high];
low++;
high--;
}
else
break;
}
a[l] = a[high];
a[high] = po;
QuickSort(a,l,high-1);
QuickSort(a,high+1,h);
}
}
void QuickSort2(int a[],int l ,int h)
{
int po;
int high = h,low = l;
if( l < h )
{
po = a[l];
while( low < high)
{
while( low < high && a[high] >= po ) high--;
a[low] = a[high];
while( low < high && a[low] <= po ) low++;
a[high] = a[low];
}
a[low] = po;
QuickSort2(a,l,low-1);
QuickSort2(a,low+1,h);
}
}
void QuickSort3(int a[],int l ,int h )
{
int high = l+1, low = l+1;
int po = a[l];
if( l < h)
{
while( high <= h)
{
if( a[high] < po) //找到慢指针
{
if(high != low)
{
a[low] ^=a[high];
a[high] ^=a[low];
a[low] ^=a[high];
}
low++;
}
high++;
}
if(low-1 != l)
{
a[low-1] ^=a[l];
a[l] ^=a[low-1];
a[low-1] ^=a[l];
}
low--;
QuickSort3(a,l,low - 1);
QuickSort3(a,low+1 ,h);
}
}
int randArr(int * pint , int size)
{
int i = 0;
if(!pint) return 0;
srand((unsigned int)time(NULL));
for( i = 0 ; i<size; i++)
{
pint[i] = rand() % 100 ;
if( rand() % 10 == 1 && rand() % 10 == 1 && rand() % 10 == 1 &&pint[i] % 10 == 2)
pint[i] *= -1;
}
return 1;
}
数据结构算法null
最近在复习排序和查找算法的时候,回想算法课程和数据结构课程上面各种写法,总结一下,顺便比较了一下它们之间的效率,
另《外数据结构》书本上阐述,如果比较的枢纽值不是第一个或者最后一个而是 a[low] ,a[high],a[(high+low)/2] 的中间值,效率还会好很多,但是我在实现的过程中,总是搬移到了错误的位置,没有实现
[cpp] view plaincopy
#include <iostream.h>
#include <windows.h>
#include <ctime>
#include <math.h>
#include <cstdlib>
#include <stdio.h>
void QuickSort( int a[],int low,int high); //比较经典的一种,将往中间扫面时找到的满足条件的交换
void QuickSort3( int a[],int low,int high); //枢纽暂存,每次找到一个比枢纽大或者小的,就放到上一次搬离的位置,最后把枢纽放回到low处(低地址必须小于高地址,不能等于)
void QuickSort2( int a[],int low,int high); //只有一个while,指针都从头部开始,,快指针每次都向后移动,遇到一个比枢纽大的就和慢指针交换值
//产生随机数
int randArr(int * pint , int size);
int size = 0;
int main()
{
int a[] ={4,-2,3,19,0,-4,99,7,2,-5,0,-11,2,2,56,-8,0,17,200,5,1,3,5,4,6,-5,29,-1,8};
int b[] ={ 34,51,38,65,119,76,16,27};
//int b[] ={76, 119 };
int tsize=150000;
int *pint = new int[tsize];
int *pint2 = new int[tsize];
int *pint3 = new int[tsize];
int id = 5;
if(! randArr(pint,tsize) )
return 0;
// memcpy(pint ,a,sizeof(int) * tsize);
memcpy(pint2,pint,sizeof(int) * tsize);
memcpy(pint3,pint,sizeof(int) * tsize);
size = tsize;
printf("=====before====\n");
for(id = 0 ; id< 10;id++)
{
printf("%3d ", pint[id]);
}printf("=====before====\n");
int start = GetTickCount();
QuickSort(pint,0,size -1);
cout<<"time QuickSort used="<< GetTickCount() - start << endl;
for(id = 0 ; id< 10;id++)
{
printf("%3d ", pint[id]);
}printf("======QuickSort===\n\n");
start = GetTickCount();
QuickSort2(pint2,0,size -1);
cout<<"time QuickSort2 used="<< GetTickCount() - start << endl;
for(id = 0 ; id< 10;id++)
{
printf("%3d ", pint2[id]);
}printf("======QuickSort2===\n\n");
QuickSort3(pint3,0,size -1);
cout<<"time QuickSort3 used="<< GetTickCount() - start << endl;
for(id = 0 ; id< tsize;id++)
{
if(pint[id] != pint2[id])
{
printf("Confliction!! %d",id);
break;
}
}printf("======QuickSort3===\n\n");
return 0;
}
void QuickSort(int a[],int l,int h)
{
int po;
int high = h , low = l;
if(low < high )
{
po = a[l];
low++;
while(1)
{
while(low <= high && a[high] >= po) high--;
while(low <= high && a[low] <= po) low++;
if(low < high)
{
a[low] ^= a[high];
a[high] ^= a[low];
a[low] ^= a[high];
low++;
high--;
}
else
break;
}
a[l] = a[high];
a[high] = po;
QuickSort(a,l,high-1);
QuickSort(a,high+1,h);
}
}
void QuickSort2(int a[],int l ,int h)
{
int po;
int high = h,low = l;
if( l < h )
{
po = a[l];
while( low < high)
{
while( low < high && a[high] >= po ) high--;
a[low] = a[high];
while( low < high && a[low] <= po ) low++;
a[high] = a[low];
}
a[low] = po;
QuickSort2(a,l,low-1);
QuickSort2(a,low+1,h);
}
}
void QuickSort3(int a[],int l ,int h )
{
int high = l+1, low = l+1;
int po = a[l];
if( l < h)
{
while( high <= h)
{
if( a[high] < po) //找到慢指针
{
if(high != low)
{
a[low] ^=a[high];
a[high] ^=a[low];
a[low] ^=a[high];
}
low++;
}
high++;
}
if(low-1 != l)
{
a[low-1] ^=a[l];
a[l] ^=a[low-1];
a[low-1] ^=a[l];
}
low--;
QuickSort3(a,l,low - 1);
QuickSort3(a,low+1 ,h);
}
}
int randArr(int * pint , int size)
{
int i = 0;
if(!pint) return 0;
srand((unsigned int)time(NULL));
for( i = 0 ; i<size; i++)
{
pint[i] = rand() % 100 ;
if( rand() % 10 == 1 && rand() % 10 == 1 && rand() % 10 == 1 &&pint[i] % 10 == 2)
pint[i] *= -1;
}
return 1;
}
相关文章推荐
- 快速排序的三种写法的效率比较
- 快速排序、归并排序、堆排序三种算法性能比较
- 比较快速排序,冒泡排序,双向冒泡排序的执行效率
- 简单选择,冒泡,插入,快速排序之效率比较
- 【更新】排序算法比较:插入排序,冒泡排序,归并排序,堆排序,快速排序,计数排序,基数排序,桶排序
- 比较两种数组随机排序方法的效率 JavaScript版
- 快速排序的三种实现及两种优化
- 快速排序php写法
- j2me三种图片切割方法之效率比较
- 冒泡排序,快速排序,堆排序比较(转自:http://linpder.blog.163.com/blog/static/487641020082124532971/)
- 三种批量删除PLSQL写法效率的比对
- 数据结构 学习笔记(十一):排序(下):快速 / 表 / 桶 / 基数 排序,排序算法的比较
- 快速排序partition的写法
- java三种排序比较
- 快速排序的优化及其与qsort的比较
- 三种排序算法(归并排序、快速排序,堆排序)
- 快速排序的正逆向比较
- 三种图象处理的效率比较,用指针法最快
- Ext.Button点击事件的三种写法及比较
- 三种xml写法比较