Computer Science Canada timing sequential search vs binary search |
| Author: | rated [ Sun Jan 25, 2009 3:45 pm ] | ||
| Post subject: | 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.
|
|||
| Author: | Clayton [ Sun Jan 25, 2009 3:53 pm ] | ||||
| Post subject: | RE:timing sequential search vs binary search | ||||
And
Look wrong to me. |
|||||
| Author: | rated [ Sun Jan 25, 2009 4:13 pm ] |
| Post subject: | RE:timing sequential search vs binary search |
the parameters do not need to be the same, do they? |
|
| Author: | Clayton [ Sun Jan 25, 2009 5:35 pm ] |
| Post subject: | 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. |
|
| Author: | DemonWasp [ Mon Jan 26, 2009 9:14 am ] |
| Post subject: | 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. |
|