Results 1 to 15 of 15

Thread: Finding the three highest integers

  1. #1
    Join Date
    Jan 2012
    Posts
    83
    Mentioned
    0 Post(s)
    Quoted
    4 Post(s)

    Default Finding the three highest integers

    How can I find the three highest integers in an integer array?
    I am looking to find the three highest numbers among 14+ numbers. Is there a function that does something similar?
    I guess I can check if each one is > than the rest and exclude numbers already found greater, but any tips?

  2. #2
    Join Date
    Sep 2012
    Location
    Here.
    Posts
    2,007
    Mentioned
    88 Post(s)
    Quoted
    1014 Post(s)

    Default

    The closest thing you could have to something built in that accomplishes this would be to use one of the sorting functions. However, that would sort the entire array by the size of the integers. If you want it to be faster and do specifically what you describe, then you will have to write it yourself. Since you have a constant quantity of numbers that you want sorted, I think you can do this in O(< 3n) time. (That is less than 3n time, not love 3 time, haha)

    Do you need this specifically for script, or is this some sort of homework project where you can use a different language?

  3. #3
    Join Date
    Dec 2011
    Location
    Toronto, Ontario
    Posts
    6,424
    Mentioned
    84 Post(s)
    Quoted
    863 Post(s)

    Default

    High(Array);

  4. #4
    Join Date
    Aug 2007
    Location
    Colorado
    Posts
    7,421
    Mentioned
    268 Post(s)
    Quoted
    1442 Post(s)

    Default

    Quote Originally Posted by Sin View Post
    High(Array);
    That returns the last variable in the array, not the largest number.

    Current projects:
    [ AeroGuardians (GotR minigame), Motherlode Miner, Blast furnace ]

    "I won't fall in your gravity. Open your eyes,
    you're the Earth and I'm the sky..."


  5. #5
    Join Date
    Jul 2011
    Location
    /home/litoris
    Posts
    2,226
    Mentioned
    0 Post(s)
    Quoted
    159 Post(s)

    Default

    Quote Originally Posted by Sin View Post
    High(Array);
    Doesn't that simply return the # of the last item in the array, effectively telling you the size of the array?
    Miner & Urn Crafter & 07 Chicken Killer
    SPS BlindWalk Tutorial

    Working on: Nothing

    teacher in every art, brought the fire that hath proved to mortals a means to mighty ends

  6. #6
    Join Date
    Dec 2011
    Location
    Toronto, Ontario
    Posts
    6,424
    Mentioned
    84 Post(s)
    Quoted
    863 Post(s)

    Default

    Oh, well I assume he wants the highest/last number in the array, which would be 14. It would effectively work right?

  7. #7
    Join Date
    Sep 2012
    Location
    Here.
    Posts
    2,007
    Mentioned
    88 Post(s)
    Quoted
    1014 Post(s)

    Default

    High(Array) will return the index of the last item in the array (the last valid position). Length(array) will return the actual length of the array, which is 1+ high(array) as they index starting at position 0.

    Quote Originally Posted by Sin View Post
    Oh, well I assume he wants the highest/last number in the array, which would be 14. It would effectively work right?
    That assumes it's already sorted, which I think we're supposed to assume isn't the case. I swear I've seen a built in function for sorting already though... QuickSort(Array) maybe?

  8. #8
    Join Date
    Dec 2011
    Location
    Toronto, Ontario
    Posts
    6,424
    Mentioned
    84 Post(s)
    Quoted
    863 Post(s)

    Default

    Ah, I was under the assumption that the numbers were linear.
    My mistake :P

  9. #9
    Join Date
    Jan 2009
    Location
    Turlock/LA, California
    Posts
    1,494
    Mentioned
    3 Post(s)
    Quoted
    66 Post(s)

    Default

    Simba Code:
    program new;
    var
     t:tintegerarray;
    begin
      t:= [1,3,7,9,4,2];
      quicksort(t);
      writeln(t);
      writeln('highest: '+tostr(t[high(t)]));
    end.

  10. #10
    Join Date
    Feb 2012
    Location
    Wonderland
    Posts
    1,988
    Mentioned
    41 Post(s)
    Quoted
    272 Post(s)

    Default

    Simba Code:
    var
      n1, n2, n3: Integer;
      arr: TIntegerArray;
    begin
      arr := [5, 2, 6, 2, 6, 8, 32, 2];
      QuickSort(arr);
      n1 := arr[high(arr)];
      n2 := arr[high(arr) - 1];
      n3 := arr[high(arr) - 2];
    end.

  11. #11
    Join Date
    Nov 2011
    Location
    England
    Posts
    3,072
    Mentioned
    296 Post(s)
    Quoted
    1094 Post(s)

    Default

    tia := [1, 211, 5, 55, 7, 9];

    quickSort(tia);
    invertTIA(tia);

    tia[0]
    tia[1]
    tia[3]

    would be the 3 biggest numbers.

    edit: three posts at the exact same time... lol

  12. #12
    Join Date
    Dec 2007
    Location
    192.168.1.73
    Posts
    2,439
    Mentioned
    6 Post(s)
    Quoted
    119 Post(s)

    Default

    :O I didn't realise Simba had an in built QuickSort, I just spent ages making both a bubblesort and insertion sort, very crude efforts :P

    Yeah, so to give you an example, this will write out the 3 highest numbers in the randomly generated array, starting with the highest:

    Code:
    program new;
    var
    temp: TIntegerArray;
    i, size: integer;
    
    begin
      //this is just me creating the array
      setarraylength(temp, 20);
      for i := 0 to 19 do
        temp[i] := random(10000);
      //this is the calcs
      quicksort(temp);
      size := getarraylength(temp);
      for i := size downto size - 2 do
        writeln(temp[i - 1]);
    end.
    If you don't understand it, many people here, including me, will help

  13. #13
    Join Date
    Dec 2011
    Location
    Toronto, Ontario
    Posts
    6,424
    Mentioned
    84 Post(s)
    Quoted
    863 Post(s)

    Default

    Crap, I forgot the BubbleSort(); I wrote a while back would've worked here -.-

  14. #14
    Join Date
    Dec 2007
    Location
    192.168.1.73
    Posts
    2,439
    Mentioned
    6 Post(s)
    Quoted
    119 Post(s)

    Default

    Quote Originally Posted by Sin View Post
    Crap, I forgot the BubbleSort(); I wrote a while back would've worked here -.-
    BubbleSort is really inefficient though, even insertion sort isn't as quick as QuickSort (although I still don't entirely understand the algorithms for quicksort, haven't looked at one in a while)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •