C++_快速排序(纯C版本)
//比较大小
static int compare_int(const void *int1,const void *int2)
{
if(*(const int*)int1>*(const int*)int2)
{
return 1;
}
else if(*(const int*)int1<*(const int*)int2)
{
return -1;
}
else
{
return 0;
}
}
//随机生成三个数,找出中间数的直接插入排序
int issort(void *data, int size,int esize,int (*compare)(const void *key1,const void *key2))
{
char *a = (char*)data; void *key=NULL; int i=0,j=0; if((key=(char *)malloc(esize))==NULL)
{
return -1;
}
else
{
memset(key, 0, esize);
for(j=1;j<size;j++)
{
memcpy(key, &a[j*esize],esize);
i=j-1; while(i>=0&&compare(&a[i*esize],key)>0){
memcpy(&a[(i+1)*esize],&a[i*esize],esize);
i--;
cout<<i<<endl;
} memcpy(&a[(i+1)*esize],key,esize); }
free(key);
return 0;
} }
//与当前pval相比,小的放左边,大的放右边,pval即r[1]
static int partition(void *data, int esize, int i, int k, int (*compare)(const void *key1,const void *key2))
{
//cout<<"enter partition"<<endl;
char *a=(char*)data; void *pval=NULL, *temp=NULL; int r[3]={0,0,0}; //allocate storage for the partition value and swap
if((pval = malloc(esize))==NULL)
{
return -1;
}
if((temp = malloc(esize))==NULL)
{
free(pval);
return -1;
} //Use the median-of-tree method to find the partition value
r[0] = (rand() % (k-i+1))+i;
r[1] = (rand() % (k-i+1))+i;
r[2] = (rand() % (k-i+1))+i; issort(r, 3, sizeof(int), compare_int); memcpy(pval, &a[r[1]*esize],esize); //cout<<"*pval= "<<(pval)<<endl;
//create two partitions around the partition value i--;
k++;
// cout<<"this is i: "<<i<<endl;
// cout<<"this is k: "<<k<<endl; while(1)
{ do{
// cout<<"doing k"<<endl;
k--;
}while(compare(&a[k*esize],pval)>0); do{
// cout<<"doing i"<<endl;
i++;
}while(compare(&a[i*esize],pval)<0); if(i >= k)
{
// cout<<"doing break"<<endl;
break;
}
else
{
// cout<<"swaping......................."<<endl;
memcpy(temp, &a[i*esize], esize);
memcpy(&a[i*esize], &a[k*esize], esize);
memcpy(&a[k*esize], temp, esize);
}
} free(pval);
free(temp);
// cout<<"left partition"<<endl; return k;
}
//排序调用
int qksort(void *data, int size, int esize, int i, int k, int (*compare)(const void *key1,const void *key2))
{
int j;
while(i<k)
{
if((j=partition(data,esize,i,k,compare))<0)
{
//cout<<"return partition"<<endl;
return -1;
} if(qksort(data,size,esize,i,j,compare)<0)
{
//cout<<"return qksort"<<endl;
return -1;
} else
{
i=j+1;
//cout<<"i value: "<<i<<endl;
//cout<<"j value: "<<j<<endl;
}
} return 0;
}
C++_快速排序(纯C版本)的更多相关文章
- 纯MATLAB版本 SIFT代码
先贴几个链接: http://blog.csdn.net/abcjennifer/article/details/7639681 Rachel-Zhang的 http://blog.csdn.net ...
- Python笔记_第五篇_Python数据分析基础教程_相关安装和版本查看
1. IDE说明: 所有的案例用Anacoda中的Jupiter工具进行交互式讲解. 2. 版本和安装: NumPy从如下网站安装:http://sourceforge.net/projects/nu ...
- 瓦片切图工具gdal2tiles.py改写为纯c++版本
gdal2tiles.py是GDAL库中用于生成TMS瓦片的python代码,支持谷歌墨卡托EPSG:3857与经纬度EPSG:4326两种瓦片,输出png格式图像. gdal2tiles.py Mo ...
- bug_ _小心android-support-v4.jar版本混乱造成的NoClassDefFoundError
当你的项目出现以下红色提示的时候,要小心了, 因为很可能因为这个错误而导致解释不通的异常出现. Found 2 versions of android-support-v4.jar in the de ...
- 快速排序(js版本)
快速排序的时间复杂度为:O(n*log2n),相比较其他O(n2)的排序算法,还是比较有优势的.原文参考在此处,因为本人对原文的一小段代码有点不理解,所以进行了小的修改. 1.基本思想:在数组的第一个 ...
- js实现的文章输入检查与测速。(纯js版本)
朋友又提出一些需求.希望不要jquery .于是修改成js版本. <!DOCTYPE html> <html> <head> <meta charset=&q ...
- php排序介绍_冒泡排序_选择排序法_插入排序法_快速排序法
这里我们介绍一些常用的排序方法,排序是一个程序员的基本功,所谓排序就是对一组数据,按照某个顺序排列的过程. 充效率看 冒泡排序法<选择排序法<插入排序法 排序分两大类: 内部排序法 交换式 ...
- Struts2-Ajax整合之纯JavaScript版本
1.Ajax的作用:能够在不重新加载页面的情况下,用异步的方式与后台服务器进行数据交互 2.Struts2-Json的jar包(包含阿里巴巴自己的jar包) commons-beanutils-1.7 ...
- linux 平台实现 web 服务器的自动化发布 (纯shell 版本,存在ssh 不能自动退出问题,待解决)
转至:https://www.cnblogs.com/vmsky/p/13824172.html 背景说明 1.集团OA系统上线,web App 部署在6台服务器中,因项目初期,每次更新都需要进行大量 ...
随机推荐
- 分享一个Web弹框类
using System; using System.Text; namespace Core { /// <summary> /// MessageBox 的摘要说明. /// < ...
- 线段相交 POJ 2653
// 线段相交 POJ 2653 // 思路:数据比较水,据说n^2也可以过 // 我是每次枚举线段,和最上面的线段比较 // O(n*m) // #include <bits/stdc++.h ...
- 直接调用系统Camera
关键思路: 初始化 组件: 创建并启动拍照intent: 使用回调函数onActivityResult()处理图像. 关键代码: 初始化 组件: takePicBtn = (Button) findV ...
- C# 颜色转换
十六进制颜色与Color对象的互相转换[C#] C#十六进制颜色与Color对象的互相转换 把十六进制颜色转化为color对象ColorTranslator.FromHtml("#FF0 ...
- [AngularJS学习笔记] 基础学习01
2016-06-06开始学习AngularJS AngularJS是会extend HTML的 ng-directives 先学习了四个 ng-app:定义AngularJS Application的 ...
- HDU-4738 Caocao's Bridges 边联通分量
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题意:在有重边的无向图中,求权值最小的桥. 注意trick就好了,ans为0时输出1,总要有一个 ...
- DelphiXE8新建AVD
相关资料: 1.http://jingyan.baidu.com/article/ea24bc398576b3da62b33107.html
- HDU 5802 Windows 10 (贪心+dfs)
Windows 10 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5802 Description Long long ago, there was ...
- BestCoder Round #71 (div.2) (hdu 5621)
KK's Point Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- HDU 3265 Posters (线段树+扫描线)(面积并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3265 给你n个中间被挖空了一个矩形的中空矩形,让你求他们的面积并. 其实一个中空矩形可以分成4个小的矩 ...