This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Sequential examination is not the only way to find the maximum element on a list. Another way to do this is to have the elements play in a tournament much like is done in athletic events. This is accomplished by comparing each pair of elements on the list and moving the largest on to the next round.

Here is the algorithm to do this:

function tournament(a,n)

for i=1 to n do p[i]:=a[i]

k=n (the match list size)

while k>1 do

{ k=k/2

for i=1 to k do

if p[2i]>p[2i-1] then p[i]=p[2i]

else p[i]=p[2i-1]

}

return(p[1])

This algorithm works for an even amount of players. I'm trying to make it work for odd number of players and basically make it work for any quantity of players.

What i thought as a solution was to add a random function if the amount of teams is odd. The team selected from the list with this rand will have a bye in the first round. Does this make sense? If it does, where do i put it in algorithm? Any suggestions?

Here is the algorithm to do this:

function tournament(a,n)

for i=1 to n do p[i]:=a[i]

k=n (the match list size)

while k>1 do

{ k=k/2

for i=1 to k do

if p[2i]>p[2i-1] then p[i]=p[2i]

else p[i]=p[2i-1]

}

return(p[1])

This algorithm works for an even amount of players. I'm trying to make it work for odd number of players and basically make it work for any quantity of players.

What i thought as a solution was to add a random function if the amount of teams is odd. The team selected from the list with this rand will have a bye in the first round. Does this make sense? If it does, where do i put it in algorithm? Any suggestions?

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

while (k > 1) do

{

if (k is odd)

{

if (p[1] < p[k])

p[1] = p[k];

}

k = k/2;

..

..

..

}

return p[1];

you must compare the last value in the odd numbered list prior to the k/2 division, or the integer divide will round down and the last value will not be compared to any value.

When you divide k by 2, the odd man is automatically discarded, because of the round off of the integer divide. ie if k = 5, k/2 = 2. In your code the 5th value would not be compared against any element. The code I added just ensures that the last value is compared against some other value prior to being discarded.

My first 2 comments were a little off the mark, but I believe my answer to be correct.

Just try it and see if it works.

I simply added code that will allow your function to work correctly with any number of elements odd or even.

The total code is as follows:

function tournament(a,n)

{

for i=1 to n do p[i]:=a[i]

k=n (the match list size)

while k>1 do

{

// if k is odd the last value will not be checked by the general algorithm, so check it here.

if (k is odd)

{

// If the last value is larger then the first value, save it in the first postition.

if (p[1] < p[k])

p[1] = p[k];

}

// Now do the tournament

k=k/2 // the last odd-numbered value is discarded by the integer divide.

for i=1 to k do

{ if p[2i]>p[2i-1] then p[i]=p[2i]

else p[i]=p[2i-1]

}

return(p[1])

}

I don't know how you can use the rand function to do what you want. This code should work for what you want.

function tournament(a,n)

{

for i=1 to n do p[i]:=a[i]

k=n (the match list size)

while (k>1) do

{

// if k is odd the last value will not be checked by the general algorithm, so check it here.

if (k is odd)

{

// If the last value is larger then the first value, save it in the first postition

if (p[1] < p[k]) then p[1] = p[k]

}

// Now do the tournament

// if the list is odd, the odd end element will be discarded by this division

k = k/2

for i=1 to k do

{

if p[2i]>p[2i-1] then p[i]=p[2i]

else p[i]=p[2i-1]

}

}

return p[1]

}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
C++

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

I guess that is similar to a bye since you're playing a "null" competitor...)