分治思想的应用:C++实现快速排序和随机化的快速排序
分治思想的应用:C++实现快速排序和随机化的快速排序
1. 快速排序时冒泡排序的升级版
都知道冒泡排序需要从0-n-1轮n-1次两两比较。并且进行多次两两交换才能得到最后的排列结果。需要
for(i from 0 to n-1)
for(j from i+1 to n-1)
compare(a[i], a[j]) and switch(a[i], a[j])
算法复杂度为O(n power 2)
快速排序通过对冒泡的改进,j将i和j从两边移到中间,使得第一次循环结果为以选取的中心值为中心。如果需要升序,
left = 0, right = n-1, pivot = a[left]
while(i< =j)
{
if(a[j]<pivot) a[i]= a[i] , i++小的往左边移动
if(a[i]>pibot) a[j] = a[j], j++, 大的往右移动
else i++
else j++
}
quicksort(a,left,i-1);
quicksort(a,i+1,right);
快速排序则添加入循环迭代思想, 不断的拆分这个数组。
选取第一个数为中心元素
使用递归
函数头:quicksort(inta[],intleft,intright)
quicksort(a,left,i-1);
quicksort(a,i+1,right);
2. 如何随机排列数列?
用这个 random_shuffle() 可以得到一个随即排序:
先用数组构造一个 vector 容器,
然后给 random_shuffle() 算法传递容易的首尾迭代器即可
参考地址: http://www.cnblogs.com/afarmer/archive/2011/05/01/2033715.html
如何从一个数列中随机得到一个数?
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
srand((unsigned)time(NULL));
for(int i = 0; i < 10;i++ )
cout << rand() << '\t';
cout << endl;
return 0;
}
产生一定范围随机数的通用表示公式
要取得[a,b)的随机整数,使用(rand() % (b-a))+ a;
要取得[a,b]的随机整数,使用(rand() % (b-a+1))+ a
3.下面实现随机化的快速排序
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- using namespace std;
- bool static Inc(const int& x, const int& y)
- {
- return x<y;
- }
- bool static Dec(const int& x, const int& y)
- {
- return x>y;
- }
- //template<bool cmp(const int&, const int&)>
- void quicksort(int a[],int left,int right)
- { int i,j;
- if(left<right)
- { i=left;j=right;
- int temp=a[left]; //store the pivot number, it can be anyone of the array a[]
- do
- { while(Inc(a[j],temp) && i<j)//Inc for des
- j--; //j一直减直到可以交换
- if(i<j)
- { a[i]=a[j];//blank postion i = a[j]
- i++;
- }
- while(Inc(temp, a[i]) && i<j)//i一直加直到a[i]大于j
- i++;
- if(i<j)
- { a[j]=a[i];
- j--;
- }
- }while(i<j);// do while can make sure the i=j will be executed
- a[i]=temp;//i =j, only one positon left, so its the last temp
- quicksort(a,left,i-1);
- quicksort(a,i+1,right);
- }
- }
- int main()
- {
- cout<<"input n"<<endl;
- int n;
- scanf("%d",&n);
- int a[100];
- cout<<"input "<< n<<" numbers"<<endl;
- for(int i = 0; i< n; i++)
- {
- scanf("%d", a+i);
- }
- int left = 0, right = n-1;
- srand(time(NULL));
- left = rand()%n;
- cout<<"random pivot is "<<a[left]<<endl;
- //swap with the first one
- int tmp;
- tmp = a[0];
- a[0]= a[left];
- a[left] = tmp;
- quicksort(a,0,n-1);
- cout<<"the result is:"<<endl;
- for(int i = 0; i< n; i++)
- {
- cout<<a[i]<<" ";
- }
- system("pause");
- return 0;
- }
- 或者<pre name="code" class="cpp"> if(left<right)
- { i=left;j=right;
- int pivot = a[left]; //store the pivot number, it can be anyone of the array a[]
- while(i <= j)
- {
- if(Inc(a[j],pivot) && i<j)
- {
- a[i] = a[j]; //大的往左边移动
- i++;
- if(Inc(pivot, a[i]) && i<j)
- {
- a[j] = a[i];//小的往右边移动
- j--;
- }
- else
- {
- i++;
- }
- }
- else
- {
- j--;
- }
- }
- a[i]=pivot;//i =j, only one positon left, so its the last temp
- quicksort(a,left,i-1);
- quicksort(a,i+1,right);
- }
4. 快速排序的变体
注意最后是交换a[i] 和A[r] 不是i+1
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- using namespace std;
- bool static Inc(const int& x, const int& y)
- {
- return x<y;
- }
- bool static Dec(const int& x, const int& y)
- {
- return x>y;
- }
- //template<bool cmp(const int&, const int&)>
- void quicksort(int a[], int left, int right)
- { int i,j;
- if(left < right)
- {
- i=left;//j=size -1;
- int pivot = a[left];
- int tmp;
- //int temp=a[i]; //store the pivot number, it can be anyone of the array a[]
- for(j = i+1; j <= right; j++)
- {
- if(a[j]<pivot)
- {
- if(j> ++i)
- {
- tmp = a[j];
- a[j] = a[i];//sawp i+1 and j
- a[i]=tmp;
- }
- }
- }
- tmp = a[i];
- a[i] = a[left];
- a[left] = tmp;
- quicksort(a,left,i-1);
- quicksort(a,i+1,right);
- }
- }
- int main()
- {
- cout<<"input n"<<endl;
- int n;
- scanf("%d",&n);
- int a[100];
- cout<<"input "<< n<<" numbers"<<endl;
- for(int i = 0; i< n; i++)
- {
- scanf("%d", a+i);
- }
- int left = 0, right = n-1;
- srand(time(NULL));
- left = rand()%n;
- cout<<"random pivot is "<<a[left]<<endl;
- //swap with the first one
- int tmp;
- tmp = a[0];
- a[0]= a[left];
- a[left] = tmp;
- quicksort(a,0,n-1);
- cout<<"the result is:"<<endl;
- for(int i = 0; i< n; i++)
- {
- cout<<a[i]<<" ";
- }
- system("pause");
- return 0;
- }
分治思想的应用:C++实现快速排序和随机化的快速排序的更多相关文章
- 分治思想--快速排序解决TopK问题
----前言 最近一直研究算法,上个星期刷leetcode遇到从两个数组中找TopK问题,因此写下此篇,在一个数组中如何利用快速排序解决TopK问题. 先理清一个逻辑解决TopK问题→快速排序→递 ...
- Big Data(一)分治思想
按照课程安排,接下来半年,我将会去上一个为期半年的大数据课程.第一课是马士兵老师机构的周老师所讲,这里单纯记录讲课的内容. 问题1: 我有一万个元素(比如数字或单词)需要存储? 如果查找某一个元素,最 ...
- 快速排序 Java实现的快速排序
快速排序 Java实现的快速排序: package xc; import java.util.Arrays; import java.util.Random; /** * * @author dax ...
- 快速排序改进——3区快速排序(3-way quicksort)
1.快速排序缺陷 快速排序面对重复的元素时的处理方法是,把它放在了左部分数组或右部分数组,下次进行分区时,还需检测它.如果需要排序的数组含有大量重复元素,则这个问题会造成性能浪费. 解决方法:新增一个 ...
- 无序数组中用 快速排序的分治思想 寻找第k大元素
#include <stdio.h> int *ga; int galen; void print_a(){ ; i < galen; i++){ printf("%d & ...
- bzoj3672/luogu2305 购票 (运用点分治思想的树上cdq分治+斜率优化dp)
我们都做过一道题(?)货币兑换,是用cdq分治来解决不单调的斜率优化 现在它放到了树上.. 总之先写下来dp方程,$f[i]=min\{f[j]+(dis[i]-dis[j])*p[i]+q[i]\} ...
- 分治思想 特别常用 Codeforces Beta Round #80 (Div. 1 Only) D
D. Time to Raid Cowavans time limit per test 4 seconds memory limit per test 70 megabytes input stan ...
- 快速排序基本思想,递归写法,python和java编写快速排序
1.基本思想 快速排序有很多种编写方法,递归和分递归,分而治之法属于非递归,比递归简单多了.在这不使用代码演示.下面我们来探讨一下快速排序的递归写法思想吧. 设要排序的数组是A[0]……A[N-1], ...
- 分治思想求解X的M次幂方
package main import ( "fmt" ) //递归形式分治求解 func power(x, m int) int { { } else { y := power( ...
随机推荐
- python之命令行解析工具argparse
以前写python的时候都会自己在文件开头写一个usgae函数,用来加上各种注释,给用这个脚本的人提供帮助文档. 今天才知道原来python已经有一个自带的命令行解析工具argparse,用了一下,效 ...
- CCNA 课程 六
路由协议概述: 通过一种路由协议学习到了多个去往同一目标网络的路由,metric小的放入路由表中.metric一样,同时放进路由表. 通过多种路由协议学习到了去往同一目标网络的路由,路由协议管理距离小 ...
- Mybatis入门2-动态代理实现CRUD
MyBatis动态代理生成DAO的步骤: 1) 编写数据管理的接口XxxMapper 2) 编写该接口对应的Mapper.xml a) namespace必须与Mapper接口全名一致 b) stat ...
- ZooKeeper-安装和运行
ZooKeeper安装和运行 1. 下载安装包 zookeeper-3.4.9.tar.gz 2. 解压 tar -zxvf zookeeper-3.4.9.tar.gz ZooKeeper提供了几个 ...
- Apache Phoenix基本操作-2
1. 如何映射一个Phoenix的表到一个Hbase的表? 你可以通过Create table/create view DDL语句在一个已经存在的hbase表上创建一个Phoenix表或者视图.对于C ...
- 【转载】Java类加载原理解析
Java类加载原理解析 原文出处:http://www.blogjava.net/zhuxing/archive/2008/08/08/220841.html 1 基本信息 摘要: 每个j ...
- HDU 2419 Boring Game(并查集+map)
感觉做得有点复杂了,但是AC了还是...爽... 题意:给你n个点每个点有一个价值,接下来有m条边,然后是q个操作,每个操作有三种情况: F X K:寻找与X点直接或间接相连的不小于价值K的最小价值, ...
- JavaScript -- 练习,Dom 获取节点
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 智课雅思词汇---十八、前缀peri是什么意思
智课雅思词汇---十八.前缀peri是什么意思 一.总结 一句话总结:前缀:peri- 表示“周围, 靠近” 词根:-peri- [词根含义]:试验,尝试 [词根来源]:英语experience, e ...
- 任务1-1 了解Web基本概念
1.认识网页 从图中可以看到,网页主要由文字.图像和超链接等元素构成.当然,除了这些元素,网页中还可以包含音频.视频及Flash. 为了快速了解网页是如何形成的,接下来查看一下网页的源代码