[LintCode]Find Median of Unsorted Array O(n) quick sort

来源:互联网 发布:windows xp软件功能 编辑:程序博客网 时间:2024/06/14 09:30

Easy Median

21%
Accepted

Given a unsorted array with integers, find the median of it. 

A median is the middle number of the array after it is sorted. 

If there are even numbers in the array, return the N/2-th number after sorted.


Example

Given [4, 5, 1, 2, 3], return 3

Given [7, 9, 4, 5], return 5

Challenge

O(n) time.

记忆中做过这个题目,找不到了。这次做遇到一个bug,做了很久。这个做法作为一个quick sort 的standard。 已末尾元素为pivot

另外,我一律用的是绝对坐标纪录起始与目标 位置,最后要找的位置,也是坐标。比起来找一个size, 绝对坐标是不变的, 更方便, 不需要在右半边情况时候,重新计算size了。


0 0