TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Alex M
NA
3
2k
How do i prevent stack overflows in binary search algorithm?
Apr 28 2019 2:12 PM
I'm using the following c# binary search algorithm to search for doubles in a double array containing 256 - 4096 elements:
public
static
void
Search(
double
[] array,
double
key)
{
Quicksort.Sort(array);
int
min = 0;
int
max = array.Length - 1;
bool
found =
false
;
while
(min <= max)
{
int
mid = (min + max) / 2;
if
(array[mid] == key)
{
found =
true
;
Console.WriteLine($
"{key} was found at index {mid} of the array"
);
}
else
if
(array[mid] > key)
max = mid - 1;
else
if
(array[mid] < key)
min = mid + 1;
}
if
(found ==
false
)
Console.WriteLine($
"{key} wasn't found in the array."
);
When i try to run it, i get a stack overflow exception thrown... How do i fix it?
Thanks for the help!
Attachment:
SearchingandSorting.zip
Reply
Answers (
2
)
Update Set is not working with Clause Where
How to get the MaxValue using LINQ ?!