نمایش نتایج 1 تا 3 از 3

نام تاپیک: مرتب سازی خارجی

  1. #1
    کاربر دائمی آواتار Pedram_Parsian
    تاریخ عضویت
    مرداد 1392
    محل زندگی
    پشت کامپیوتر
    پست
    430

    Lightbulb مرتب سازی خارجی

    با سلام

    می خواستم ببینم مرتب سازی خارجی چه جوری توی سی شارپ انجام میشه ؟

    ممنون میشم یه منبع یا سورس و ... معرفی کنید.

    با تشکر

  2. #2
    کاربر دائمی
    تاریخ عضویت
    دی 1383
    محل زندگی
    اصفهان
    پست
    1,436

    نقل قول: مرتب سازی خارجی (فوری)

    ظاهرا #C پیاده سازی ای برای این الگوریتم نداره. خودتون باید پیاده سازی کنید یا از پیاده سازی های بقیه استفاده کنید.
    برای مثال اینجا رو ببنید http://www.codeproject.com/Articles/...rnal-Sort-in-C

  3. #3
    کاربر دائمی آواتار khokhan
    تاریخ عضویت
    دی 1388
    محل زندگی
    اسکو
    پست
    2,176

    نقل قول: مرتب سازی خارجی (فوری)

    نقل قول نوشته شده توسط Pedram_Parsian مشاهده تاپیک
    با سلام

    می خواستم ببینم مرتب سازی خارجی چه جوری توی سی شارپ انجام میشه ؟

    ممنون میشم یه منبع یا سورس و ... معرفی کنید.

    با تشکر
    بدون شرح :

    ..............
    static void Split(string file)
    {
    int split_num = 1;
    StreamWriter sw = new StreamWriter(
    string.Format("c:\\split{0:d5}.dat", split_num));
    long read_line = 0;
    using (StreamReader sr = new StreamReader(file))
    {
    while (sr.Peek() >= 0)
    {
    // Progress reporting
    if (++read_line % 5000 == 0)
    Console.Write("{0:f2}% \r",
    100.0 * sr.BaseStream.Position / sr.BaseStream.Length);

    // Copy a line
    sw.WriteLine(sr.ReadLine());

    // If the file is big, then make a new split,
    // however if this was the last line then don't bother
    if (sw.BaseStream.Length > 100000000 && sr.Peek() >= 0)
    {
    sw.Close();
    split_num++;
    sw = new StreamWriter(
    string.Format("c:\\split{0:d5}.dat", split_num));
    }
    }
    }
    sw.Close();
    }


    .........

    static void SortTheChunks()
    {
    foreach (string path in Directory.GetFiles("C:\\", "split*.dat"))
    {
    // Read all lines into an array
    string[] contents = File.ReadAllLines(path);
    // Sort the in-memory array
    Array.Sort(contents);
    // Create the 'sorted' filename
    string newpath = path.Replace("split", "sorted");
    // Write it
    File.WriteAllLines(newpath, contents);
    // Delete the unsorted chunk
    File.Delete(path);
    // Free the in-memory sorted array
    contents = null;
    GC.Collect();
    }
    }


    ..................

    static void MergeTheChunks()
    {
    string[] paths = Directory.GetFiles("C:\\", "sorted*.dat");
    int chunks = paths.Length; // Number of chunks
    int recordsize = 100; // estimated record size
    int records = 10000000; // estimated total # records
    int maxusage = 500000000; // max memory usage
    int buffersize = maxusage / chunks; // bytes of each queue
    double recordoverhead = 7.5; // The overhead of using Queue<>
    int bufferlen = (int)(buffersize / recordsize /
    recordoverhead); // number of records in each queue

    // Open the files
    StreamReader[] readers = new StreamReader[chunks];
    for (int i = 0; i < chunks; i++)
    readers[i] = new StreamReader(paths[i]);

    // Make the queues
    Queue<string>[] queues = new Queue<string>[chunks];
    for (int i = 0; i < chunks; i++)
    queues[i] = new Queue<string>(bufferlen);

    // Load the queues
    for (int i = 0; i < chunks; i++)
    LoadQueue(queues[i], readers[i], bufferlen);

    // Merge!
    StreamWriter sw = new StreamWriter("C:\\BigFileSorted.txt");
    bool done = false;
    int lowest_index, j, progress = 0;
    string lowest_value;
    while (!done)
    {
    // Report the progress
    if (++progress % 5000 == 0)
    Console.Write("{0:f2}% \r",
    100.0 * progress / records);

    // Find the chunk with the lowest value
    lowest_index = -1;
    lowest_value = "";
    for (j = 0; j < chunks; j++)
    {
    if (queues[j] != null)
    {
    if (lowest_index < 0 ||
    String.CompareOrdinal(
    queues[j].Peek(), lowest_value) < 0)
    {
    lowest_index = j;
    lowest_value = queues[j].Peek();
    }
    }
    }

    // Was nothing found in any queue? We must be done then.
    if (lowest_index == -1) { done = true; break; }

    // Output it
    sw.WriteLine(lowest_value);

    // Remove from queue
    queues[lowest_index].Dequeue();
    // Have we emptied the queue? Top it up
    if (queues[lowest_index].Count == 0)
    {
    LoadQueue(queues[lowest_index],
    readers[lowest_index], bufferlen);
    // Was there nothing left to read?
    if (queues[lowest_index].Count == 0)
    {
    queues[lowest_index] = null;
    }
    }
    }
    sw.Close();

    // Close and delete the files
    for (int i = 0; i < chunks; i++)
    {
    readers[i].Close();
    File.Delete(paths[i]);
    }
    }

    static void LoadQueue(Queue<string> queue,
    StreamReader file, int records)
    {
    for (int i = 0; i < records; i++)
    {
    if (file.Peek() < 0) break;
    queue.Enqueue(file.ReadLine());
    }
    }

تاپیک های مشابه

  1. سوال: مرتب سازی داخلی/خارجی و درجا/ برون از جا
    نوشته شده توسط milad_d993 در بخش الگوریتم، کامپایلر، هوش مصنوعی و ساختمان داده ها
    پاسخ: 1
    آخرین پست: چهارشنبه 02 اردیبهشت 1394, 10:13 صبح
  2. مرتب سازی ادغام خارجی
    نوشته شده توسط hamid_22_7 در بخش الگوریتم، کامپایلر، هوش مصنوعی و ساختمان داده ها
    پاسخ: 1
    آخرین پست: چهارشنبه 12 بهمن 1390, 15:15 عصر
  3. مرتب سازی فیلد های فارسی
    نوشته شده توسط habdolah در بخش Foxpro
    پاسخ: 26
    آخرین پست: دوشنبه 22 آبان 1385, 14:09 عصر
  4. مرتب سازی
    نوشته شده توسط ghaum در بخش Access
    پاسخ: 2
    آخرین پست: شنبه 20 اسفند 1384, 23:56 عصر
  5. مرتب سازی
    نوشته شده توسط mostafa612003 در بخش SQL Server
    پاسخ: 5
    آخرین پست: پنج شنبه 27 آذر 1382, 16:39 عصر

قوانین ایجاد تاپیک در تالار

  • شما نمی توانید تاپیک جدید ایجاد کنید
  • شما نمی توانید به تاپیک ها پاسخ دهید
  • شما نمی توانید ضمیمه ارسال کنید
  • شما نمی توانید پاسخ هایتان را ویرایش کنید
  •