Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
4.5 kB
2
Indexable
Never
using System;
using System.Collections.Generic;
using System.IO;

class Program
{
    static void Main()
    {
        string inputFilePath = "input_large_file.txt";
        string outputFilePath = "sorted_large_file.txt";
        int chunkSize = 100000; // Adjust this based on your system's memory and performance

        try
        {
            // Step 1: Read the input file in chunks, sort each chunk, and write them to temporary files
            int tempFileCount = CreateSortedChunks(inputFilePath, chunkSize);

            // Step 2: Merge sorted chunks into the output file
            MergeSortedChunks(tempFileCount, outputFilePath);

            Console.WriteLine("File sorted successfully.");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }

    static int CreateSortedChunks(string inputFilePath, int chunkSize)
    {
        int tempFileCount = 0;
        List<int> chunk = new List<int>(chunkSize);

        using (var inputFileStream = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read))
        using (var streamReader = new StreamReader(inputFileStream))
        {
            while (!streamReader.EndOfStream)
            {
                string line;
                while (chunk.Count < chunkSize && (line = streamReader.ReadLine()) != null)
                {
                    if (int.TryParse(line, out int number))
                    {
                        chunk.Add(number);
                    }
                }

                chunk.Sort();

                string tempFilePath = $"temp_chunk_{tempFileCount}.txt";
                using (var tempFileStream = new FileStream(tempFilePath, FileMode.Create, FileAccess.Write))
                using (var streamWriter = new StreamWriter(tempFileStream))
                {
                    foreach (int num in chunk)
                    {
                        streamWriter.WriteLine(num);
                    }
                }

                tempFileCount++;
                chunk.Clear();
            }
        }

        return tempFileCount;
    }

    static void MergeSortedChunks(int tempFileCount, string outputFilePath)
    {
        using (var outputFileStream = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write))
        using (var streamWriter = new StreamWriter(outputFileStream))
        {
            var readers = new StreamReader[tempFileCount];
            var currentValues = new int[tempFileCount];
            var finished = new bool[tempFileCount];

            // Open all temporary files and initialize currentValues array
            for (int i = 0; i < tempFileCount; i++)
            {
                readers[i] = new StreamReader($"temp_chunk_{i}.txt");
                string line = readers[i].ReadLine();
                if (line != null)
                {
                    currentValues[i] = int.Parse(line);
                }
                else
                {
                    finished[i] = true;
                }
            }

            // Merge sorted chunks
            while (true)
            {
                int minValue = int.MaxValue;
                int minIndex = -1;

                // Find the smallest value among the current values from all temporary files
                for (int i = 0; i < tempFileCount; i++)
                {
                    if (!finished[i] && currentValues[i] < minValue)
                    {
                        minValue = currentValues[i];
                        minIndex = i;
                    }
                }

                // If all values are read from a file, mark it as finished
                if (minIndex == -1)
                    break;

                // Write the smallest value to the output file
                streamWriter.WriteLine(minValue);

                // Read the next value from the temporary file
                string line = readers[minIndex].ReadLine();
                if (line != null)
                {
                    currentValues[minIndex] = int.Parse(line);
                }
                else
                {
                    finished[minIndex] = true;
                }
            }

            // Close all readers and delete temporary files
            for (int i = 0; i < tempFileCount; i++)
            {
                readers[i].Close();
                File.Delete($"temp_chunk_{i}.txt");
            }
        }
    }
}
Leave a Comment