0

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 -1 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.

#include <iostream>
#include <cmath>


using namespace std;

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

int mini(int i, int j)
{
  if (i < 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>1; i=i-2)
   {
    tourn[i/2] = maxi(tourn[i], tourn[i+1]);
    a=mini(tourn[i], tourn[i+1]);
    if (min1>a) min1=a;
    }
    return min1;
}

int getnext(int tourn[], int n, int low)
{
int i = 2;
//Part 1 - downward traversal
while (i <= 2*n-1)
{
    if (tourn[i]>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>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 << "Give n :" ;
cin >> n;
cout<< "Enter the integers to be sorted : " << endl;
for (i=n; i<=2*n-1; i++)
    cin >> tourn[i];

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

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

return 0;
}

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.

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

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.

EDIT: This is a link to view the output i am receiving. https://i.stack.imgur.com/o5oNK.jpg

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

user3358064
  • 7
  • 2
  • 7
  • Please read [http://stackoverflow.com/help/how-to-ask]. How do you know it does not sort properly? You should include input, actual and expected output in the question – 463035818_is_not_an_ai Oct 09 '16 at 17:30
  • Perhaps if you properly indented your code, so that it's actually readable, it might be easier to see the problem. – Sam Varshavchik Oct 09 '16 at 17:30
  • @tobi303 I apologize, i've edited it to include a link of the output I am recieving – user3358064 Oct 09 '16 at 17:35
  • @SamVarshavchik I copy pasted straight the program i was using to make this, I will fix that as well. – user3358064 Oct 09 '16 at 17:35
  • 1
    Ok, now your question has a different problem: questions on stackoverflow.com must include all pertinent information in the question itself, instead of links to external web sites that can stop working at any time, rendering the question meaningless. – Sam Varshavchik Oct 09 '16 at 17:36
  • @SamVarshavchik I do not have the reputation to include embedded images, It only lets me provide links – user3358064 Oct 09 '16 at 17:37
  • 3
    Who said anything about images? In fact, images are discouraged, and if you include a useless image in your question it will be quickly downvoted. I see nothing that you can't mention here as plain text. Your program is not drawing a graph. It's output should be plain text and you must include as such, instead of some utterly useless image. – Sam Varshavchik Oct 09 '16 at 17:39
  • why `int min1=0` ? should'nt it be initialized with a greater value like `MAX_INT` since it can only decrease ? – Franck Oct 09 '16 at 17:43
  • @Franck I was thinking i only had to initialize this variable since it is assigned a new value within the function? Or is that the wrong way of thinking? – user3358064 Oct 09 '16 at 17:46
  • 1
    Where is it assigned to a new value ? I only see `if (min1>a) min1=a;` so if all `a` are `>= 0` it will never be assigned. – Franck Oct 09 '16 at 17:49
  • I can't make sense of your Tournament sort algorithm. Did the instructor give this to you or did you make it up or copy it from somewhere? – Barmak Shemirani Oct 09 '16 at 19:17
  • @BarmakShemirani it was given by an instructor – user3358064 Oct 10 '16 at 16:09

1 Answers1

0

In mini maxi functions, replace < and > by <= and >=