Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Recursion challenge
#21
Well, attending to Moneo, here's my entri Smile

Code:
FUNCTION binarySearch% (Array%(), n%)
   binarySearch% = binarySearchRec%(Array%(), LBOUND(Array%), UBOUND(Array%), n%)
END FUNCTION

'
' binarySearchRec% is called by binarySearch% takes four parameters:
'
' Array%() -> The array to look inside of.
' i1% -> lower limit.
' i2% -> upper limit.
' n% -> Number that is searched.
'
' We basicly take the array, and look the element in the mid place.
' if it equals n%, we have found it!
'
' If the element in the array is SMALLER than n%, we should look in
' the right half of the array, so we call ourselves recursively with
' new i1%, i2% values.
'
' If the element in the array is BIGGER than n%, we should look in the
' left half of the array.
'
' This (of course) only works if the array is sorted.
'
FUNCTION binarySearchRec% (Array%(), i1%, i2%, n%)
   Res% = -1
   IF i1% > i2% THEN binarySearchRec% = Res%: EXIT FUNCTION
   midPoint% = i1% + (i2% - i1%) \ 2
   IF Array%(midPoint%) = n% THEN
      Res% = midPoint%
   ELSEIF Array%(midPoint%) < n% THEN
      Res% = binarySearchRec%(Array%(), midPoint% + 1, i2%, n%)
   ELSE
      Res% = binarySearchRec%(Array%(), i1%, midPoint% - 1, n%)
   END IF
   binarySearchRec% = Res%
END FUNCTION
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply


Messages In This Thread
Recursion challenge - by Antoni Gual - 06-26-2003, 08:04 PM
Recursion challenge - by Antoni Gual - 06-27-2003, 12:37 AM
Recursion challenge - by Moneo - 06-28-2003, 09:49 AM
I beg to differ. - by Agamemnus - 06-30-2003, 05:11 AM
Re: I beg to differ. - by Moneo - 06-30-2003, 06:22 AM
nah - by Agamemnus - 06-30-2003, 09:07 PM
Recursion challenge - by Antoni Gual - 07-01-2003, 01:13 AM
Recursion challenge - by Moneo - 07-01-2003, 02:25 AM
Recursion challenge - by LooseCaboose - 07-01-2003, 05:02 AM
To LooseCaboose: Re Gray's Code - by Moneo - 07-01-2003, 05:38 AM
Recursion challenge - by LooseCaboose - 07-01-2003, 05:52 AM
Recursion challenge - by Antoni Gual - 07-02-2003, 01:30 AM
Recursion challenge - by LooseCaboose - 07-03-2003, 03:30 PM
Recursion challenge - by Moneo - 07-03-2003, 11:14 PM
Recursion challenge - by Hexadecimal Disaster - 07-04-2003, 01:33 AM
Recursion challenge - by LooseCaboose - 07-04-2003, 09:25 AM
To LooseCaboose: - by Moneo - 07-04-2003, 09:15 PM
Recursion challenge - by LooseCaboose - 07-05-2003, 07:39 AM
To LooseCaboose: - by Moneo - 07-05-2003, 08:31 AM
Recursion challenge - by na_th_an - 07-12-2003, 06:43 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)