参考:https://minenet.me/2016/08/24/quickSort.html

快速排序

利用分治法可将快速排序的分为三步:

  1. 在数据集之中,选择一个元素作为”基准”。
  2. 所有小于”基准”的元素,都移到”基准”的左边;所有大于”基准”的元素,都移到”基准”的右边。这个操作称为分区 (partition) 操作,分区操作结束后,基准元素所处的位置就是最终排序后它的位置。
  3. 对”基准”左边和右边的两个子集,不断重复第一步和第二步,直到所有子集只剩下一个元素为止。
def quickSort(arr, left, right):
if left >= right:
return
low, high = left, right
key = arr[low]
while low < high:
while low < high and arr[high] >= key:
high -= 1
arr[low], arr[high] = arr[high], arr[low]
while low < high and arr[low] <= key:
low += 1
arr[low], arr[high] = arr[high], arr[low]
quickSort(arr, left, low-1)
quickSort(arr, low+1, right) if __name__ == '__main__':
arr = [4, 5, 2, 1, 5, 8, 3, 2, 6]
quickSort(arr, 0, len(arr)-1)
print arr

Java

import java.util.Scanner;

public class Main{
private void swap(int[] arr, int i, int j) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
} public void quickSort(int[] arr, int left, int right) {
if (left >= right) {
return;
}
int key = arr[left], low = left, high = right;
while (low < high) {
while (low < high && arr[high] > key) {
high--;
}
swap(arr, low, high);
while (low < high && arr[low] < key) {
low++;
}
swap(arr, low, high);
}
quickSort(arr, left, low-1);
quickSort(arr, low+1, right);
} public static void main(String[] args) {
Main mainObj = new Main();
Scanner in = new Scanner(System.in);
while(in.hasNext()) {
int n = Integer.parseInt(in.next());
int[] a = new int[n];
for (int i=0; i<n; i++) {
a[i] = Integer.parseInt(in.next());
}
mainObj.quickSort(a, 0, a.length-1);
System.out.println("Sorted a : ");
for (int i=0; i<a.length; i++) {
System.out.print(a[i] + " ");
}
}
in.close();
}
}

寻找第K大(小)的数

寻找第K小的数:

  1. 快速排序中确定基准值后,将数组分为两部分,基准元素前面的一定小于基准元素。后面的大于基准元素。
  2. 如果基准元素前面的元素个数大于K个,则第K小的数一定在基准元素的前面,没必要进行后面的排序。否则就在后面,没必要前面的排序
  3. 直到这个基准元素的位置刚好是K-1
# 寻找第K小的数
def kNum(arr, k):
if not arr or k <= 0 or k > len(arr):
return None
index = partition(arr, 0, len(arr)-1)
while k-1 != index:
if k-1 < index:
index = partition(arr, 0, index-1)
else:
index = partition(arr, index+1, len(arr)-1)
return arr[k-1] def partition(arr, low, high):
key = arr[low]
while low < high:
while low < high and arr[high] >= key:
high -= 1
arr[low], arr[high] = arr[high], arr[low]
while low < high and arr[low] <= key:
low += 1
arr[low], arr[high] = arr[high], arr[low]
return low if __name__ == '__main__':
arr = [4, 5, 2, 1, 5, 8, 3, 2, 6]
print kNum(arr,9)

