Advertisement
maxlarin2

CombAlg 1.1 2.0

Feb 17th, 2014
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.08 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Diagnostics;
  8. using System.Text.RegularExpressions;
  9. namespace selection_sort
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.  
  16.             string[] findFiles = Directory.GetFiles(@"F:\ИТМО\Комбинаторные алгоритмы\Сортировка\F_Sort");                              // список всех файлов директории - в массив
  17.             for (int q = 0; q  < findFiles.Length; q++)                // цикл пробега по всем файлам директории
  18.             {                                                                                    
  19.                 {
  20.                     StreamReader file = new StreamReader(findFiles[q]);                                                                 // открываем файл
  21.                     int n = Convert.ToInt16(Path.GetFileName(findFiles[q]).Remove(Path.GetFileName(findFiles[q]).IndexOf('_')));        // длина массива
  22.                     string s = file.ReadToEnd();                                                                                        
  23.                     int[] mas = new int[n];
  24.                     string[] buf;
  25.  
  26.                     buf = s.Split();
  27.                     int k = 0;
  28.                     for (int i = 0; i < buf.Length; i++)
  29.                     {
  30.                         if (buf[i] != "")
  31.                         {
  32.                             mas[k] = Convert.ToInt16(buf[i]);
  33.                             k++;
  34.                         }
  35.                     }
  36.                     k = 0;                                                                                                              // считывание чисел в массив
  37.  
  38.                     Stopwatch time = new Stopwatch();                                                                                  
  39.                     time.Start();
  40.                     int m = 0, min;
  41.                     for (int i = 0; i < (mas.Length - 1); i++)
  42.                     {
  43.                         m = i; min = mas[m];
  44.                         for (int j = i + 1; j < mas.Length; j++)
  45.                         {
  46.                             if (mas[j] < min)
  47.                             {
  48.                                 min = mas[j];
  49.                                 m = j;
  50.                             }
  51.                         }
  52.                         mas[m] = mas[i];
  53.                         mas[i] = min;
  54.                     }
  55.                     time.Stop();                                                                                                        // замер времени сортировки
  56.                    
  57.                     bool is_true = true;
  58.                     for (int i = 1; i < mas.Length - 1; i++)
  59.                     {
  60.                         if (mas[i] > mas[i - 1])
  61.                         { is_true = true; }
  62.                     }                                                                                                                   // проверка на упорядоченность
  63.  
  64.                     using (StreamWriter Print = new StreamWriter(findFiles[q] + "_selection_sort_result"))
  65.                     {
  66.                         for (int i = 0; i < mas.Length; i++)
  67.                         {
  68.                             Print.Write("{0,5}", mas[i]);
  69.                         }
  70.                         if (is_true == true)
  71.                             Print.WriteLine("\nМассив отсортирован!");
  72.                         else Print.WriteLine("Массив не отсортирован!");
  73.                         Print.WriteLine(time.ElapsedTicks);
  74.                     }                                                                                                                   // создание файла результата
  75.  
  76.                 }
  77.             }
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement