利用快排思想,如果标志位大于k,则第k小的数字在左边,否则在右边。(程序是第k大的元素)

#include <iostream>
#include <vector>
using namespace std; //求向量中第k大的数字,从大到小进行排列 int q_sort(vector<int> &v, int low, int high)
{
int tmp = v[low]; while (low < high)
{
while (low<high && v[high] <= tmp)
--high;
v[low] = v[high]; while (low<high && v[low] >= tmp)
++low;
v[high] = v[low];
}
v[low] = tmp;
cout << low << endl;
for (auto &i : v)
cout << i << " ";
cout << endl;
return low;
}
int k_max(vector<int> &v, int low, int high,int k)
{
if (low >= high)
return v[low];
else
{
int mid = q_sort(v, low, high);
if (mid>k)
k_max(v, low, mid - ,k);
else if (mid < k)
k_max(v, mid + , high,k);
else
return v[mid];
} } int main()
{
vector<int> v = { , , , , , , , , , , };
int len = v.size();
int k_num=k_max(v, , len - , -); //此处为k-1,从大到小的排列,下标为k-1的数。
cout << k_num << endl;
return ;
}

第k小的元素的更多相关文章

  1. [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  2. [LeetCode] Kth Smallest Element in a BST 二叉搜索树中的第K小的元素

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

  3. 树状数组求第k小的元素

    int find_kth(int k) { int ans = 0,cnt = 0; for (int i = 20;i >= 0;i--) //这里的20适当的取值,与MAX_VAL有关,一般 ...

  4. 查找第k小的元素(O(n)递归解法)

    今天分享一个小技巧,虽然是小技巧但是还是很有价值的,曾经是微软的面试题.题目是这样的,一个无序的数组让你找出第k小的元素,我当时看到这道题的时候也像很多人一样都是按普通的思维,先排序在去第K个,但是当 ...

  5. 求第k小的元素

    用快排解决: 用快排,一趟排序后,根据基准值来缩小问题规模.基准值的下角标i 加1 表示了基准值在数组中第几小.如果k<i+1,那就在左半边找:如果k>i+1那就在右半边找.当基准值的下角 ...

  6. [Swift]LeetCode378. 有序矩阵中第K小的元素 | Kth Smallest Element in a Sorted Matrix

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  7. 230. 二叉搜索树中第K小的元素

    230. 二叉搜索树中第K小的元素 题意 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. ...

  8. leetcode 二叉搜索树中第K小的元素 python

          二叉搜索树中第K小的元素     给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元 ...

  9. [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素

    题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...

  10. 230. Kth Smallest Element in a BST 找到bst中的第k小的元素

    [抄题]: Given a binary search tree, write a function kthSmallest to find the kth smallest element in i ...

随机推荐

  1. jenkins+supervisor实现django项目的自动部署(ubuntu)

    没有安装supervisor的话首先安装supervisor, apt install upservisor 安装supervisor大概率会出现问题,不推荐,最好是用pip 安装, 注意python ...

  2. Spring-Boot配置文件web性能(服务器)配置项

    参数 介绍server.address 服务器应绑定到的网络地址server.compression.enabled = false 如果启用响应压缩server.compression.exclud ...

  3. ImportError: Imageio Pillow plugin requires Pillow, not PIL!

    问题: 解决: 首先确定是否安装了pillow,若没有输入以下命令安装: sudo pip3 install pillow 然后,再跑一边代码,看是否还会出现错误.若还有错误,更新pillow到最新版 ...

  4. 阿里云服务器ftp连接后21端口无法使用的问题

    今天在阿里云Centos上搭了一个ftp 服务,开启了20和21端口的权限.但是用工具和ftp命令登录,均超时. ftp命令登录成功后不能使用ls 命令,直接超时. 工具登录成功后 获取根目录失败,也 ...

  5. [转] GloVe公式推导

    from: https://pengfoo.com/post/machine-learning/2017-04-11 GloVe(Global Vectors for Word Representat ...

  6. FJUT-这还是一道数论题

    这还是一道数论题 TimeLimit:4000MS  MemoryLimit:128MB 64-bit integer IO format:%lld Special Judge   Problem D ...

  7. Linux中Too many open files

    1.ulimit –a open files一项就是默认的句柄数,最大为 65536 2.修改最大open files /etc/security/limits.conf文件中,加入以下配置: * s ...

  8. LoadRunner学习笔记(二)

    LoadRunner Controller简介: 当虚拟用户脚本开发完成后,使用controller将这个执行脚本的用户从单用户转化为多用户,从而,模拟大量用户操作, 进而形成负债(多用户单循环,多用 ...

  9. css @import 导入文件

    导入另一个css文件 例如 <style type="text/css"> @import url(css/main.css); //导入css目录下的main.css ...

  10. (转载)配置 Linux 操作系统的 JDK

    系统Ubuntu,下载jdk-9.0.1 1,切换到root   ,创建文件夹 xxxx@ubuntu:~$ sudo su root@ubuntu:~# mkdir /usr/java 2,找到下载 ...