473,382 Members | 1,545 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Sort it with one array and some tmp files

Hi,
I've got the following problem. I have to sort x*y elements which are
in one file. I can use only an array for x elements and floor[y/4] tmp
files which can be read only forward.

Thanks for every clue.

JS

Oct 29 '06 #1
5 1845
ja******@gmail.com wrote:
>
I've got the following problem. I have to sort x*y elements which
are in one file. I can use only an array for x elements and
floor[y/4] tmp files which can be read only forward.
Arrange the array to be a heap. Read in and heapify the first x
elements, and dump the heap (see heapsort). Repeat until 4x have
been processed. Read any remainder into the heap. Now read back
the 4 tmp files element by element (they will be sorted) and
mergesort them (and the extra data in the x array, which is
effectively a 5th temp file) and write the elements out one by one.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Oct 29 '06 #2
ja******@gmail.com wrote:
Hi,
I've got the following problem. I have to sort x*y elements which are
in one file. I can use only an array for x elements and floor[y/4] tmp
files which can be read only forward.
OK, off you go and do it then.
Thanks for every clue.
Clue 1, this is an algorithms problem not a C problem. We discuss C here
hence the name of the group.

Clue 2, read the text book that goes with the course you are doing. Try
looking in the index under sort.

Clue 3, the normal reason for only reading files forwards is that they
are on a tape.

A more appropriate group would probably be comp.programming, but read
there FAQ before posting there.
--
Flash Gordon.
Oct 29 '06 #3
In article <45***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
>ja******@gmail.com wrote:
>I've got the following problem. I have to sort x*y elements which
are in one file. I can use only an array for x elements and
floor[y/4] tmp files which can be read only forward.
>Arrange the array to be a heap. Read in and heapify the first x
elements, and dump the heap (see heapsort). Repeat until 4x have
been processed. Read any remainder into the heap. Now read back
the 4 tmp files element by element (they will be sorted) and
mergesort them (and the extra data in the x array, which is
effectively a 5th temp file) and write the elements out one by one.
I don't think that will solve his assignment / exam / interview
question.

The process you describe will handle at most 5*x elements, but
he needs to be able to handle y*x elements. He is not restricted
to 4 tmp files, he is restricted to floor[y/4] tmp files, each
of indefinite size but each of which "can be read only forward".
Also, your mergesort would require at least 5 variables (one
per temp file and one for the remaining data in the array), but
the problem specification says "I can use only an array for x elements"
together with the temp files, and that could be interpreted as
indicating that those temporary variables for the mergesort are not
allowed unless they are part of that array whose total fixed
length is x (in which case at most 5*x-5 elements could be sorted.)

I'm not sure what "can be read only forward" means --
it -might- mean that each is permitted only a sequential write at
end of file, then a single rewind, then a single read through in
the forward direction. But it maybe random access writes are
acceptable as long as there is never a read of an element earlier
than one that has already been read later in the file. On the
other hand, the problem statement doesn't say that the files can't
be reused any number of times -- write data, rewind, read forward,
rewind, now you can write data again... Indeed, though I don't have
a solid algorithm in mind, I believe that the key would be to reuse
the tmp files. (Hmmm, the shadow of an algorithm I had in mind
wouldn't work with less than 2 tmp files, but if y is 2 or 3 then
*no* tmp files are allowed; if y is 1 then just sort into the array
since y*x = 1*x = x...)

Hmmm, is that even possible, to sort 2*x or 3*x elements using
only a single array of length x and no temporary files? I don't
think it is. Imagine that the inputs are exactly in reverse order,
then the first thing output must be the last thing input, but that
requires temporary storage of 2*x or 3*x pieces of information
into an array that can only hold x pieces of information. By the
Pigeon Hole Principle, this is impossible.

So, the problem is impossible to solve.

Possibly the problem would be solvable if ceiling[y/4] tmp files
were allowed instead of floor[y/4], but that would depend on
what the part about read only forward means.
--
Prototypes are supertypes of their clones. -- maplesoft
Oct 29 '06 #4

