# Writing algorithms.

• January 16th, 2013, 10:49 PM
psk_002
Writing algorithms.
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?
• January 16th, 2013, 11:19 PM
BioPhysEngr
Re: Writing algorithms.
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?
• January 17th, 2013, 12:12 AM
psk_002
Re: Writing algorithms.
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??
• January 17th, 2013, 02:24 AM
BioPhysEngr
Re: Writing algorithms.
Wouldn't want you to have to run a google search or think about how algorithms might work. Shock! Horror!

• January 17th, 2013, 05:44 AM
nuzzle
Re: Writing algorithms.
Quote:

Originally Posted by psk_002
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)?
• January 17th, 2013, 06:48 PM
psk_002
Re: Writing algorithms.
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.
• January 17th, 2013, 08:15 PM
BioPhysEngr
Re: Writing algorithms.
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.
• January 17th, 2013, 10:09 PM
psk_002
Re: Writing algorithms.
yup i will post in new thread with a new question and in the second question i am done with my algorithm but the analysg the time is bit cnfusing

secondly the the problem with the file sytem s solved from the paper which u posted
• January 20th, 2013, 11:24 PM
psk_002
Re: Writing algorithms.
yes we are allowed to create a new stored file or we can use the old file.
• January 21st, 2013, 03:25 AM
nuzzle
Re: Writing algorithms.
Quote:

Originally Posted by psk_002
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.