Median
Posted codingEskimo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Median相关的知识,希望对你有一定的参考价值。
O(n) worst, O(nlogn) average. Use quickSort
This is quickSort
public class Solution { /** * @param nums: A list of integers. * @return: An integer denotes the middle number of the array. */ public int median(int[] nums) { // write your code here if (nums == null || nums.length == 0) { return 0; } quickSort(nums, 0, nums.length - 1); return nums[(nums.length - 1) / 2]; } private void quickSort(int[] nums, int start, int end) { if (start >= end) { return; } int left = start; int right = end; int pivot = nums[start + (end - start) / 2]; while (left <= right) { while (left <= right && nums[left] < pivot) { left++; } while (left <= right && nums[right] > pivot) { right--; } if (left <= right) { int tmp = nums[left]; nums[left] = nums[right]; nums[right] = tmp; left++; right--; } } quickSort(nums, start, right); quickSort(nums, left, end); } }
以上是关于Median的主要内容,如果未能解决你的问题,请参考以下文章
4. Median of Two Sorted Arrays
题目1004:Median(qsort函数自定义cmp函数)
CVPR论文《100+ Times Faster Weighted Median Filter (WMF)》的实现和解析(附源代码)。