C++ 如何有效地从数组中找到第二个最大值?
通过只遍历一次整数数组,是否可以从整数数组中找到第二个最大数 例如,我有一个由五个整数组成的数组,我想从中找到第二个最大数。以下是我在采访中的一次尝试:C++ 如何有效地从数组中找到第二个最大值?,c++,arrays,algorithm,C++,Arrays,Algorithm,通过只遍历一次整数数组,是否可以从整数数组中找到第二个最大数 例如,我有一个由五个整数组成的数组,我想从中找到第二个最大数。以下是我在采访中的一次尝试: #define MIN -1 int main() { int max=MIN,second_max=MIN; int arr[6]={0,1,2,3,4,5}; for(int i=0;i<5;i++){ cout<<"::"<<arr[i]; } for(
#define MIN -1
int main()
{
int max=MIN,second_max=MIN;
int arr[6]={0,1,2,3,4,5};
for(int i=0;i<5;i++){
cout<<"::"<<arr[i];
}
for(int i=0;i<5;i++){
if(arr[i]>max){
second_max=max;
max=arr[i];
}
}
cout<<endl<<"Second Max:"<<second_max;
int i;
cin>>i;
return 0;
}
#定义最小值-1
int main()
{
int max=MIN,second_max=MIN;
int-arr[6]={0,1,2,3,4,5};
对于(int i=0;i最简单的解决方案是使用。您需要进行第二次测试:
for(int i=0;i<5;i++){
if(arr[i]>max){
second_max=max;
max=arr[i];
}
else if (arr[i] > second_max && arr[i] != max){
second_max = arr[i];
}
}
for(inti=0;imax){
第二个最大值=最大值;
max=arr[i];
}
否则如果(arr[i]>second_max&&arr[i]!=max){
第二个最大值=arr[i];
}
}
给您:
std::pair<int, int> GetTwoBiggestNumbers(const std::vector<int>& array)
{
std::pair<int, int> biggest;
biggest.first = std::max(array[0], array[1]); // Biggest of the first two.
biggest.second = std::min(array[0], array[1]); // Smallest of the first two.
// Continue with the third.
for(std::vector<int>::const_iterator it = array.begin() + 2;
it != array.end();
++it)
{
if(*it > biggest.first)
{
biggest.second = biggest.first;
biggest.first = *it;
}
else if(*it > biggest.second)
{
biggest.second = *it;
}
}
return biggest;
}
std::pair GetTwoBiggestNumbers(const std::vector&array)
{
std::对最大;
max.first=std::max(数组[0],数组[1]);//前两个中最大的一个。
max.second=std::min(数组[0],数组[1]);//前两个中最小的一个。
//继续第三步。
对于(std::vector::const_迭代器it=array.begin()+2;
it!=array.end();
++(it)
{
如果(*it>最大值。第一个)
{
最大。第二=最大。第一;
最大的。第一个=*它;
}
else if(*it>最大。秒)
{
最大。秒=*它;
}
}
回报最大;
}
将max
和second\u max
初始化为-1
有缺陷。如果数组具有类似{-2,-3,-4}
的值,该怎么办
您可以做的是取数组的前2个元素(假设数组至少有2个元素),比较它们,将较小的元素分配给second\u max
,将较大的元素分配给max
:
if(arr[0] > arr[1]) {
second_max = arr[1];
max = arr[0];
} else {
second_max = arr[0];
max = arr[1];
}
然后从第三个元素开始比较,并根据需要更新max
和/或second\u max
:
for(int i = 2; i < arr_len; i++){
// use >= n not just > as max and second_max can hav same value. Ex:{1,2,3,3}
if(arr[i] >= max){
second_max=max;
max=arr[i];
}
else if(arr[i] > second_max){
second_max=arr[i];
}
}
for(int i=2;i=n而不仅仅是>,因为max和second_max可以有相同的值。例如:{1,2,3,3}
如果(arr[i]>=max){
第二个最大值=最大值;
max=arr[i];
}
否则如果(arr[i]>第二个最大值){
第二个最大值=arr[i];
}
}
您的原始代码没有问题,您只需初始化max和second\u max变量。使用数组中的前两个元素。就是使用这两个元素的方法。该链接中提供了伪代码,因此我将仅解释整个算法:
QuickSelect for kth largest number:
Select a pivot element
Split array around pivot
If (k < new pivot index)
perform quickselect on left hand sub array
else if (k > new pivot index)
perform quickselect on right hand sub array (make sure to offset k by size of lefthand array + 1)
else
return pivot
快速选择第k个最大数字:
选择轴元素
围绕枢轴拆分阵列
If(k<新枢轴指数)
在左侧子阵列上执行quickselect
else if(k>新轴索引)
在右侧子数组上执行quickselect(确保按左侧数组的大小+1偏移k)
其他的
回程枢轴
这显然是基于旧的快速排序算法
遵循此算法,每次始终选择元素零作为轴心:
select 4th largest number:
1) array = {1, 3, 2, 7, 11, 0, -4}
partition with 1 as pivot
{0, -4, _1_, 3, 2, 7, 11}
4 > 2 (new pivot index) so...
2) Select 1st (4 - 3) largest number from right sub array
array = {3, 2, 7, 11}
partition with 3 as pivot
{2, _3_, 7, 11}
1 < 2 (new pivot index) so...
3) select 1st largest number from left sub array
array = {2}
4) Done, 4th largest number is 2
选择第四大数字:
1) 数组={1,3,2,7,11,0,-4}
以1为轴心的分区
{0, -4, _1_, 3, 2, 7, 11}
4>2(新轴心指数),因此。。。
2) 从右子数组中选择第一个(4-3)最大的数字
数组={3,2,7,11}
以3为轴心的分区
{2, _3_, 7, 11}
1<2(新枢轴指数),因此。。。
3) 从左子数组中选择第一个最大的数字
数组={2}
4) 完成后,第四大数字是2
这将使您的数组保持未定义的顺序,如果有问题,则由您决定。步骤1。决定前两个数字。
第二步。循环浏览剩余的数字。
第三步。保持最新最大值和第二最大值。
第四步。更新第二个最大值时,请注意您没有使最大值和第二个最大值相等。
测试排序输入(升序和降序)、随机输入、具有重复项的输入,效果良好
#include <iostream>
#define MAX 50
int GetSecondMaximum(int* data, unsigned int size)
{
int max, secmax;
// Decide on first two numbers
if (data[0] > data[1])
{
max = data[0];
secmax = data[1];
}
else
{
secmax = data[0];
max = data[1];
}
// Loop through remaining numbers
for (unsigned int i = 2; i < size; ++i)
{
if (data[i] > max)
{
secmax = max;
max = data[i];
}
else if (data[i] > secmax && data[i] != max/*removes duplicate problem*/)
secmax = data[i];
}
return secmax;
}
int main()
{
int data[MAX];
// Fill with random integers
for (unsigned int i = 0; i < MAX; ++i)
{
data[i] = rand() % MAX;
std::cout << "[" << data[i] << "] "; // Display input
}
std::cout << std::endl << std::endl;
// Find second maximum
int nSecondMax = GetSecondMaximum(data, MAX);
// Display output
std::cout << "Second Maximum = " << nSecondMax << std::endl;
// Wait for user input
std::cin.get();
return 0;
}
#包括
#定义最大值50
int GetSecondMaximum(int*数据,无符号整数大小)
{
int max,secmax;
//决定前两个数字
if(数据[0]>数据[1])
{
最大值=数据[0];
secmax=数据[1];
}
其他的
{
secmax=数据[0];
max=数据[1];
}
//循环浏览剩余的数字
for(无符号整数i=2;i最大值)
{
secmax=max;
max=数据[i];
}
else if(data[i]>secmax&&data[i]!=max/*删除重复问题*)
secmax=数据[i];
}
返回secmax;
}
int main()
{
整数数据[MAX];
//用随机整数填充
用于(无符号整数i=0;i std::cout解决此问题的另一种方法是使用元素之间的比较。例如
a[10] = {1,2,3,4,5,6,7,8,9,10}
比较1,2,说max=2,第二个max=1
现在比较3和4,并将其中最大的与最大的进行比较
if element > max
second max = max
element = max
else if element > second max
second max = element
这样做的好处是,您可以在两次比较中消除两个数字
如果您在理解上有任何问题,请告诉我。//将前两个不同的数字设置为最大值和第二个最大值
// Set the first two different numbers as the maximum and second maximum numbers
int max = array[0];
int i = 1;
//n is the amount of numbers
while (array[i] == max && i < n) i++;
int sec_max = array[i];
if( max < sec_max ) {
tmp = sec_max;
sec_max = max;
max = tmp;
}
//find the second maximum number
for( ; i < n; ++i ) {
if( array[i] > max ) {
sec_max = max;
max = array[i];
} else if( array[i] > sec_max && array[i] != max ) {
sec_max = array[i];
}
}
printf("The second maximum number is %d\n", sec_max);
int max=数组[0];
int i=1;
//n是数字的数量
而(数组[i]==max&&imax){
sec_max=max;
max=数组[i];
}else if(数组[i]>sec_max&&array[i]!=max){
sec_max=数组[i];
}
}
printf(“第二个最大值为%d\n”,秒最大值);
检查此解决方案
max1 = a[0];
max2 = a[1];
for (i = 1; i < n; i++)
{
if (max1 < a[i])
{
max2 = max1;
max1 = a[i];
}
if (max2 == max1) max2 = a[i + 1];
if (max2 == a[n])
{
printf("All numbers are the same no second max.\n");
return 0;
}
if (max2 < a[i] && max1 != a[i]) max2 = a[i];
}
max1=a[0];
max2=a[1];
对于(i=1;i
以下是一些可能有效的方法
public static int secondLargest(int[] a){
int max=0;
int secondMax=0;
for(int i=0;i<a.length;i++){
if(a[i]<max){
if(a[i]>secondMax){
secondMax=a[i];
}
continue;
}
if(a[i]>max){
secondMax=max;
max=a[i];
}
}
return secondMax;
}
公共静态int第二大(int
#include <algorithm>
#include <iostream>
int main()
{
int arr[6]={0,1,2,3,4,5};
std::make_heap(arr, arr+6);
std::cout << "First Max: " << arr[0] << '\n';
std::cout << "Second Max: " << std::max(arr[1], arr[2]) << '\n';
return 0;
}
int max,secondMax;
max=secondMax=array[0];
for(int i=0;i<array.length;i++)
{ if(array[i]>max) { max=array[i]; }
if(array[i]>secondMax && array[i]<max) {
secondMax=array[i]; }
}
#include <iostream>
using namespace std;
int main() {
int max = 0;
int sec_Max = 0;
int array[] = {81,70,6,78,54,77,7,78};
int loopcount = sizeof(array)/sizeof(int);
for(int i = 0 ; i < loopcount ; ++i)
{
if(array[i]>max)
{
sec_Max = max;
max = array[i];
}
if(array[i] > sec_Max && array[i] < max)
{
sec_Max = array[i];
}
}
cout<<"Max:" << max << " Second Max: "<<sec_Max<<endl;
return 0;
}