Select算法(最坏复杂度O(n))
#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <string>
#include <string.h>
using namespace std; const int nMax = ;
int A[nMax+];
int B[nMax+];//用来每次5分法后保存要比较的值在A中的下标
int AIndex[nMax+]; //用来保存A的初始化下标 //通过插入排序获取中位数下标
int InsertSort(int A[], int B[], int start, int end)
{
if (start == end)
{
return B[start];
} for (int i = start+; i <= end; ++i)
{
int num = A[B[i]];
int j = i-;
for ( ; j >= start; --j)
{
if (num < A[B[j]])
{
A[B[j + ]] = A[B[j]];
}
else
{
break;
}
}
A[B[j + ]] = num;
} return B[(start + end)/];
} //获取中位数的中位数的下标
int GetMidMid(int A[], int AIndex[], int k, int n)
{
if (k == n)
{
return AIndex[n];
} int len_s = n - k + ;
//筛选出n/5份的中位数
int mod = len_s % ;
int len = len_s / + (mod != );
for (int i = , j = k; i<= len && j <= n-mod; ++i, j+=)
{
B[i] = InsertSort(A, AIndex,j, j+);
}
if (mod != )
{
B[len] = InsertSort(A, AIndex, n - mod + , n);
}
return GetMidMid(A, B, , len);
} //原址排序
int Partition(int A[], int p, int n)
{
int pivot = A[n];
int j = p - ;
for (int i = p; i <= n - ; ++i)
{
if (A[i] <= pivot)
{
j++;
swap(A[j], A[i]);
}
} swap(A[j + ], A[n]);
return j + ;
} int Select(int A[], int k, int n, int i)
{
if (k == n)
{
return A[n];
} int midValueIndex = GetMidMid(A, AIndex, k, n); //将该中位数作为主元(pivot element)
//使用一次原址重排
int pivot = A[midValueIndex];
swap(A[midValueIndex], A[n]);
int mid = Partition(A, k, n); int t = mid - k + ;
if (i == t)
{
return A[mid];
}
else if (i < t)
{
return Select(A, k, mid-, i);
}
else
{
return Select(A, mid+, n, i-t);
}
}
int main(int argc, char** argv)
{
int n = ;
for (int i = ; i <= n; ++i)
{
A[i] = i;
AIndex[i] = i;
} //for (int i = 1; i <= n; ++i)
//{
// cout << A[i] << " ";
//}
//cout << endl; int equalNum = ;
for (int i = ; i <= n; ++i)
{
//随机排列A数组
for (int i = ; i <= n; ++i)
{
int j = i + rand() % nMax;
//swap(A[i], A[j]);
A[i] = j;
} int ans1 = Select(A, , n, i);
sort(A + , A + n + );
int ans2 = A[i]; if (ans1 == ans2)
{
equalNum++;
}
}
cout << n << " " << equalNum << endl;
return ;
}
Select算法(最坏复杂度O(n))的更多相关文章
- 重拾算法之复杂度分析(大O表示法)
.katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...
- 数据结构与算法 java描述 第一章 算法及其复杂度
目录 数据结构与算法 java描述 笔记 第一章 算法及其复杂度 算法的定义 算法性能的分析与评价 问题规模.运行时间及时间复杂度 渐进复杂度 大 O 记号 大Ω记号 Θ记号 空间复杂度 算法复杂度及 ...
- 【Unsolved】线性时间选择算法的复杂度证明
线性时间选择算法中,最坏情况仍然可以保持O(n). 原因是通过对中位数的中位数的寻找,保证每次分组后,任意一组包含元素的数量不会大于某个值. 普通的Partition最坏情况下,每次只能排除一个元素, ...
- 浅谈压缩感知(二十七):压缩感知重构算法之稀疏度自适应匹配追踪(SAMP)
主要内容: SAMP的算法流程 SAMP的MATLAB实现 一维信号的实验与结果 稀疏度K与重构成功概率关系的实验与结果 一.SAMP的算法流程 前面所述大部分OMP及其前改算法都需要已知信号的稀疏度 ...
- 算法最坏,平均和最佳情况(Worst, Average and Best Cases)-------geeksforgeeks 翻译
最坏,平均和最佳运行时间(Worst, Average and Best Cases) 在上一篇文章中,我们讨论到了渐进分析可以解决分析算法的问题,那么在这一篇中,我们用线性搜索来举例说明一下如何用渐 ...
- C++:主要几种排序算法及其复杂度
常见排序算法稳定性和复杂度分析快速简记以及转载 分类: 算法 2012-02-07 22:18 399人阅读 评论(1) 收藏 举报 算法mergeshell http://blogold.chin ...
- 给定一个实数数组,按序排列(从小到大),从数组从找出若干个数,使得这若干个数的和与M最为接近,描述一个算法,并给出算法的复杂度。
有N个正实数(注意是实数,大小升序排列) x1 , x2 ... xN,另有一个实数M. 需要选出若干个x,使这几个x的和与 M 最接近. 请描述实现算法,并指出算法复杂度. #define M 8 ...
- C++ STL标准容器插入删除算法的复杂度
1 vector内部实现: 数组 // 就是没有固定大小的数组,vector直接翻译是向量的意思支持操作:begin(), //取首个元素,返回一个iteratorend(), //取末尾(最后一个元 ...
- Fast Newman-FN算法以及模块度定义介绍
一.社区的定义 Newman第一次提出模块度定义就是在2004年发表的这篇文章“fast algorithm for community structure in networks”,第一次用量化的公 ...
随机推荐
- UITableViewCell的高度与UILabel自适应
UITableViewCell内部只放了一个UILabel,Cell的高度随着UILabel内容的高度变化而变化,可重写UITableView的委托方法动态调整高度,还要设置UILabel.numbe ...
- Eclipse之NDK编译-- Type 'jint' could not be resolved, and JNIEnv, jclass错误解决办法
最近在研究面部识别美白相关的功能.使用的是opencv,就去研究了.今天正好有空就把安装了ndk,安装完成之后就试图去编译demo程序,hellow-jni c代码,一开始编辑就报错了3个错误信息: ...
- HDU 3342:Legal or Not(拓扑排序)
Legal or Not Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- ElasticSearch(六):IK分词器的安装与使用IK分词器创建索引
之前我们创建索引,查询数据,都是使用的默认的分词器,分词效果不太理想,会把text的字段分成一个一个汉字,然后搜索的时候也会把搜索的句子进行分词,所以这里就需要更加智能的分词器IK分词器了. 1. i ...
- long long 与 int
- 《DSP using MATLAB》Problem 4.5
1. 2. 3. 5.不会
- spring boot 好文
配置: https://www.jianshu.com/p/3af2a8721d86 : Spring Boot启动报错:Whitelabel Error Page 分页: https://bbs.c ...
- MySQL--修改MySQL账号密码
##使用mysqladmin进行修改 mysqladmin -u username -h hostname password 'new password'; ##使用set命令进行修改 SET PAS ...
- vulcanjs 包类型
npm 添加在pacakge.json 文件中的 meteor core 包 由meteor 框架提供的 meteor remote 包 从包服务器加载的,使用username:package 格式组 ...
- Socket-Vs-WebSocket-TestTool
项目地址 : https://github.com/kelin-xycs/Socket-Vs-WebSocket-TestTool Socket-Vs-WebSocket-TestTool 一个用 ...