寻找水王(编程之美)

问题简要描述:在数据集中,如何很快地找到超过数据集半数的数。

example:

    input:  2        3        2        4        2        6        2        3        2        2

 processing:..........

    result:    2

 

分析一:利用排序算法,将数据进行排序,然后数据集中间的元素即为所求:

上述数据排完序后:2    2     2    2     2    2    3    3    4    6

中间的元素为:   2

时间复杂度仅与快速排序的时间复杂度有关。

代码实现:(排序算法选用快速排序法)

int findHalf_methodTwo(int *arr,int begin,int end){
	quickSort(arr,begin,end);
	return arr[(begin + end - 1)/2];
}

void quickSort(int *arr, int begin, int end){
	if(begin >= end) return;
	// 获取上一次排序枢轴位置(即就是 排序位置确定的元素位置) 
	int index = getIndex( arr, begin, end);
	
	//对枢轴左边的数据进行递归排序 
	quickSort( arr, begin, index - 1);  
	//对枢轴右边的数据进行递归排序 
	quickSort( arr, index + 1, end);
	
}
int getIndex(int *arr, int low, int high){
	
	int temp = arr[low];
	
	while(low < high){
		// 从最右端开始向左找到小于枢轴的元素 
		while(low < high && arr[high] >= temp)	high --; 
		arr[low] = arr[high];//将 high指针 指向的小于枢轴的元素交换到 low指针 所指的位置 
		
		// 从最右端开始想右找到大于枢轴的元素 
		while(low < high && arr[low] <= temp)	low ++;
		arr[high] = arr[low];//将 low指针 指向的大于枢轴的元素交换到 high指针 所指的位置 
	}
	
	arr[low] = temp;
	return low;
}

 

分析二:每次删除两个不同的数,剩下的数据集中,原数据集过半的元素依然是新数据集中过半的元素,利用这个规律。

    需要一个变量记录当前扫描的元素值;一个变量记录在扫描过程中,删除了两个不同的数后,剩下相同数的个数。

注意count的变化与数据的关系。

代码实现:

int findHalf_methodOne(int *arr,int N){
	int temp;// 候选代号
	 
	int count;// 相同数的个数 
	
	for(int i = count = 0;i < N; i ++){
		if(count == 0){
			temp = arr[i];
			count = 1; 
		}else{
			if(temp == arr[i])
				count ++;
			else
				count --; 
		} 
	}	
	return temp;
}

 

附:

      整个程序代码:

#include <iostream>
#include <cstdio>

using namespace std;
void quickSort(int *arr, int begin, int end);
int getIndex(int *arr, int low, int high);
void show(int *arr, int N){ 
	for(int i = 0;i < N;i ++)
		cout<<arr[i]<<"  ";
	cout<<endl;
} 

int findHalf_methodOne(int *arr,int N);
int findHalf_methodTwo(int *arr,int begin,int end);
int main(void){
	
	int arr[10] = {2,3,2,4,2,6,2,3,2,2};
	
	cout<<"the array:"<<endl;
	show(arr, 10);
	cout<<endl<<endl;
	cout<<"the number of taking up half of all:"<<endl;
	cout<<"method one:"<<findHalf_methodOne(arr,10)<<endl;
	cout<<"method two:"<<findHalf_methodTwo(arr,0,9)<<endl;
	return 0;
} 
int findHalf_methodOne(int *arr,int N){
	int temp;// 候选代号
	 
	int count;// 相同数的个数 
	
	for(int i = count = 0;i < N; i ++){
		if(count == 0){
			temp = arr[i];
			count = 1; 
		}else{
			if(temp == arr[i])
				count ++;
			else
				count --; 
		} 
	}	
	return temp;
}
int findHalf_methodTwo(int *arr,int begin,int end){
	quickSort(arr,begin,end);
	return arr[(begin + end - 1)/2];
}

void quickSort(int *arr, int begin, int end){
	if(begin >= end) return;
	// 获取上一次排序枢轴位置(即就是 排序位置确定的元素位置) 
	int index = getIndex( arr, begin, end);
	
	//对枢轴左边的数据进行递归排序 
	quickSort( arr, begin, index - 1);  
	//对枢轴右边的数据进行递归排序 
	quickSort( arr, index + 1, end);
	
}
int getIndex(int *arr, int low, int high){
	
	int temp = arr[low];
	
	while(low < high){
		// 从最右端开始向左找到小于枢轴的元素 
		while(low < high && arr[high] >= temp)	high --; 
		arr[low] = arr[high];//将 high指针 指向的小于枢轴的元素交换到 low指针 所指的位置 
		
		// 从最右端开始想右找到大于枢轴的元素 
		while(low < high && arr[low] <= temp)	low ++;
		arr[high] = arr[low];//将 low指针 指向的大于枢轴的元素交换到 high指针 所指的位置 
	}
	
	arr[low] = temp;
	return low;
}

运行结果:

 

### 寻找活跃的Java论坛帖子或讨论 为了找到关于Java相关的高产帖子或讨论,可以通过以下几个途径来获取有价值的信息: #### 1. Stack Overflow Stack Overflow 是一个非常受欢迎的技术问答网站,其中包含了大量有关Java的问题和解答。通过搜索特定的关键字,如“Java performance optimization”,可以发现很多高质量的话题讨论[^1]。 #### 2. Reddit 社区 Reddit 上有许多专门针对编程和技术交流的子版块(subreddit),比如 r/learnprogramming 和 r/java 。这些地方经常会有开发者分享自己的经验、遇到的问题以及解决方案。参与这样的社区可以帮助更好地理解当前流行的Java话题和发展趋势[^2]。 #### 3. 官方 Oracle Java 论坛 作为Java官方维护者Oracle所运营的一个重要资源中心,这里不仅有来自世界各地的专业人士发表的文章,还有机会直接向专家提问并获得反馈。这使得该平台成为一个不可多得的学习与交流场所。 #### 4. GitHub Discussions 许多开源项目都会在其GitHub页面设置Discussion板块供贡献者们沟通协作。对于那些想要深入了解某些具体库或者框架的人来说,这里是很好的起点。例如Spring Boot就有自己活跃的discussion area,能够帮助用户解决实际编码过程中碰到的各种难题。 ```python import webbrowser def open_forum_links(): forums = [ ("Stack Overflow", "https://stackoverflow.com/questions/tagged/java"), ("Reddit r/java", "https://www.reddit.com/r/java/"), ("Official Oracle Java Forum", "https://community.oracle.com/community/java"), ("Spring Boot Discussion on GitHub", "https://github.com/spring-projects/spring-boot/discussions") ] for name, url in forums: print(f"Opening {name}...") webbrowser.open(url) open_forum_links() ``` 上述Python脚本可以根据个人喜好修改链接列表中的内容,并自动打开浏览器标签页访问指定网址,方便快捷地浏览各个热门Java论坛。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值