
-----------------------------------
rated
Sun Jan 25, 2009 3:45 pm

timing sequential search vs binary search
-----------------------------------
Hey, guys im creating a program that compares the time it takes to execute a binary search vs a sequential search, with the location of the item printed out. AM I doing something wrong? Im getting 0 seconds for both searches.


public static void generate(int list1 

-----------------------------------
Clayton
Sun Jan 25, 2009 3:53 pm

RE:timing sequential search vs binary search
-----------------------------------
public static void generate(int list1 

And

public static int sequentialSearch(int list1

Look wrong to me.

-----------------------------------
rated
Sun Jan 25, 2009 4:13 pm

RE:timing sequential search vs binary search
-----------------------------------
the parameters do not need to be the same, do they?

-----------------------------------
Clayton
Sun Jan 25, 2009 5:35 pm

RE:timing sequential search vs binary search
-----------------------------------
Well, you're trying to pass an array of integers are you not? I don't see that reflected in your parameters.

-----------------------------------
DemonWasp
Mon Jan 26, 2009 9:14 am

RE:timing sequential search vs binary search
-----------------------------------
I see a couple of problems here. First, you need to sort the array before passing it to a binary sort. Sequential sorts work on unsorted data; binary sorts only work on sorted data. Second, you need to realise that "binary search is faster than sequential search" doesn't mean that one is noticeably faster than the other; the important difference is what happens if you have to do the search a large number of times (say a million or more).

Don't forget to include the time required to sort the array into the execution time of the binary search. It may be true that for few enough executions, sequential search is faster than binary search, since you don't have the high initial cost of the sort beforehand.