Walter Roberson wrote:
In article <45***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
ja******@gmail.com wrote:
I've got the following problem. I have to sort x*y elements which
are in one file. I can use only an array for x elements and
floor[y/4] tmp files which can be read only forward.
Arrange the array to be a heap. Read in and heapify the first x
elements, and dump the heap (see heapsort). Repeat until 4x have
been processed. Read any remainder into the heap. Now read back
the 4 tmp files element by element (they will be sorted) and
mergesort them (and the extra data in the x array, which is
effectively a 5th temp file) and write the elements out one by one.

I don't think that will solve his assignment / exam / interview
question.

The process you describe will handle at most 5*x elements, but
he needs to be able to handle y*x elements. He is not restricted
to 4 tmp files, he is restricted to floor[y/4] tmp files, each
of indefinite size but each of which "can be read only forward".
Also, your mergesort would require at least 5 variables (one
per temp file and one for the remaining data in the array), but
the problem specification says "I can use only an array for x elements"
together with the temp files, and that could be interpreted as
indicating that those temporary variables for the mergesort are not
allowed unless they are part of that array whose total fixed
length is x (in which case at most 5*x-5 elements could be sorted.)

I'm not sure what "can be read only forward" means --
it -might- mean that each is permitted only a sequential write at
end of file, then a single rewind, then a single read through in
the forward direction. But it maybe random access writes are
acceptable as long as there is never a read of an element earlier
than one that has already been read later in the file. On the
other hand, the problem statement doesn't say that the files can't
be reused any number of times -- write data, rewind, read forward,
rewind, now you can write data again... Indeed, though I don't have
a solid algorithm in mind, I believe that the key would be to reuse
the tmp files. (Hmmm, the shadow of an algorithm I had in mind
wouldn't work with less than 2 tmp files, but if y is 2 or 3 then
*no* tmp files are allowed; if y is 1 then just sort into the array
since y*x = 1*x = x...)

Hmmm, is that even possible, to sort 2*x or 3*x elements using
only a single array of length x and no temporary files? I don't
think it is. Imagine that the inputs are exactly in reverse order,
then the first thing output must be the last thing input, but that
requires temporary storage of 2*x or 3*x pieces of information
into an array that can only hold x pieces of information. By the
Pigeon Hole Principle, this is impossible.

So, the problem is impossible to solve.

Possibly the problem would be solvable if ceiling[y/4] tmp files
were allowed instead of floor[y/4], but that would depend on
what the part about read only forward means.
--
Thanks, I moved the topic to comp.programming. Please answer there.
We can write to this file at its end, then rewind it, read it through
in the forward direction, and then again - rewind, write, rewind, read
etc. Let's assume that y is greater then 4. If not enough files, let's
make it 8.
Thanks for response and I'm sorry for posting on comp.lang.c

JS

Oct 29 '06 #5
ja******@gmail.com wrote:
Hi,
I've got the following problem. I have to sort x*y elements which are
in one file. I can use only an array for x elements and floor[y/4] tmp
files which can be read only forward.

Thanks for every clue.

JS
If _I_ were faced with this kind of problem I would look in Knuth's
"The Art of Computer Programming". I think it may be vol. I that
you want, "Sorting and Searching" (unless that's v. III ;-)

--
Julian V. Noble
Professor Emeritus of Physics
University of Virginia
Oct 29 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: jagg | last post by:
Hi, with the code below the output is sort by $verantw but $title and $file in the same row DON'T belong to $verantw. How do I have to make the sort command that $verantw, $title and $file in...
40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
19
by: David | last post by:
Hi all, A while back I asked how to sort an array of strings which would have numerals and I wanted to put them in sequential numerical order. For example: myArray = "file1"; myArray =...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
16
by: Gerrit | last post by:
Hello, Is it possible to sort an array with a struct in it? example: I have a struct: public struct mp3file { public int tracknr;
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
6
by: shana07 | last post by:
Phew, I have problem..How to sort number in my files..I have these in my input files...: I need to sort the line in array from 12, 64, 8, 128 etc. 3 12 4 64 7 8 10 128 ... I just wanna...
4
by: jonathan184 | last post by:
Hi I have a perl script, basically what it is suppose to do is check a folder with files. Now the files are checked using a timestamp with the command ls -l so the timestamp in this format is...
3
by: aRTx | last post by:
I have try a couple of time but does not work for me My files everytime are sortet by NAME. I want to Sort my files by Date-desc. Can anyone help me to do it? The Script <? /* ORIGJINALI
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.