Collections-C icon indicating copy to clipboard operation
Collections-C copied to clipboard

Binary search on a sorted Array

Open srdja opened this issue 8 years ago • 7 comments

You should be able do perform a binary search on a sorted array.

srdja avatar Mar 26 '16 12:03 srdja

So this begs the question, when do we know it's sorted? We could sort it every time the method is called. But probably, we'd want to have a variable in array_c called "is_sorted" that is only ever set to true when the array_sort() is called, and set to false when any alteration to the array is made, yeah?

Nekel-Seyew avatar Mar 30 '16 06:03 Nekel-Seyew

Yeah, sorting on each search is way worse that just doing a linear search. We definetly want to just mark the array when it's sorted.

I think it's best to implement bsearch as an internal optimization instead of a function that the user can call. For example if the user calls something like array_index_of, it can use bsearch internally if the array is marked as sorted instead of a linear search to speed up the search.

srdja avatar Mar 30 '16 16:03 srdja

Bsearch won't work in that instance, because bsearch only returns if a given item is in the array, not the first index as "index_of" normally returns. We could then just go backwards till we get first item, but then that's basically the same as linear search.

Nekel-Seyew avatar Mar 30 '16 17:03 Nekel-Seyew

There's no need to use the libc bsearch. It's trivial to implement a bsearch to return the index of the matching element.

srdja avatar Mar 30 '16 18:03 srdja

I mean the bsearch algorithm, not just the libc implementation, isn't guaranteed to return the first element in a sorted array which matches, just returns true if it does find it. Which means we'd then have to search for it ourselves, going backwards.

Nekel-Seyew avatar Mar 30 '16 18:03 Nekel-Seyew

I see what you mean. It doesn't match the spec of finding the first match in the sequence. But we should still be able to get better performance if we mix bsearch and linear search than with plain linear search, because on average, once we find the match through bsearch, we won't have to go too far backwards to find the first element.

srdja avatar Mar 30 '16 18:03 srdja

This idea is good, but there is only problem, we need a comparator, as i saw the array implementation, the pointer to the comparator function is not stored in the array struct, so it could be easy if there was a pointer, as if the array was sorted then we could determine if the new element when added will result the array to remain sorted or not in O(1) time, and it would be easy to do bin search, in the function array_index_of function, as bin search will req a comparator, and it would not be wise to give the comparator every time in array_index_of function while searching

nimitbhardwaj avatar Jun 28 '17 17:06 nimitbhardwaj