Array Search Algorithm
Page 1 of 114 Replies - 3937 Views - Last Post: 27 May 2011 - 08:14 AM
#1
Array Search Algorithm
Posted 24 May 2011 - 08:25 AM
I wonder what is a good algorithm to find a specific value in an already sorted array (I know that I could simply loop through the array, but that’s not efficient …).
maybe an example will help to explain that better:
let’s assume there is a data array [1, 2, 3, 5, 8, …] (the application will store objects, but for the sake of simplicity I use integers here), then I want to find those two numbers, which are closest to my input number, i.e. if the input is 3.4 it should return 3 & 5 (respectively the array indices thereof). I thought the algorithm could be something kinda like Quicksort (without the actual sorting) but I’m not sure about that.
Dormi
PS. the application language is Javascript
Replies To: Array Search Algorithm
#2
Re: Array Search Algorithm
Posted 24 May 2011 - 08:28 AM

POPULAR
#3
Re: Array Search Algorithm
Posted 24 May 2011 - 08:56 AM
#4
Re: Array Search Algorithm
Posted 24 May 2011 - 09:25 AM
//not my own example... Taken from other source var object = new Object(); object.abc = "123"; //can be retrieved with object.abc
this might be useful to you since you already know what you are looking for (known as a key). If you try to retrieve the key, a value assigned to it will be returned. Just spewing ideas
#5
Re: Array Search Algorithm
Posted 24 May 2011 - 09:37 AM
#6
Re: Array Search Algorithm
Posted 24 May 2011 - 09:49 AM
#7
Re: Array Search Algorithm
Posted 24 May 2011 - 01:31 PM
for those interested in the implementation
var data = [];
data.binarySearch = function (key, value)
{
if (isNaN(value)) {
throw new TypeError("Viscosity is not a Number");
}
value = parseFloat(value);
var c, min = 0, max = this.length - 1;
function dataOf(i)
{
return data[i][key];
}
// check bounds
if (dataOf(min) > value) {
throw new Error("Value too small");
}
if (dataOf(max) < value) {
throw new Error("overflow");
}
do {
// abort condition
if ((max - min) < 2) {
return Viscosity.createIntermediate(this[min], this[max], value);
}
c = Math.ceil((max + min) / 2);
if (dataOf(c) > value) {
max = c;
}
else if (dataOf(c) < value) {
min = c;
}
} while (dataOf(c) != value);
return this[c];
};
This post has been edited by Dormilich: 24 May 2011 - 01:33 PM
#8
Re: Array Search Algorithm
Posted 25 May 2011 - 04:02 PM
I think I've coded it correctly, if not is something very similar.
Function Find(Value,Min,Max) Offset=Min Delta = A[Max] - A[Min] Count = Max - Min Targe = Value / Delta Index = Targe * Count + Offset cmp = A[Index].ComparedTo(Value) If cmp=0 Then Return Index If Min=>Max Then Return -1 If cmp>0 Then Return Find(Min,Index) If cmp<0 Then Return Find(Index,Max) End Function
This post has been edited by AdamSpeight2008: 25 May 2011 - 04:05 PM
#9
Re: Array Search Algorithm
Posted 25 May 2011 - 10:17 PM
This post has been edited by Dormilich: 26 May 2011 - 02:30 AM
#10
Re: Array Search Algorithm
Posted 27 May 2011 - 05:10 AM
Module Module1
Sub Main()
Dim a0() As Integer = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Dim a1() As Integer = {0, 1, 1, 2, 2, 3, 3, 4, 5, 5}
Dim a2() As Integer = {0, 5, 8, 9, 10, 11, 12, 14, 15, 16}
Dim i1 = Find(a2, 5, 0, 9)
End Sub
Private Function Find(ByRef ar As Integer(), Target As Integer, I_min As Integer, I_max As Integer, Optional depth As Integer = 0) As Integer
Dim I_delta As Integer = I_max - I_min
' There must be at least one item in the collection.
If I_delta < 1 Then Return -1
' Calculate the abosulate value of the minimum value.
Dim V_offset = Math.Abs(ar(I_min))
' Work out the delta between the max and min values, accounting for the offset.
Dim V_delta As Integer = (ar(I_max) + V_offset) - (ar(I_min) + V_offset)
' What
Dim Percentage As Double = ((Target + V_offset) / (V_delta))
' Is the ratio not within the bound 0 to 1, if so then return -1 (Not Found)
If Percentage > 1 OrElse Percentage < 0 Then Return -1
' Calcluate the Index position of the guess.
Dim I_Guess = Math.Floor(I_min + (I_delta * Percentage))
' What value is at the guess index.
Dim V_Guess = ar(I_Guess)
' Compate the guess value with the target
Dim cmp = V_Guess.CompareTo(Target)
Select Case cmp
Case 0 : Return I_Guess + I_min
Case Is < 0 : Return Find(ar, Target, I_Guess+1, I_max - 1, depth + 1)
Case Else
Return Find(ar, Target, I_min + 1, I_Guess-1, depth + 1)
End Select
End Function
End Module
Worse Case: O(n)
Best Case: O(ln ln n)
Edit: Modified the code so the algorithm converges correctly.
This post has been edited by AdamSpeight2008: 27 May 2011 - 06:27 AM
#11
Re: Array Search Algorithm
Posted 27 May 2011 - 06:22 AM
while(true) {
if ((max - min) < 2) { return Viscosity.createIntermediate(this[min], this[max], value); }
var c = Math.ceil((max + min) / 2);
var foundValue = dataOf(c);
if (foundValue==value) { return this[c]; }
if (foundValue > value) {
max = c;
} else {
min = c;
}
}
This saves two array lookups and one comparison. Might be a noticeable improvement on larger sets.
#12
Re: Array Search Algorithm
Posted 27 May 2011 - 06:26 AM
#13
Re: Array Search Algorithm
Posted 27 May 2011 - 06:43 AM
If the contents of the array are linear, then the lookup (assuming that it is within the set) is O(1).
#14
Re: Array Search Algorithm
Posted 27 May 2011 - 06:48 AM
This post has been edited by baavgai: 27 May 2011 - 06:48 AM
#15
Re: Array Search Algorithm
Posted 27 May 2011 - 08:14 AM
Find ( ar : array [Int32],
target : Int32,
I_min : Int32,
I_max : Int32
) : Boolean * Int32
{ def I_delta = I_max - I_min;
match( I_delta < 1 )
{ | true => ( false, I_min )
| false =>
{ def V_offset = Math.Abs(ar[I_min]);
def V_delta = ar[I_max] - ar[I_min];
def p = (target:double + V_offset) / V_delta;
match (p)
{ | p when p<0 => ( false, I_max + 1 )
| p when p>1 => ( false, I_min )
| _ =>
def I_guess = Math.Floor(I_delta * p + V_offset):>Int32;
def V_guess = ar[I_guess];
def cmp = V_guess.CompareTo(target);
match (cmp)
{ | 0 => ( true, I_guess );
| p when p<0 => Find(ar, target, I_guess + 1, I_max - 1);
| _ => Find(ar, target, I_min + 1, I_guess - 1);
}
}
}
}
}
This post has been edited by AdamSpeight2008: 27 May 2011 - 08:16 AM
|
|

New Topic/Question
Reply


MultiQuote










|