We can also solve it using the devide-and-conquer method in o(log N), as below:
Split the original array A into two equal sub-arrays of size N/2, say A1, A2. The original array A is sorted if: - If both A1 and A2 are in sorted order, and - last_element of A1 <= first_element_of A2 the above method corresponds to the recursive eq: T(N) = 2 T(N/2) + 1. Googmeister wrote: > Terry wrote: > > hi, > > this is regarding a problem from programming pearls by jon bentley > > column 5. > > I have few questions , > > 1) how to check if an array is sorted or not. Can it be done it sub - > > linear time > > No, if you don't look at all n elements, there's no way to no if > it's sorted. > > > 2) In section 5.8 problem 5, he describes saying that checking a array > > is sorted or not takes n-1 operations. How can you add partial checking > > to the function at significantly less cost. > > I suspect he's talking about binary search. A common > mistake is to apply binary search on a non-sorted array. > Checking whether the array is sorted each time would > ruin the main benefit of binary search (logarithmic time). > Instead, you could verify that the log n elements binary > search probes (and perhaps they're immediate neighbors) > are in the right order. This partial checking takes only > O(log n) time. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/algogeeks -~----------~----~----~----~------~----~------~--~---
