find zhe median

题目描述:

Median Value
The Median is the "middle" of a sorted list of numbers.

How to Find the Median Value

To find the Median, place the numbers in value order and find the middle.
Example: find the Median of 12, 3 and 5
Put them in order:
3, 5, 12
The middle is 5, so the median is 5.
// 经典折半查找
int parition(int* pArr, int begin, int end)
{
	if (pArr == NULL || begin>end)
	{
		return -1;
	}

	//showData(pArr, 7);

	int temp = pArr[begin];

	while (end>begin)
	{  // ==
		while (end>begin && pArr[end] >= temp)
		{
			end--;
		}
		pArr[begin] = pArr[end];

		while (end>begin && pArr[begin] <= temp)
		{
			begin++;
		}
		pArr[end] = pArr[begin];
	}

	pArr[begin] = temp;

	cout << "mid=" << begin << ":" << temp<< endl;
	showData(pArr, 7);
	return begin;
}
/************************************************************************/
//   Find the Median
//   {4,5,1,6,2,7,3,8}
//  
//  分析:
//   中位数位置就是 i=len/2
//   
//   实现方式
//   1 递归 
//   
//  @input:mid=len/2 
// 
/************************************************************************/
int GetMedianNumber(int* pArr, int begin,int end,int mid)
{  

	int iPivit = parition(pArr, begin, end);
	//符合条件: 中位数位置就是 i=len/3
	if (iPivit == mid)
	{
		cout << "Find the Median..." << endl;
		return iPivit;
	}
	else if(mid> iPivit)
	{
		GetMedianNumber(pArr, iPivit + 1, end,mid);
	}
	else
	{
		GetMedianNumber(pArr, begin, iPivit-1, mid);
	}

}
//非递归方式
int GetMedianNumber(int* pArr, int begin, int end)
{   

	if (end%2 !=0)
	{
		return -1;
	}

	int mid = end/ 2; //中位数的位置
	int iPivit = 0;

	while (end >=begin)
	{
		iPivit = parition(pArr, begin, end);
	   //符合条件: 中位数位置就是 i=len/2
		if (iPivit == mid)
		{
			cout << "Find the Median..."<<endl;
			return iPivit;
		}
		else if (mid > iPivit)
		{

			begin = iPivit + 1;
			parition(pArr, begin, end);
		}
		else
		{
			end = iPivit - 1;
			parition(pArr, begin, end);
		}
	}
	return 0;
}

void test()
{  /**
	int aray[8] = { 2,4, 3, 6,3 ,2,5,5};
	//2 0010
	int num1 = 0;
	int num2 = 0;
	FindNumsAppearOnce(aray, 8, num1, num2);
	cout << "11=" << num1 << "22=" << num2 << endl;
	**/
	int aray[7] = { 0, 1, 2, 4, 6, 5, 3 };
	//int mid = GetMedianNumber(aray,0,6,3);
	int mid = GetMedianNumber(aray, 0, 6);
	cout << "mid============" << mid << ":" << aray[mid];
}

欢迎关注微信账号 进行讨论

时间: 2024-08-14 18:36:40

find zhe median的相关文章

LeetCode OJ 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 two sorted arrays. The overall run time complexity should be O(log (m+n)). Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1,

leetcode笔记:Find Median from Data Stream

一. 题目描述 Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value. Examples: [2,3,4] , the median is 3 [2,3], the median is (2 + 3) / 2 = 2.5 De

zoj Median (multiset)

Median Time Limit: 5 Seconds      Memory Limit: 65536 KB The median of m numbers is after sorting them in order, the middle one number of them ifm is even or the average number of the middle 2 numbers if m is odd. You have an empty number list at fir

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 two sorted arrays. The overall run time complexity should be O(log (m+n)). Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1,

LeetCode-刷题 Median of Two Sorted Arrays

之前一直是写C++的,想着开始学学用JAVA写Code会有什么不同,是否会更加简洁?于是开始学着用JAVA去刷LEETCODE 习题如下: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Example

Median Weight Bead_floyd

Description There are N beads which of the same shape and size, but with different weights. N is an odd number and the beads are labeled as 1, 2, ..., N. Your task is to find the bead whose weight is median (the ((N+1)/2)th among all beads). The foll

POJ 3784 Running Median

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1670   Accepted: 823 Description For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After each odd-indexed value is read, output the median (midd

[CareerCup] 18.9 Find and Maintain the Median Value 寻找和维护中位数

18.9 Numbers are randomly generated and passed to a method. Write a program to find and maintain the median value as new values are generated. LeetCode上的原题,请参见我之前的博客Find Median from Data Stream. 解法一: priority_queue<int> small; priority_queue<int,

【Lintcode】LRU Cache, Data Stream Median

主要是priority_queue的用法 一个是内置类型优先队列怎么设置小根堆(默认大根堆) 如果是自定义数据结构,有两种办法 1.定义这种数据结构的比较符号,就可以当成内置类型整 2.传进去一个重载()的类,当小于号用,默认还是大根堆,也许传进去的是个callable object都行的吧,我试了一下函数好像不行,不懂,不管了 LRU Cache class LRUCache{ public: // @param capacity, an integer int Time; typedef i