The Code-Bin
Solution for Dev102.InterviewChallenge
Posted by: Heiko Hatzfeld | May 28, 2008 @ 11:15am

C# Code



Syntax Highlighting





Author Comments
Since my Pseudocode was kinda ugly, I wrapped up a real implementation.

I am using C#.
My “Objects” are some plain old number strings, which contain 10 digits, padded with ‘0’s
The Data is stored in a plain textfile. This is NOT the most optimized version, since I am creating a gazillion strings in my code, but thats not the point of this prototype.

How is this Chunk of Code working?

It will read an input file (c:tempmess.txt), and split it into 2 seperate output files. Its doing this by reading chunks of Data that would fit into memory (MaxInitRecords = 100), and sorting those records in its memory.

MaxIntRecords defines the maximum Number of records that can be held in memory at a given time. Those chunks are written to alternate files.

So we arrive at a point with 2 tmp files, each containing blocks of MaxInitRecords that are sorted.
Example for max=3:
1 3
3 4
5 7
2 3
4 5
9 11

After that we start to merge those 2 files into 2 new temp files. Each file will now contain twice the amount of sorted records.

We repeat this until we have not written any data into the 2nd tmp file. This means we have sorted all data.

So… Thats about it… Here is the code:

P.s.: No error handling, since we live in a perfect word… Okok… I just didnt have time to implement it

P.p.s:
- CreateDataFile will create an input file to sort.

Rating


4.90 / 8
568 Votes