Algorithmic Implementation series(7) Implementation of Randomized_Quick_Sort
来源:互联网 发布:淘宝仓库里的宝贝在哪 编辑:程序博客网 时间:2024/05/18 20:50
Compiler: gcc 4.7.3
C++ Standard: C++0X
OS: CentOS 6.3 x86
1 #include <iostream>
2 #include <random>
3
4 using namespace std;
5
6 //swap the ith element and the jth element in an array
7 void swap_elements(int ia[], const size_t i, const size_t j) {
8 if(i == j) { return; }
9 const int tmp = ia[i - 1];
10 ia[i - 1] = ia[j - 1];
11 ia[j - 1] = tmp;
12 }
13
14 const size_t partition(int ia[], const size_t p,const size_t r) {
15 //The value of variable x is the last element in array ia.
16 const int x = ia[r - 1];
17
18 //Variable i marks the position before the first element in
19 //array ia.
20 int i = p - 2;
21
22 //The for loop goes from the first element inclusive to one
23 //before the last element in array ia.
24 for(size_t j = p; j != r; ++j) {
25 if(ia[j - 1] <= x) {
26 ++i;
27 swap_elements(ia, i + 1, j);
28 }
29 }
30 swap_elements(ia, i + 2, r);
31 return i + 2;
32 }
33
34 void Quick_Sort(int ia[], const size_t p,const size_t r) {
35 if(p < r) {
36 const size_t q = partition(ia, p, r);
37 Quick_Sort(ia, p, q - 1);
38 Quick_Sort(ia, q + 1, r);
39 }
40 }
41
42
43 //Generating random value between p and r inclusive.
44 const size_t random(const size_t p, const size_t r) {
45 static default_random_engine e;
46 uniform_int_distribution<unsigned> u(p, r);
47 return u(e);
48 }
49
50 const size_t randomized_partition(int ia[], const size_t p,
51 const size_t r) {
52
53 const size_t i = random(p, r);
54 swap_elements(ia, i, r);
55
56 return partition(ia, p, r);
57 }
58
59 void Randomized_Quick_Sort(int ia[], const size_t p, const size_t r) {
60 if(p < r) {
61 const size_t q = randomized_partition(ia, p, r);
62 Randomized_Quick_Sort(ia, p, q - 1);
63 Randomized_Quick_Sort(ia, q + 1, r);
64 }
65 }
66
67 int main() {
68 int ia[] = {2, 32, 4, 5, -33, -55, 65, 90, -33, -42, 11, 105};
69
70 const size_t size = sizeof(ia)/sizeof(int);
71
72 for(size_t i = 0; i != size; ++i) {
73 cout << ia[i] << " ";
74 }
75 cout << endl;
76
77 Randomized_Quick_Sort(ia, 1, size);
78
79 for(size_t i = 0; i != size; ++i) {
80 cout << ia[i] << " ";
81 }
82 cout << endl;
83
84 return EXIT_SUCCESS;
85 }
- Algorithmic Implementation series(7) Implementation of Randomized_Quick_Sort
- Algorithmic Implementation series(3) Implementation of Merge_Sort
- Algorithmic Implementation series(5) Implementation of Heap_Sort
- Algorithmic Implementation series(6) Implementation of Quick_Sort
- Algorithmic Implementation series(8) Implementation of Counting_Sort
- Algorithmic Implementation series(1)——Implementation of Insertion_sort
- Algorithmic Implementation series(2)——Implementation of Selection_sort
- Algorithmic Implementation series(4) Implementation of Maximum Subarray problem(O(nlgn) and O(n))
- Implementation
- implementation
- Implementation of Polymorphism
- implementation of bitmap
- Implementation of Heap Sort
- The Implementation of ALG_DES_MAC8_ISO9797_1_M2_ALG3
- Implementation of CServer
- Implementation of CClient
- Implementation Of Choreography
- MATLAB Implementation of MonoSLAM
- 由于 ASP.NET 进程标识对全局程序集缓存没有读权限,因此未能执行请求。错误: 0x80131902
- Socket 代码例子
- 遥感图像处理流程 .
- Android系统联系人全特效实现(上),分组导航和挤压动画
- MSADO相关问题
- Algorithmic Implementation series(7) Implementation of Randomized_Quick_Sort
- 是时候跟 MongoDB 说再见了
- 一个问题P-hard以及NP-hard的解释
- Android系统联系人全特效实现(下),字母表快速滚动
- JQ $("#form1 :input" )与$("#form1input")有什么区别
- source insight3.5版的KEY?
- 领域建模分析
- 转载:Javascript获取html元素的XPath路径(选择器)
- 动态增删表格行(纯JS写法) 归档