Return to Snippet

Revision: 60258
at October 28, 2012 00:48 by awsomedevsigner


Initial Code
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace testhash
{
    class Program
    {
        static void Main(string[] args)
        {

            var table = new int[1000];

            using (TextWriter tw = new StreamWriter(@"d:\test\TestDataFile.txt"))
            {

                Random r = new Random(23874837);



                for (int i = 1; i < 1000000; i++)
                {

                    tw.Write(r.Next(1, 1000));
                    tw.Write(Environment.NewLine);

                }



            }    

            using(TextReader reader = new StreamReader(@"d:\test\TestDataFile.txt"))
            {
                string line;

                        while((line = reader.ReadLine()) != null)
                        {
                            table[Convert.ToInt32(line)] += 1;
                        }
                
                            //int sum = table.Sum();

                            //Console.WriteLine(sum);


                        using (TextWriter tw = new StreamWriter(@"d:\test\TestDataFileOutup.txt"))
                        {
                            for(var i = 0; i <= table.Count()-1;i++)
                            {
                                
                                for(int writeTimes = 1; writeTimes <= table[i];writeTimes++)
                                {
                                    tw.Write(i);
                                    tw.Write(Environment.NewLine);
                                }

                            }
                        }
            }
            
        }





    }

    
}

Initial URL

                                

Initial Description
Sort a file that contains integers between 1 and thousand. The output file should contain the entries like (n-times 1, followed by n-times 2 and so on): 1111222333555... But - you are not allowed to load all entries into memory at once. How to solve it? Here is my solution.

Initial Title
Interview Question (C#)

Initial Tags
c#

Initial Language
C#