剑指offer——面试题11:快速排序
#include"iostream"
#include"random"
using namespace std; /*
void Swap(int &a,int &b)
{
int tmp;
tmp=a;
a=b;
b=tmp;
}
*/
//官方给的partition函数
int Partition1(int *data,int start,int end)
{
int index=start+(rand()%(end-start+));
swap(data[index],data[end]);
int small=start-;
for(index=start;index<end;index++)
{
if(data[index]<data[end])
{
small++;
if(index!=small)
swap(data[index],data[small]);
}
}
small++;
swap(data[small],data[end]);
return small;
} //自定义Partition函数
int Partition2(int *data,int start,int end)
{
int value=data[start];
while(start<end)
{
while(start<end)
{
if(data[end]>value)
end--;
else
break;
}
if(start<end)
data[start++]=data[end];
while(start<end)
{
if(data[start]<=value)
start++;
else
break;
}
if(start<end)
data[end--]=data[start];
}
data[start]=value;
return start;
} void QuickSort1(int *data,int start,int end)
{
if(start==end) return; int mid=Partition1(data,start,end);
if(mid>start)
QuickSort1(data,start,mid-);
if(mid<end)
QuickSort1(data,mid+,end);
} void QuickSort2(int *data,int start,int end)
{
if(start==end) return; int mid=Partition2(data,start,end);
if(mid>start)
QuickSort2(data,start,mid-);
if(mid<end)
QuickSort2(data,mid+,end);
}
QuickSort.h
#include"QuickSort.h" void Test1()
{
int data[]={,,,,,,,,,};
cout<<"use QuickSort1:";
QuickSort1(data,,);
for(int i=;i<;i++)
cout<<data[i]<<" ";
cout<<endl;
} void Test2()
{
int data[]={,,,,,,,,,};
cout<<"use QuickSort2:";
QuickSort2(data,,);
for(int i=;i<;i++)
cout<<data[i]<<" ";
cout<<endl;
}
int main()
{
Test1();
Test2();
}
TestQuickSort.cpp
剑指offer——面试题11:快速排序的更多相关文章
- 剑指offer——面试题11:旋转数组的最小数字
#include"iostream" using namespace std; int GetMinNumber(int *data,int len) { ,right=len-, ...
- 剑指Offer面试题11(Java版):数值的整数次方
题目:实现函数double Power(double base,int exponent),求base的exponent次方.不得使用库函数,同一时候不须要考虑大数问题 1.自以为非常easy的解法: ...
- 剑指Offer:面试题11——数值的整数次方(java实现)
题目描述: 实现函数double Power(double base, int exponent),求base的exponent次方,不得使用库函数,同时不需要考虑大数问题 思路:本题的重点考察内容是 ...
- 数值的整数次方(剑指offer面试题11)
实现函数 double Power(double base, int exponent),即乘方运算. 考虑问题 exponet < 0 , 可以转化为 1.0 / Power(base, -1 ...
- 剑指Offer——笔试题+知识点总结
剑指Offer--笔试题+知识点总结 情景回顾 时间:2016.9.23 12:00-14:00 19:00-21:00 地点:山东省网络环境智能计算技术重点实验室 事件:笔试 注意事项:要有大局观, ...
- C++版 - 剑指offer 面试题23:从上往下打印二叉树(二叉树的层次遍历BFS) 题解
剑指offer 面试题23:从上往下打印二叉树 参与人数:4853 时间限制:1秒 空间限制:32768K 提交网址: http://www.nowcoder.com/practice/7fe2 ...
- C++版 - 剑指offer 面试题24:二叉搜索树BST的后序遍历序列(的判断) 题解
剑指offer 面试题24:二叉搜索树的后序遍历序列(的判断) 题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true.否则返回false.假设输入的数组的任意两个 ...
- C++版 - 剑指Offer 面试题45:圆圈中最后剩下的数字(约瑟夫环问题,ZOJ 1088:System Overload类似)题解
剑指Offer 面试题45:圆圈中最后剩下的数字(约瑟夫环问题) 原书题目:0, 1, - , n-1 这n个数字排成一个圈圈,从数字0开始每次从圆圏里删除第m个数字.求出这个圈圈里剩下的最后一个数字 ...
- 剑指Offer:面试题15——链表中倒数第k个结点(java实现)
问题描述 输入一个链表,输出该链表中倒数第k个结点.(尾结点是倒数第一个) 结点定义如下: public class ListNode { int val; ListNode next = null; ...
随机推荐
- Yii2中ACF和RBAC
ACF ( Access Control Filter) ACF ( Access Control Filter)官网的解释就是一个可以在模型或控制器执行行为过滤器,当有用户请求时,ACF将检查acc ...
- Struts中ActionContext和ServletActionContext的比较
一.ActionContext在Struts2开发中除了将请求参数自动设置到Action的字段中,往往也需要在Action里直接获取请求(Request)或会话(Session)的一些信息,甚至需要直 ...
- js $.inArray
var arr = [ "xml", "html", "css", "js" ]; $.inArray(" ...
- 微信AES-128-CBC加密解密
[TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { var key = "cheaye ...
- Unity本地持久化类Playerprefs使用详解
一.PlayerPrefs是什么? PlayerPrefs是Unity3d提供了一个用于数据本地持久化保存与读取的类.工作原理十分简单,就是以key-value的形式将数据保存在本地,然后在代码中可以 ...
- linking against a dylib which is not safe for use in application extensions
完成到这里可能会出现警告 linking against a dylib which is not safe for use in application extensions 解决办法:
- day08.4-samba共享网盘服务
1. 安装软件:yum install samba -y 2. 新建共享目录物理路径:mkdir /zizaijiapu 修改配置文件:vim /etc/samba/smb.con ...
- C# 小球100米自由落下
//一球从N 米高自由落下,每次落地后反跳回原高度的一般:再录下,求它在第十次落地时,共经过多少米?第10次反弹多高 static string ballDsitance(float height1, ...
- fiddler 代理调试本地手机页面
https://www.cnblogs.com/zichi/p/4944581.html
- SpringBoot+MyBatis+MySQL读写分离(实例)
1. 引言 读写分离要做的事情就是对于一条SQL该选择哪个数据库去执行,至于谁来做选择数据库这件事儿,无非两个,要么中间件帮我们做,要么程序自己做.因此,一般来讲,读写分离有两种实现方式.第一种是 ...