Suppose we have a file of n records which are partially sorted as x1 <= x2 <= x3 <= … <= xm, and xm+1 <= ….. <= xn, is it possible to sort the entire file in time O(n) using only a small fixed amount of additional storage?
Yes, this is part of how one of the commonly-taught sorting algorithms builds a final result, by merging together partial results after a divide-and-conquer step. Any guess which algorithm?
alternatively, how would you build an algorithm to solve it in O(n) time?
Best Regards,
BioPhysEngr http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
I don't know which algorithm is used and more over i am not building the algorithm. So the basic question is is it possible to sort in O(n) time and by using only small amount of additional storage??
I don't know which algorithm is used and more over i am not building the algorithm. So the basic question is is it possible to sort in O(n) time and by using only small amount of additional storage??
Just to make sure. Are you allowed to create a new sorted file (leaving the old file unchanged)?
Write an algorithm that takes an array containing zeroes and ones, and returns true, if every sequence of consecutive ones is even. Else, it returns false. Analyze the running time of the algorithm.
yes we are allowed to create a new stored file or we can use the old file.
If you're allowed to create a new file for the sorted output the problem is easy and equivalent to merging two sorted files (which has a well known O(N) solution, just search the net).
You need to treat your partially sorted file P as two sorted files A and B. A starts at the beginning of P and ends where the sorting on P breaks. This is where B starts and it ends where P ends.
BioPhysEngr http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
So, first, if you have a new question, please create a new thread. It makes it easier for others to discover the discussion via search.
Second, we're happy to help you learn, but you need to show some degree of effort. If you have a specific question, show us what you have got so far and explain where you are stuck and we will try to help you learn, probably by providing a hint or a helpful link. You will, in general, not find people willing to solve your homework problems for you though.
Best Regards,
BioPhysEngr http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
Bookmarks