In the Quicksort challenges, you sorted an entire array. Sometimes, you just need specific information about a list of numbers, and doing a full sort would be unnecessary. Can you figure out a way to use your partition code to find the median in an array?

Challenge 
Given a list of numbers, can you find the median?

Input Format 
There will be two lines of input:

  • n - the size of the array
  • ar - n numbers that makes up the array

Output Format 
Output one integer, the median.

Constraints 
1<= n <= 1000001 
-10000 <= x <= 10000 , x ∈ ar 
There will be an odd number of elements.


题解:久闻Partition函数可以用来找到乱序数组的中位数,今天终于实现了一把。

设置一个变量need为数组长度的一半,另一个变量hasFound为当前比找到的比中位数小的数的个数,当hasFound=need的时候,我们就找到了中位数。

在每次Partition后,看比pivot小的那部分数组有多少个元素,如果hasFound加上这部分元素正好等于need,那么pivot就是所求的中位数。如果hasFound加上这部分元素大于need,说明比pivot小的这部分数组需要继续划分,递归的调用Partition;如果hasFound加上这部分元素小于need,那么就把这部分元素个数加到hasFound上,并且继续划分比pivot大的那部分数组。

例如数组:3 1 4 5 2,找中位数的过程如下图所示:

代码如下:

 import java.util.*;

 public class Solution {
private static int need = 0;
private static int hasFound = 0; private static void swap(int[] ar,int i,int j){
int temp = ar[i];
ar[i]= ar[j];
ar[j]=temp;
}
private static int Partition(int[] ar,int start,int end){
int pivot = ar[end];
int i = start;
int j = start;
while(i < end){
if(ar[i] >= pivot)
i++;
else if(ar[i] < pivot){
swap(ar,i,j);
i++;
j++;
}
}
swap(ar, j, end);
if(hasFound+j-start+1==need)
return pivot;
else if(hasFound+j-start+1<need){
hasFound += j-start+1;
return Partition(ar, j+1, end);
}
else {
return Partition(ar, start, j-1);
} } public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
need = n%2==0?n/2:n/2+1;
int[] ar = new int[n]; for(int i = 0;i < n;i ++)
ar[i] = in.nextInt();
System.out.println(Partition(ar, 0, n-1)); }
}

以上代码就很容易扩展到找寻数组中第k小的数了,只要改变need变量的值即可。

【HackerRank】Find the Median(Partition找到数组中位数)的更多相关文章

  1. 【ShareCode】不错的技术文章 -- 如何使用异或(XOR)运算找到数组中缺失的数?

    如何使用异或(XOR)运算找到数组中缺失的数? 今天给大家分享一篇关于使用XOR(异或)运算找到数组中缺失的数的问题. 在一次Javascript面试中,有这么一个问题: 假设有一个由0到99(包含9 ...

  2. luogu P3031 [USACO11NOV]高于中位数Above the Median (树状数组优化dp)

    链接:https://www.luogu.org/problemnew/show/P3031 题面: 题目描述 Farmer John has lined up his N (1 <= N &l ...

  3. 【LeetCode】4.Median of Two Sorted Arrays 两个有序数组中位数

    题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

  4. POJ 3579:Median 差值的中位数

    Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4680   Accepted: 1452 Descriptio ...

  5. Java 找到数组中两个元素相加等于指定数的所有组合

    思路1:可以用hash表来存储数组中的元素,这样我们取得一个数后,去判断sum - val 在不在数组中,如果在数组中,则找到了一对二元组,它们的和为sum,该算法的缺点就是需要用到一个hash表,增 ...

  6. lintcode 中等题:partition array 数组划分

    题目 数组划分 给出一个整数数组nums和一个整数k.划分数组(即移动数组nums中的元素),使得: 所有小于k的元素移到左边 所有大于等于k的元素移到右边 返回数组划分的位置,即数组中第一个位置i, ...

  7. leetcode-1 Two Sum 找到数组中两数字和为指定和

     问题描写叙述:在一个数组(无序)中高速找出两个数字,使得两个数字之和等于一个给定的值.如果数组中肯定存在至少一组满足要求. <剑指Offer>P214(有序数组) <编程之美& ...

  8. URAL 1306 - Sequence Median 小内存求中位数

    [题意]给出n(1~250000)个数(int以内),求中位数 [题解]一开始直接sort,发现MLE,才发现内存限制1024k,那么就不能开int[250000]的数组了(4*250000=1,00 ...

  9. 求两个排序数组中位数 C++

    题目描述: 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nu ...

随机推荐

  1. python如何连接mysql数据库

    先花点时间来说说一个程序怎么和数据库进行交互1.和数据库建立连接2.执行sql语句,接收返回值3.关闭数据库连接使用MySQLdb也要遵循上面的几步.让我们一步步的进行. 1.MySQL数据库要用My ...

  2. sqoop-1.4.4安装配置

    环境:redhat6.5 hadoop2.4.1 感谢: http://wenku.baidu.com/view/a9083da8dd3383c4bb4cd274.html注释hbase检查 感谢: ...

  3. 《深入浅出WPF》笔记——事件篇

    如果对事件一点都不了解或者是模棱两可的话,建议先去看张子阳的委托与事件的文章(比较长,或许看完了,也忘记看这一篇了,没事,我会原谅你的)http://www.cnblogs.com/JimmyZhan ...

  4. OpenCV学习笔记二十:opencv_ts模块

    一,简介: OpenCV测试库,用于单元测试.

  5. react import改为绝对路径

    最近在使用react时发现路径用../../很不方便,特别是修改项目结构时,加减../都能改到吐血, 所有在网上找了半天webpack的配置,特此记录下 module.exports = (webpa ...

  6. std::condition_variable(2)复习

    #include <iostream> // std::cout #include <thread> // std::thread, std::this_thread::yie ...

  7. 【转】10 个MySQL数据库备份教程推荐

    10 个MySQL数据库备份教程推荐 MySQL是动态网站开发中最著名的开源数据库系统.如果你在网站中使用了MySQL,那么你应该定期备份你的数据以防止它丢失. 本文将介绍自动或手动备份MySQL数据 ...

  8. KVC && KVO 初见

    Look,这是一个很简单的要求,点击Add me,age +1. 想一想的话很简单的,设置一个属性Nsinteger age,点击button add me,直接加1在重新显示Lable就好啦,不过, ...

  9. 内置函数:max 用法

    内置函数——max Python max内置函数 max(iterable, *[, key, default]) max(arg1, arg2, *args[, key]) Return the l ...

  10. python多线程(三)

    同步锁 两个需要注意的点: 线程抢的是GIL锁,GIL锁相当于执行权限,拿到执行权限后才能拿到互斥锁Lock,其他线程也可以抢到GIL,但如果发现Lock仍然没有被释放则阻塞,即便是拿到执行权限GIL ...