Simultaneous search for maximum and second largest tournament algorithms

Source: Internet
Author: User

Problem: Look for both the maximum and the second largest value in an array, assuming that the number of elements in the array is n greater than 2

method One : Iterate through the array, set the Max and Secondmax flags, and update max if there is greater than Max, and update Secondmax if there is less than Max but greater than Secondmax.

Comparisons: Finding temporary Max and Secondmax in a[0] and a[1] requires a comparison. The worst of the remaining number of n-2 needs to be compared with Max and Secondmax respectively, a total of 2 (n-2), so the total number of comparisons is 1+2 (n-2). The code is as follows:

void Traverse_find (int a[],int len,int *max,int *secondmax) {int maxt,secondmaxt;int i;if (a[0]<a[1]) {     // Select a temporary maximum and second largest value, need a comparison maxt = A[1];secondmaxt = A[0];} Else{maxt = A[0];secondmaxt = A[1];} for (i=2;i<len;i++) {if (a[i]>maxt) {<span style= "white-space:pre" ></span>secondmaxt = Maxt;maxt = a[ I];} else if (A[I]>SECONDMAXT) {  //This case is required two times compared secondmaxt = A[i];}} *max = Maxt;*secondmax = Secondmaxt;}

method Two : Using the tournament algorithm, set an additional 2n array (the size of the extra array in the optimized algorithm is N) b, first copy the n number of the original array to the last n positions of the extra array, and the extra array B as a structure similar to the maximum heap. Then from the second-to-last position, select the two number of the larger person to the Father node, in turn, until the root node (b[1]), then b[1] must be the maximum value of the n number, the second largest value must be compared with the maximum value and lost to the maximum value, Therefore, to find the second largest value only from the elements directly to the maximum value to find, directly to the maximum number of elements have "lgn the upper bound"; Find the maximum need to n-1 times comparison, find the second big value needs "LGN upper bound"-1 times comparison, so altogether need n+ "LGN upper Bound"-2 times comparison, A practical adversary strategy can prove that in the worst case this is the minimum number of comparisons to find the second largest value.

Below, where the red number represents the ascent of the maximum value, the yellow node represents the element that is directly lost to the maximum value, looking for the second largest value from these yellow nodes.



1): Using the tournament algorithm with an extra space of 2n, the algorithm directly applies an array of 2n space and copies the existing array directly into the second n positions of the extra array:

void Tournamet (int a[],int len,int *max,int *secondmax) {int aLen = 2*len;int *ta = (int*) malloc (sizeof (int) *alen); int i;in T secondlarge = -1;for (i=len;i<alen;i++) {ta[i] = A[i-len];} for (i=alen-2;i>=2;i-=2) {TA[I/2] = max (ta[i],ta[i+1]);} *max = ta[1];  TA[1] is the maximum value saved//search for the second largest value in the number of direct loss to the maximum value for (i=1;2*i<alen;) {if (Ta[2*i]==ta[i]) {if (secondlarge<ta[2*i+1]) Secondlarge = Ta[2*i+1];i = 2*i;} Else{if (secondlarge<ta[2*i]) Secondlarge = Ta[2*i];i = 2*i+1;}} *secondmax = Secondlarge;}
2): Using a tournament algorithm with an additional space of N, this algorithm directly uses the original array as the leaf node of the heap, in the process of searching if the size of the extra array is searched from the original array, the maximum subscript is not more than 2n. The algorithm is as follows, note that there is a node in the left child and right child respectively in two arrays of the situation:

void tournamet_modify (int a[],int len,int *max,int *secondmax) {int aLen = 2*len;int *ta = (int*) malloc (sizeof (int) *len); int i; int secondlarge = -1;for (i=alen-2;i>=2;i-=2) {if (I>=len) {TA[I/2] = max (A[i-len],a[i+1-len]);} else if (i==len-1) {TA[I/2] = max (A[0],ta[i]);} ELSE{TA[I/2] = max (ta[i],ta[i+1]);}}  *max = ta[1]; TA[1] That is the maximum value to save//search for the second largest value, in the number of directly lost to the maximum value for (i=1;2*i<alen;) {if (2*i<len-1) {if (Ta[2*i]==ta[i]) {if ( SECONDLARGE<TA[2*I+1]) Secondlarge = Ta[2*i+1];i = 2*i;} Else{if (secondlarge<ta[2*i]) Secondlarge = Ta[2*i];i = 2*i+1;}} else if (2*i==len-1) {if (Ta[2*i]==ta[i]) {if (secondlarge<a[0]) Secondlarge = A[0];i = 2*i;} Else{if (secondlarge<ta[2*i]) Secondlarge = Ta[2*i];i = 2*i+1;}} Else{if (A[2*i-len]==ta[i]) {if (secondlarge<a[2*i-len+1]) Secondlarge = A[2*i-len+1];i = 2*i;} Else{if (Secondlarge<a[2*i-len]) Secondlarge = A[2*i-len];i = 2*i+1;}}} *secondmax = Secondlarge;} 

The above three methods can be called using the following main function:

void Main () {int a[] = {100,23,43,6,123,8,8,99,20};int Max,secondmax;int maxt,secondmaxt;tournamet_modify (a,9,& Max,&secondmax); Traverse_find (A,9,&MAXT,&SECONDMAXT);p rintf ("Max:%d,second max:%d\n", Max,secondMax) ;p rintf ("Max:%d,second max:%d\n", Maxt,secondmaxt);p rintf ("\ n");}


Simultaneous search for maximum and second largest tournament algorithms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.