快速排序 && 寻找第K大(小)的数的更多相关文章

  1. 一题多解(五) —— topK(数组中第 k 大/小的数)

    根据对称性,第 k 大和第 k 小,在实现上,是一致的,我们就以第 k 小为例,进行说明: 法 1 直接排序(sort(A, A+N)),当使用一般时间复杂度的排序算法时,其时间复杂度为 O(N2) ...

  2. 寻找第K大的数(快速排序的应用)

    有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数.给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在.测试样例:[1,3,5,2,2],5, ...

  3. 寻找第K大的数

    在一堆数据中查找到第k个大的值. 名称是:设计一组N个数,确定其中第k个最大值,这是一个选择问题,解决这个问题的方法很多. 所谓“第(前)k大数问题”指的是在长度为n(n>=k)的乱序数组中S找 ...

  4. 数组,寻找第K大的数

    时间复杂度 O(n) def partition(data,left,right): if (len(data)<=0 or left<0 or right>=len(data)): ...

  5. Coursera Algorithms week3 快速排序 练习测验: Selection in two sorted arrays(从两个有序数组中寻找第K大元素)

    题目原文 Selection in two sorted arrays. Given two sorted arrays a[] and b[], of sizes n1 and n2, respec ...

  6. [SOJ]寻找第k大数字(numberk)

    Description 经过长时间的筹备工作,在Jourk,Ronny,Plipala,阿长,阿沈等人的努力下,DM实验室建立起自己的系列网站,其中包括三个大板块:DMOJ首页.DMOJ论坛.DMOJ ...

  7. 快速选择算法/Select 寻找第k大的数

    参考算法导论9.3节的内容和这位大神的博客:http://blog.csdn.net/v_JULY_v上对这一节内容代码的实现进行了学习 尝试实现了以查找中位数为前提的select算法. 算法功能:可 ...

  8. 分治法寻找第k大的数

    利用快速排序的思想·去做 #include<iostream>using namespace std;int FindKthMax(int*list, int left, int righ ...

  9. 寻找第K大 网易2016实习研发工程师编程题

    有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5,2,2] ...

随机推荐

  1. 2019 ACM/ICPC 全国邀请赛(西安)J And And And (树DP+贡献计算)

    Then n - 1n−1 lines follow. ii-th line contains two integers f_{a_i}(1 \le f_{a_i} < i)fai​​(1≤fa ...

  2. Netty学习第三章 Linux网络编程使用的I/O模型

    一.同步阻塞IO:blocking IO(BIO) 1.过程分析: 当进程进行系统调用时,内核就会去准备数据,当数据准备好后就复制数据到内核缓冲器,复制完成后将数据拷贝到用户进程内存,整个过程都是阻塞 ...

  3. docker常用命令及操作

    1).镜像操作 操作 命令 说明 检索 docker search 关 键 字 eg:docker search redis 我们经常去docker hub上检索镜像的详细信息,如镜像的TAG. 拉取 ...

  4. python基本数据类型常用方法

    python基本数据类型 1.整型 1.1 int 1.2 bit_lenght # 当前数字的二进制位数,至少用n位表示 r = age.bit_length() >>> a = ...

  5. unittest----skip(跳过用例)

    我们在执行测试用例时,有时有些用例时不需要执行的,这时就需要用到unittest给我们提供的跳过用例的方法 @unittest.skip(reason):强制跳过,不需要判断条件.reason是跳过原 ...

  6. Linux kswapd0 进程CPU占用过高

    图便宜买了个1核1G虚拟机,启动两个jar后cpu飙升直接卡死,查看cpu及内存占用 发现kswapd0进程cpu占用一直居高不下,于是查询资料,总结如下. swap分区的作用是当物理内存不足时,会将 ...

  7. LeetCode--078--子集(python)

    给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: nums = [1,2,3]输出:[ [3],  [1],  [2],  ...

  8. Comet OJ - Contest #6 D. 另一道树题 并查集 + 思维 + 计数

    Code: #include <cstdio> #include <algorithm> #include <cstring> #include <vecto ...

  9. R 保存图片——tiff

    tiff(filename = "c:\\aaa.tiff", res = 800, pointsize = 2) plot(1:100) dev.off() tiff(file= ...

  10. php开发IDE选择

    优先选择Netbeans,理由如下:: 1.ZendStudio有的方便特性Netbeans也提供,如:ctrl+f5也支持ctrl+shift+r的文件选择功能,[git | svn]团队代码管理. ...