SRL Archive - OSR Help

Finding the three highest integers


halgboy_tbc

2013-03-14 12:04:32

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?

Kevin

2013-03-14 12:16:01

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?

Sin

2013-03-14 12:22:53

High(Array);

Flight

2013-03-14 12:30:59

From: Sin
High(Array);


That returns the last variable in the array, not the largest number.

litoris

2013-03-14 12:31:53

From: Sin
High(Array);


Doesn't that simply return the # of the last item in the array, effectively telling you the size of the array?

Sin

2013-03-14 12:34:04

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

Kevin

2013-03-14 12:35:18

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.

From: Sin
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?

Sin

2013-03-14 12:37:46

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

x[Warrior]x3500

2013-03-14 12:42:00


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

Le Jingle

2013-03-14 12:42:11


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.

Olly

2013-03-14 12:42:12

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

Richard

2013-03-14 12:46:33

: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:

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 :)

Sin

2013-03-14 13:30:09

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

Richard

2013-03-14 13:36:20

From: Sin
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)