Tournament Structure Sorting C++
<p>Here I have this code that sorts a tournament structure like array in descending order. It sorts all but one number and always returns a <strong>-1</strong> as the lowest integer it sorts, I have read through this code multiple times and I can't seem to figure out why it's not sorting properly, I'm not sure if it's just missing my eyes or if there is a small typo somewhere.</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;cmath&gt;


using namespace std;

int maxi(int i, int j)
{
  if (i &gt; j) return(i);
  else return(j);
}

int mini(int i, int j)
{
  if (i &lt; j) return(i);
  else return (j);
}

int buildtourn(int tourn[], int n)
{
  int min1=0, a;
  //Compute tournament structure
for (int i=2*n-2; i&gt;1; i=i-2)
   {
    tourn[i/2] = maxi(tourn[i], tourn[i+1]);
    a=mini(tourn[i], tourn[i+1]);
    if (min1&gt;a) min1=a;
    }
    return min1;
}

int getnext(int tourn[], int n, int low)
{
int i = 2;
//Part 1 - downward traversal
while (i &lt;= 2*n-1)
{
    if (tourn[i]&gt;tourn[i+1])
    {
        tourn[i]=low;
        i=2*i;
    }
    else
    {
        tourn[i+1]=low;
        i=2*(i+1);
    }
}

//Part 2 - upward traversal
for (i = i/2; i&gt;1; i=i/2)
{
    if (i%2==0) tourn[i/2]=maxi(tourn[i],tourn[i+1]); // go to the right of i
    else tourn[i/2]=maxi(tourn[i], tourn[i-1]); // to the left of i
}
return 0;
}
int main()
{
int tourn[100], n, i, low;
//Read
cout &lt;&lt; "Give n :" ;
cin &gt;&gt; n;
cout&lt;&lt; "Enter the integers to be sorted : " &lt;&lt; endl;
for (i=n; i&lt;=2*n-1; i++)
    cin &gt;&gt; tourn[i];

//build tournament
low=buildtourn(tourn,n)-1;

//Sorting
cout &lt;&lt; " Sorted items are : " &lt;&lt; endl;
for(i=1; i&lt;=n; i++)
{
    cout &lt;&lt; tourn[i] &lt;&lt; '\t';
    getnext(tourn,n,low);
}
cout &lt;&lt; '\n';

return 0;
}
</code></pre>

<p>I believe the error lies solely in my function that builds the tournament structure but, i'm not quite sure if i'm looking in the wrong place.</p>

<pre><code>int buildtourn(int tourn[], int n)
{
  int min1=0, a;
  //Compute tournament structure
  for (int i=2*n-2; i&gt;1; i=i-2)
  {
    tourn[i/2] = maxi(tourn[i], tourn[i+1]);
    a=mini(tourn[i], tourn[i+1]);
    if (min1&gt;a) min1=a;
  }
return min1;
}
</code></pre>

<p>Thank you in advance for any help and If I need to add anymore details to this problem please let me know in the comments.</p>

<p>EDIT: This is a link to view the output i am receiving. 
<a href="http://imgur.com/a/KNDO8" rel="nofollow">http://imgur.com/a/KNDO8</a></p>

<p>EDIT 2: If i were to use the numbers 20 14 1 3 8 to be sorted, it would sort them as <strong>20 8 1 3 -1</strong></p>