The revolutionary project management tool is here! Plan visually with a single glance and make sure your projects get done.

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?

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

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.

All Courses

From novice to tech pro — start learning today.

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]

}