Advertisement
Margoshinka

govno

Oct 13th, 2022
1,128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 19.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Numerics;
  6. using System.Threading.Tasks;
  7. using Excel = Microsoft.Office.Interop.Excel;
  8. using System.Threading;
  9. namespace lab2
  10. {
  11.     internal class Program
  12.     {
  13.         static BigInteger res = new BigInteger(1);
  14.         static System.Diagnostics.Stopwatch myStopwatch = new System.Diagnostics.Stopwatch();
  15.         static Excel.Application excelApp = new Excel.Application();
  16.  
  17.         static string file_path = @"C:\Users\margo\source\repos\algorithm complexity analysis\ConsoleApp1\результаты2.xlsx";
  18.         static Excel.Workbook wb = excelApp.Workbooks.Open(file_path);
  19.         static Excel.Worksheet ws1 = wb.Worksheets["Лист1"];
  20.  
  21.  
  22.         static void Swap(int[] array, int i, int j)
  23.         {
  24.             int temp = array[i];
  25.             array[i] = array[j];
  26.             array[j] = temp;
  27.         }
  28.         static void InsertionSort(int[] inArray)
  29.         {
  30.             int x;
  31.             int j;
  32.             for (int i = 1; i < inArray.Length; i++)
  33.             {
  34.                 x = inArray[i];
  35.                 j = i;
  36.                 while (j > 0 && inArray[j - 1] > x)
  37.                 {
  38.                     Swap(inArray, j, j - 1);
  39.                     j -= 1;
  40.                 }
  41.                 inArray[j] = x;
  42.             }
  43.         }
  44.         static Excel.SeriesCollection CreateChart(string algoName, ref int count, int st)
  45.         {
  46.             Excel.ChartObjects xlCharts = (Excel.ChartObjects)ws1.ChartObjects(Type.Missing);
  47.             Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
  48.             Excel.Chart chartPage = myChart.Chart;
  49.             myChart.Select();
  50.  
  51.             chartPage.ChartType = Excel.XlChartType.xlXYScatterLines;
  52.  
  53.             Excel.SeriesCollection seriesCollection = chartPage.SeriesCollection();
  54.  
  55.             Excel.Series series1 = seriesCollection.NewSeries();
  56.             series1.XValues = ws1.get_Range("B" + st, "B" + (count - 1));
  57.             series1.Values = ws1.get_Range("C" + st, "C" + (count - 1));
  58.             series1.Name = algoName;
  59.  
  60.             return seriesCollection;
  61.         }
  62.         static void CreateTable(string algoName, ref int count)
  63.         {
  64.             Excel.Range _excelCells1 = (Excel.Range)ws1.get_Range("A" + count, "D" + count).Cells;
  65.  
  66.             _excelCells1.Merge(Type.Missing);
  67.             ws1.Cells[count, 1] = algoName;
  68.             count++;
  69.             ws1.Cells[count, 1].Value = "Время";
  70.             ws1.Cells[count, 2].Value = "n";
  71.             ws1.Cells[count, 3].Value = "Такты";
  72.         }
  73.         static int[] FillSortArray(int n)
  74.         {
  75.             int[] a = new int[n];
  76.             for (int i = 0; i < n; i++)
  77.             {
  78.                 a[i] = i;
  79.             }
  80.             return a;
  81.         }
  82.         static int[] FillRandomArray(int n)
  83.         {
  84.             int[] a = new int[n];
  85.             var rnd = new Random();
  86.             for (int i = 0; i < n; i++)
  87.             {
  88.                 a[i] = rnd.Next(1, 2 * n);
  89.             }
  90.             return a;
  91.         }
  92.         static int[] FillUnsortArray(int n)
  93.         {
  94.             int[] a = new int[n];
  95.             for (int i = 0; i < n; i++)
  96.             {
  97.                 a[i] = n - 1 - i;
  98.             }
  99.             return a;
  100.         }
  101.         static public void quickSort1(int[] array, int leftIndex, int rightIndex)
  102.         {
  103.             var i = leftIndex;
  104.             var j = rightIndex;
  105.             var pivot = array[(leftIndex + rightIndex) / 2];
  106.             while (i <= j)
  107.             {
  108.                 while (array[i] < pivot)
  109.                 {
  110.                     i++;
  111.                 }
  112.  
  113.                 while (array[j] > pivot)
  114.                 {
  115.                     j--;
  116.                 }
  117.                 if (i <= j)
  118.                 {
  119.                     int temp = array[i];
  120.                     array[i] = array[j];
  121.                     array[j] = temp;
  122.                     i++;
  123.                     j--;
  124.                 }
  125.             }
  126.  
  127.             if (leftIndex < j)
  128.                 quickSort1(array, leftIndex, j);
  129.             if (i < rightIndex)
  130.                 quickSort1(array, i, rightIndex);
  131.             //return array;
  132.         }
  133.  
  134.         static public void quickSort2(int[] array, int leftIndex, int rightIndex)
  135.         {
  136.             var i = leftIndex;
  137.             var j = rightIndex;
  138.             var pivot = array[1];
  139.             while (i <= j)
  140.             {
  141.                 while (array[i] < pivot)
  142.                 {
  143.                     i++;
  144.                 }
  145.  
  146.                 while (array[j] > pivot)
  147.                 {
  148.                     j--;
  149.                 }
  150.                 if (i <= j)
  151.                 {
  152.                     int temp = array[i];
  153.                     array[i] = array[j];
  154.                     array[j] = temp;
  155.                     i++;
  156.                     j--;
  157.                 }
  158.             }
  159.  
  160.             if (leftIndex < j)
  161.                 quickSort2(array, leftIndex, j);
  162.             if (i < rightIndex)
  163.                 quickSort2(array, i, rightIndex);
  164.             //return array;
  165.         }
  166.         //static void quickSort2(int[] array, int start, int end)
  167.         //{
  168.  
  169.  
  170.         //    int i = start, j = end-1;      // поставить указатели на исходные места
  171.  
  172.         //    int p = array[1];
  173.         //    int temp;
  174.  
  175.         //    // процедура разделения
  176.         //    do
  177.         //    {
  178.         //        while (array[i] < p) i++;
  179.         //        while (array[j] > p) j--;
  180.  
  181.         //        if (i <= j)
  182.         //        {
  183.         //            temp = array[i]; array[i] = array[j]; array[j] = temp;
  184.         //            i++; j--;
  185.         //        }
  186.         //    } while (i <= j);
  187.  
  188.         //    if (j > 0) quickSort2(array, start, j);
  189.         //    if (end > i) quickSort2(array, start + i, end-i);
  190.            
  191.         //}
  192.         static void Show(int[] a)
  193.         {
  194.             foreach (var item in a)
  195.             {
  196.                 Console.Write( item + " " );
  197.             }
  198.         }
  199.  
  200.  
  201.         static void Main(string[] args)
  202.         {
  203.             int stackSize = 1024 * 1024 * 256;
  204.             Thread th = new Thread(() =>
  205.             {
  206.  
  207.  
  208.                 excelApp.Visible = true;
  209.  
  210.                 int n; int count = 1;
  211.                 string algoName = "Сортировка простыми вставками лучший случай";
  212.                 //Console.WriteLine(algoName);
  213.                 CreateTable(algoName, ref count);
  214.                 int st = count + 1;
  215.                 ++count;
  216.                 int[] a;
  217.                 for (n = 10; n <= 10000; n++)
  218.                 {
  219.                 //    a = FillSortArray(n);
  220.                 //    myStopwatch = System.Diagnostics.Stopwatch.StartNew();
  221.                 //    InsertionSort(a);
  222.                 //    myStopwatch.Stop();
  223.                 //    var result = myStopwatch.Elapsed;
  224.                 //    var resultTime = myStopwatch.ElapsedTicks;
  225.  
  226.                 //    string elapsedTime = result.ToString(@"hh\:mm\:ss\:fff");
  227.  
  228.  
  229.                 //    Console.WriteLine("RunTime " + resultTime + "n = " + n);
  230.                 //    ws1.Cells[count, 1].Value = elapsedTime;
  231.                 //    ws1.Cells[count, 2].Value = n;
  232.                 //    ws1.Cells[count, 3].Value = resultTime;
  233.  
  234.                     count++;
  235.                     res = 1;
  236.                 }
  237.                 //Excel.SeriesCollection seriesCollection1 = CreateChart(algoName, ref count, st);
  238.  
  239.  
  240.  
  241.                 count += 2;
  242.  
  243.                 algoName = "Быстрая сортировка лучший случай";
  244.                 CreateTable(algoName, ref count); st = count + 1;
  245.                 ++count;
  246.                 //Console.WriteLine(algoName);
  247.                 for (n = 10; n <= 10000; n++)
  248.                 {
  249.                     //a = FillSortArray(n);
  250.                     //myStopwatch = System.Diagnostics.Stopwatch.StartNew();
  251.                     //quickSort1(a, 0, n - 1);
  252.  
  253.                     //myStopwatch.Stop();
  254.                     //var result = myStopwatch.Elapsed;
  255.                     //var resultTime = myStopwatch.ElapsedTicks;
  256.                     //var resultms = myStopwatch.ElapsedMilliseconds;
  257.                     //string elapsedTime = result.ToString(@"hh\:mm\:ss\:fff");
  258.  
  259.  
  260.                     //Console.WriteLine("RunTime " + resultTime + " " + "n =" + n);
  261.                     //ws1.Cells[count, 1].Value = elapsedTime;
  262.                     //ws1.Cells[count, 2].Value = n;
  263.                     //ws1.Cells[count, 3].Value = resultTime;
  264.                     count++;
  265.                     res = 1;
  266.                 }
  267.  
  268.                 //Excel.Series series2 = seriesCollection1.NewSeries();
  269.                 //series2.XValues = ws1.get_Range("B" + st, "B" + (count - 1));
  270.                 //series2.Values = ws1.get_Range("C" + st, "C" + (count - 1));
  271.                 //series2.Name = algoName;
  272.  
  273.                 count += 2;
  274.  
  275.                 algoName = "Встроенная сортировка лучший случай"; Console.WriteLine(algoName);
  276.                 CreateTable(algoName, ref count); st = count + 1;
  277.                 ++count;
  278.  
  279.                 for (n = 10; n <= 10000; n++)
  280.                 {
  281.                     //a = FillSortArray(n);
  282.  
  283.  
  284.                     //myStopwatch = System.Diagnostics.Stopwatch.StartNew();
  285.  
  286.                     //Array.Sort(a);
  287.                     //myStopwatch.Stop();
  288.                     //var result = myStopwatch.Elapsed;
  289.                     //var resultTime = myStopwatch.ElapsedTicks;
  290.                     //var resultms = myStopwatch.ElapsedMilliseconds;
  291.                     //string elapsedTime = result.ToString(@"hh\:mm\:ss\:fff");
  292.  
  293.  
  294.  
  295.                     //Console.WriteLine("RunTime " + resultTime + " " + "n = " + n);
  296.                     //ws1.Cells[count, 1].Value = elapsedTime;
  297.                     //ws1.Cells[count, 2].Value = n;
  298.                     //ws1.Cells[count, 3].Value = resultTime;
  299.  
  300.                     count++;
  301.                     res = 1;
  302.                 }
  303.  
  304.                 //Excel.Series series3 = seriesCollection1.NewSeries();
  305.                 //series3.XValues = ws1.get_Range("B" + st, "B" + (count - 1));
  306.                 //series3.Values = ws1.get_Range("C" + st, "C" + (count - 1));
  307.                 //series3.Name = algoName;
  308.                 count += 2;
  309.  
  310.  
  311.                 algoName = "Сортировка простыми вставками худший случай";
  312.                 Console.WriteLine(algoName);
  313.                 CreateTable(algoName, ref count);
  314.                 st = count + 1;
  315.                 ++count;
  316.  
  317.                 for (n = 10; n <= 10000; n++)
  318.                 {
  319.                     //a = FillUnsortArray(n);
  320.                     //myStopwatch = System.Diagnostics.Stopwatch.StartNew();
  321.                     //InsertionSort(a);
  322.                     //myStopwatch.Stop();
  323.                     //var result = myStopwatch.Elapsed;
  324.                     //var resultTime = myStopwatch.ElapsedTicks;
  325.                     //var resultms = myStopwatch.ElapsedMilliseconds;
  326.                     //string elapsedTime = result.ToString(@"hh\:mm\:ss\:fff");
  327.  
  328.  
  329.                     //Console.WriteLine("RunTime " + resultTime + "n = " + n);
  330.                     //ws1.Cells[count, 1].Value = elapsedTime;
  331.                     //ws1.Cells[count, 2].Value = n;
  332.                     //ws1.Cells[count, 3].Value = resultTime;
  333.  
  334.                     count++;
  335.                     res = 1;
  336.                 }
  337.                 //Excel.SeriesCollection seriesCollection2 = CreateChart(algoName, ref count, st);
  338.  
  339.  
  340.  
  341.                 count += 2;
  342.  
  343.                 algoName = "Быстрая сортировка худший случай";
  344.                 CreateTable(algoName, ref count); st = count + 1;
  345.                 ++count;
  346.                 Console.WriteLine(algoName);
  347.                 for (n = 10; n <= 10000; n++)
  348.                 {
  349.                     //a = FillSortArray(n);
  350.                     //myStopwatch = System.Diagnostics.Stopwatch.StartNew();
  351.                     //quickSort2(a, 0, n - 1);
  352.  
  353.                     //Console.WriteLine();
  354.                     //myStopwatch.Stop();
  355.                     //var result = myStopwatch.Elapsed;
  356.                     //var resultTime = myStopwatch.ElapsedTicks;
  357.                     //var resultms = myStopwatch.ElapsedMilliseconds;
  358.                     //string elapsedTime = result.ToString(@"hh\:mm\:ss\:fff");
  359.  
  360.  
  361.                     //Console.WriteLine("RunTime " + resultTime + " " + "n =" + n);
  362.                     //ws1.Cells[count, 1].Value = elapsedTime;
  363.                     //ws1.Cells[count, 2].Value = n;
  364.                     //ws1.Cells[count, 3].Value = resultTime;
  365.  
  366.                     count++;
  367.                     res = 1;
  368.                 }
  369.  
  370.                 //Excel.Series series4 = seriesCollection2.NewSeries();
  371.                 //series4.XValues = ws1.get_Range("B" + st, "B" + (count - 1));
  372.                 //series4.Values = ws1.get_Range("C" + st, "C" + (count - 1));
  373.                 //series4.Name = algoName;
  374.  
  375.                 count += 2;
  376.  
  377.                 algoName = "Встроенная сортировка худший случай"; Console.WriteLine(algoName);
  378.                 CreateTable(algoName, ref count); st = count + 1;
  379.                 ++count;
  380.  
  381.                 for (n = 10; n <= 10000; n++)
  382.                 {
  383.                     //a = FillUnsortArray(n);
  384.  
  385.  
  386.                     //myStopwatch = System.Diagnostics.Stopwatch.StartNew();
  387.  
  388.                     //Array.Sort(a);
  389.                     //myStopwatch.Stop();
  390.                     //var result = myStopwatch.Elapsed;
  391.                     //var resultTime = myStopwatch.ElapsedTicks;
  392.                     //var resultms = myStopwatch.ElapsedMilliseconds;
  393.                     //string elapsedTime = result.ToString(@"hh\:mm\:ss\:fff");
  394.  
  395.  
  396.  
  397.                     //Console.WriteLine("RunTime " + resultTime + " " + "n = " + n);
  398.                     //ws1.Cells[count, 1].Value = elapsedTime;
  399.                     //ws1.Cells[count, 2].Value = n;
  400.                     //ws1.Cells[count, 3].Value = resultTime;
  401.  
  402.                     count++;
  403.                     res = 1;
  404.                 }
  405.  
  406.                 //Excel.Series series5 = seriesCollection2.NewSeries();
  407.                 //series5.XValues = ws1.get_Range("B" + st, "B" + (count - 1));
  408.                 //series5.Values = ws1.get_Range("C" + st, "C" + (count - 1));
  409.                 //series5.Name = algoName;
  410.                 count += 2;
  411.  
  412.  
  413.  
  414.  
  415.                 algoName = "Сортировка простыми вставками средний случай";
  416.                 Console.WriteLine(algoName);
  417.                 CreateTable(algoName, ref count);
  418.                 st = count + 1;
  419.                 ++count;
  420.                  long resultTicks=0;
  421.                 for ( n = 10; n <= 10000; n++)
  422.                 {
  423.                     //for (int k = 0; k < 5; k++)
  424.                     //{
  425.  
  426.  
  427.                     //    a = FillRandomArray(n);
  428.                     //    // Show(a);
  429.                     //    // Console.WriteLine();
  430.                     //    myStopwatch = System.Diagnostics.Stopwatch.StartNew();
  431.                     //    InsertionSort(a);
  432.                     //    myStopwatch.Stop();
  433.                     //    resultTicks+= myStopwatch.ElapsedTicks;
  434.                     //}
  435.                     //var result = myStopwatch.Elapsed;
  436.                     //var resultTime = resultTicks/5;
  437.                     //var resultms = myStopwatch.ElapsedMilliseconds;
  438.                     //string elapsedTime = result.ToString(@"hh\:mm\:ss\:fff");
  439.  
  440.  
  441.                     //Console.WriteLine("RunTime " + resultTime + "n = " + n);
  442.                     //ws1.Cells[count, 1].Value = elapsedTime;
  443.                     //ws1.Cells[count, 2].Value = n;
  444.                     //ws1.Cells[count, 3].Value = resultTime;
  445.  
  446.                     count++;
  447.                     res = 1;
  448.                 }
  449.                 Excel.SeriesCollection seriesCollection3 = CreateChart(algoName, ref count, st);
  450.  
  451.  
  452.  
  453.                 count += 2;
  454.  
  455.                 algoName = "Быстрая сортировка средний случай";
  456.                 CreateTable(algoName, ref count); st = count + 1;
  457.                 ++count;
  458.                 Console.WriteLine(algoName);
  459.                 resultTicks = 0;
  460.                 resultTicks = 0;
  461.                 for (n = 10; n <= 10000; n++)
  462.                 {
  463.                     for (int k = 0; k < 5; k++)
  464.                         {
  465.  
  466.  
  467.                         a = FillRandomArray(n);
  468.                         // Show(a);
  469.                         // Console.WriteLine();
  470.                         myStopwatch = System.Diagnostics.Stopwatch.StartNew();
  471.                         quickSort1(a, 0, n - 1);
  472.                        
  473.                         myStopwatch.Stop();
  474.                         resultTicks += myStopwatch.ElapsedTicks;
  475.                     }
  476.                    
  477.                    
  478.                    
  479.                     var result = myStopwatch.Elapsed;
  480.                     var resultTime = resultTicks / 5;
  481.                     var resultms = myStopwatch.ElapsedMilliseconds;
  482.                     string elapsedTime = result.ToString(@"hh\:mm\:ss\:fff");
  483.  
  484.  
  485.                     Console.WriteLine("RunTime " + resultTime + " " + "n =" + n);
  486.                     ws1.Cells[count, 1].Value = elapsedTime;
  487.                     ws1.Cells[count, 2].Value = n;
  488.                     ws1.Cells[count, 3].Value = resultTime;
  489.  
  490.                     count++;
  491.                     res = 1;
  492.                 }
  493.  
  494.                 Excel.Series series6 = seriesCollection3.NewSeries();
  495.                 series6.XValues = ws1.get_Range("B" + st, "B" + (count - 1));
  496.                 series6.Values = ws1.get_Range("C" + st, "C" + (count - 1));
  497.                 series6.Name = algoName;
  498.  
  499.                 count += 2;
  500.  
  501.                 algoName = "Встроенная сортировка средний случай"; Console.WriteLine(algoName);
  502.                 CreateTable(algoName, ref count); st = count + 1;
  503.                 ++count;
  504.                 resultTicks = 0;
  505.                 for (n = 10; n <= 10000; n++)
  506.                 {
  507.                     for (int k = 0; k < 5; k++)
  508.                     {
  509.                         a = FillRandomArray(n);
  510.  
  511.  
  512.                         myStopwatch = System.Diagnostics.Stopwatch.StartNew();
  513.  
  514.                         Array.Sort(a);
  515.                         myStopwatch.Stop();
  516.                         resultTicks += myStopwatch.ElapsedTicks;
  517.                     }
  518.                    
  519.                     var result = myStopwatch.Elapsed;
  520.                     var resultTime = resultTicks/5;
  521.                     var resultms = myStopwatch.ElapsedMilliseconds;
  522.                     string elapsedTime = result.ToString(@"hh\:mm\:ss\:fff");
  523.  
  524.  
  525.  
  526.                     Console.WriteLine("RunTime " + resultTime + " " + "n = " + n);
  527.                     ws1.Cells[count, 1].Value = elapsedTime;
  528.                     ws1.Cells[count, 2].Value = n;
  529.                     ws1.Cells[count, 3].Value = resultTime;
  530.  
  531.                     count++;
  532.                     res = 1;
  533.                 }
  534.  
  535.                 Excel.Series series7 = seriesCollection3.NewSeries();
  536.                 series7.XValues = ws1.get_Range("B" + st, "B" + (count - 1));
  537.                 series7.Values = ws1.get_Range("C" + st, "C" + (count - 1));
  538.                 series7.Name = algoName;
  539.                 count += 2;
  540.  
  541.             },
  542.                 stackSize);
  543.  
  544.             th.Start();
  545.             th.Join();
  546.         }
  547.     }
  548. }
  549.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement