0

I'm writing a program that has to find the smallest number through the tournament bracket. For example there is an array

int[] a = new int[4] {4, 2, 1, 3}

and by comparing numbers standing next to each other I've to choose the smallest one. (min(4, 2) -> 2, min(1, 3) -> 1, and then I'm comparing 1 and 2, 1 is the smallest so it's the winner, but it's not possible to compare 2 and 1. Just a[0] with a1, a[2] with a[3] and so. In general a[2*i] with a[(2*i)+1] for(int i=0; i<a.Length/2; i++) <- something like this

First question: If there are n numbers, the whole tree consists of 2n-1 brackets. Am I supposed to create an array of 4 or 7 elements? 4 seems like a better option.

Second question: if I'm comparing 4 and 2, and 2 is smaller should I make a[0] = 2, and then while comparing 1 and 3 a1 = 1? Finally comparing a[0] with a1 and putting the smallest number to a[0]? Temporary int might be needed.

Last question: what do you propose to do it in the simplest way? I could hardly find any info about this algorithm. I hope you will direct my mind into working algorithm.

enter image description here

Not much, but I'm posting my code:

int[] a = new int[4] { 4, 2, 1, 3 };
int tmp = 0;
for (int i = 0; i < (a.Length)/2; i++)
{
    if (a[tmp] > a[tmp + 1])
    {
        a[i] = a[i + 1];
    }
    else if(a[tmp] < a[tmp +1])
    {
        a[i] = a[i + 1];
    }
    tmp = tmp + 2;
}

Can you point what I'm doing ok, and what should be improved?

mikro098
  • 2,173
  • 2
  • 32
  • 48
  • Can you tell us what is it you need to do with the numbers? Do you want to generate a new list of numbers from the previous round of the tournament? How are there 2n-1 brackets when there are n numbers? – Flying_Banana Oct 17 '15 at 23:45
  • I just need to find the smallest one. I was talking about 2n-1 "spaces" in the whole tree as you can see here: http://f.cl.ly/items/463s1h060m3T3b3c2h3l/Zrzut%20ekranu%202015-10-18%2001.50.32.png So on the beginning we've got 4 numbers, but the whole tree consists of 2n-1. – mikro098 Oct 17 '15 at 23:48
  • You seem to be generally on a solid track. Try something, see how it works, post your code and we can help you move forward. – Daniel Hoffmann-Mitscherling Oct 18 '15 at 00:01
  • Okay, so a bracket can be thought as 2 numbers, and you are not allowed to compare numbers other than the number next to it (in its own bracket)? I'm a little confused about what's the exact constraints you are having here...and "If there are n numbers, the whole tree consists of 2n-1 brackets. Am I supposed to create an array of 4 or 7 elements?" doesn't really make sense - where did the n go? – Flying_Banana Oct 18 '15 at 00:01
  • So the whole algorithm can be based on 4 item array if there are 4 numbers like in this example? – mikro098 Oct 18 '15 at 00:20

2 Answers2

1

If tournament style is a must, a recursive approach seems the most appropriate:

int Minimum  (int [] values, int start, int end)
{
   if (start == end)
      return values [start];
   if (end - start == 1)
      if ( values [start] < values [end])
         return values [start];
      else
         return values [end];
   else
   {
      int middle = start + (end - start) / 2;
      int min1 = Minimum  (values, start, middle);
      int min2 = Minimum  (values, middle + 1, end);
      if (min1 < min2)
         return min1;
      else
         return min2;
   }
}

EDIT: Code is untested and errors might have slipped in, since it's been typed on the Android app.

EDIT: Forgot to say how you call this method. Like so:

int min = Minimum  (myArray, 0, myArray.Length -1);

EDIT: Or create another overload:

int Minimum  (int [] values)
{
   return Minimum  (values, 0, values.Length -1);
}

And to call use just:

int min = Minimum  (myArray);

EDIT: And here's non-recursive method (bare in mind that this method actually modifies the array):

int Minimum(int[] values)
{
    int step = 1;
    do
    {
        for (int i = 0; i < values.Length - step; i += step)        
            if(values[i] > values[i + step])
                values[i] = values[i + step];
        step *= 2;
    }
    while(step < values.Length);

    return values[0];
}
Mihai Caracostea
  • 8,336
  • 4
  • 27
  • 46
  • I didn't notice your code, but I've achived a very close thing. So now the values[0] keeps the smallest number, but what with for example "2"? – mikro098 Oct 18 '15 at 14:15
0

There are various simple solutions that utilize functions set up in C#:

int min = myArray.Min();
//Call your array something other than 'a' that's generally difficult to figure out later

Alternatively this will loop with a foreach through all of your values.

int minint = myArray[0];
foreach (int value in myArray) {
  if (value < minint) minint = value;
}

1 - What tree are you talking about? Your array has n values to start with, so it will have n values max. If you mean the number of values in all the arrays you will create is 2n-1, it still doesn't mean you need to fit all of these in 1 array, create an array, use it and then create another array. C# GC will collect Objects of a reference type that have no pointers (are not going to be used again) so it will be fine memory wise if that's your concern?

2 - Post your code. There are a few gotchas but likely you will be fine changing the current array values or creating a new array. Temp int will not be needed.

3 - The above posted algos are the "simplest" using built in functions available to C#. If this is a homework assignment, please post some code.

As a general direction, using a recursive function would likely be most elegant (and some general reading on merge sorts would prove useful to you going forward).

  • I couldn't figure out how to compare a[0] with a[1], a[2] with a[3] and then winners for example a[0] with a[2]. I've also attached a picture of what I'm talking about. Tomorrow I will post what I've got. – mikro098 Oct 18 '15 at 00:04
  • I can definitely help you, however I will not do your homework (or whatever it is) for you, as you seem to be on the right track. Please do feel free to post your code whenever you have time, I believe you can figure this out with a couple small pointers (once we have code to see)! – Daniel Hoffmann-Mitscherling Oct 18 '15 at 00:06