Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Numerics;
- using System.Threading.Tasks;
- using Excel = Microsoft.Office.Interop.Excel;
- using System.Threading;
- namespace lab2
- {
- internal class Program
- {
- static BigInteger res = new BigInteger(1);
- static System.Diagnostics.Stopwatch myStopwatch = new System.Diagnostics.Stopwatch();
- static Excel.Application excelApp = new Excel.Application();
- static string file_path = @"C:\Users\margo\source\repos\algorithm complexity analysis\ConsoleApp1\результаты2.xlsx";
- static Excel.Workbook wb = excelApp.Workbooks.Open(file_path);
- static Excel.Worksheet ws1 = wb.Worksheets["Лист1"];
- static void Swap(int[] array, int i, int j)
- {
- int temp = array[i];
- array[i] = array[j];
- array[j] = temp;
- }
- static void InsertionSort(int[] inArray)
- {
- int x;
- int j;
- for (int i = 1; i < inArray.Length; i++)
- {
- x = inArray[i];
- j = i;
- while (j > 0 && inArray[j - 1] > x)
- {
- Swap(inArray, j, j - 1);
- j -= 1;
- }
- inArray[j] = x;
- }
- }
- static Excel.SeriesCollection CreateChart(string algoName, ref int count, int st)
- {
- Excel.ChartObjects xlCharts = (Excel.ChartObjects)ws1.ChartObjects(Type.Missing);
- Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
- Excel.Chart chartPage = myChart.Chart;
- myChart.Select();
- chartPage.ChartType = Excel.XlChartType.xlXYScatterLines;
- Excel.SeriesCollection seriesCollection = chartPage.SeriesCollection();
- Excel.Series series1 = seriesCollection.NewSeries();
- series1.XValues = ws1.get_Range("B" + st, "B" + (count - 1));
- series1.Values = ws1.get_Range("C" + st, "C" + (count - 1));
- series1.Name = algoName;
- return seriesCollection;
- }
- static void CreateTable(string algoName, ref int count)
- {
- Excel.Range _excelCells1 = (Excel.Range)ws1.get_Range("A" + count, "D" + count).Cells;
- _excelCells1.Merge(Type.Missing);
- ws1.Cells[count, 1] = algoName;
- count++;
- ws1.Cells[count, 1].Value = "Время";
- ws1.Cells[count, 2].Value = "n";
- ws1.Cells[count, 3].Value = "Такты";
- }
- static int[] FillSortArray(int n)
- {
- int[] a = new int[n];
- for (int i = 0; i < n; i++)
- {
- a[i] = i;
- }
- return a;
- }
- static int[] FillRandomArray(int n)
- {
- int[] a = new int[n];
- var rnd = new Random();
- for (int i = 0; i < n; i++)
- {
- a[i] = rnd.Next(1, 2 * n);
- }
- return a;
- }
- static int[] FillUnsortArray(int n)
- {
- int[] a = new int[n];
- for (int i = 0; i < n; i++)
- {
- a[i] = n - 1 - i;
- }
- return a;
- }
- static public void quickSort1(int[] array, int leftIndex, int rightIndex)
- {
- var i = leftIndex;
- var j = rightIndex;
- var pivot = array[(leftIndex + rightIndex) / 2];
- while (i <= j)
- {
- while (array[i] < pivot)
- {
- i++;
- }
- while (array[j] > pivot)
- {
- j--;
- }
- if (i <= j)
- {
- int temp = array[i];
- array[i] = array[j];
- array[j] = temp;
- i++;
- j--;
- }
- }
- if (leftIndex < j)
- quickSort1(array, leftIndex, j);
- if (i < rightIndex)
- quickSort1(array, i, rightIndex);
- //return array;
- }
- static public void quickSort2(int[] array, int leftIndex, int rightIndex)
- {
- var i = leftIndex;
- var j = rightIndex;
- var pivot = array[1];
- while (i <= j)
- {
- while (array[i] < pivot)
- {
- i++;
- }
- while (array[j] > pivot)
- {
- j--;
- }
- if (i <= j)
- {
- int temp = array[i];
- array[i] = array[j];
- array[j] = temp;
- i++;
- j--;
- }
- }
- if (leftIndex < j)
- quickSort2(array, leftIndex, j);
- if (i < rightIndex)
- quickSort2(array, i, rightIndex);
- //return array;
- }
- //static void quickSort2(int[] array, int start, int end)
- //{
- // int i = start, j = end-1; // поставить указатели на исходные места
- // int p = array[1];
- // int temp;
- // // процедура разделения
- // do
- // {
- // while (array[i] < p) i++;
- // while (array[j] > p) j--;
- // if (i <= j)
- // {
- // temp = array[i]; array[i] = array[j]; array[j] = temp;
- // i++; j--;
- // }
- // } while (i <= j);
- // if (j > 0) quickSort2(array, start, j);
- // if (end > i) quickSort2(array, start + i, end-i);
- //}
- static void Show(int[] a)
- {
- foreach (var item in a)
- {
- Console.Write( item + " " );
- }
- }
- static void Main(string[] args)
- {
- int stackSize = 1024 * 1024 * 256;
- Thread th = new Thread(() =>
- {
- excelApp.Visible = true;
- int n; int count = 1;
- string algoName = "Сортировка простыми вставками лучший случай";
- //Console.WriteLine(algoName);
- CreateTable(algoName, ref count);
- int st = count + 1;
- ++count;
- int[] a;
- for (n = 10; n <= 10000; n++)
- {
- // a = FillSortArray(n);
- // myStopwatch = System.Diagnostics.Stopwatch.StartNew();
- // InsertionSort(a);
- // myStopwatch.Stop();
- // var result = myStopwatch.Elapsed;
- // var resultTime = myStopwatch.ElapsedTicks;
- // string elapsedTime = result.ToString(@"hh\:mm\:ss\:fff");
- // Console.WriteLine("RunTime " + resultTime + "n = " + n);
- // ws1.Cells[count, 1].Value = elapsedTime;
- // ws1.Cells[count, 2].Value = n;
- // ws1.Cells[count, 3].Value = resultTime;
- count++;
- res = 1;
- }
- //Excel.SeriesCollection seriesCollection1 = CreateChart(algoName, ref count, st);
- count += 2;
- algoName = "Быстрая сортировка лучший случай";
- CreateTable(algoName, ref count); st = count + 1;
- ++count;
- //Console.WriteLine(algoName);
- for (n = 10; n <= 10000; n++)
- {
- //a = FillSortArray(n);
- //myStopwatch = System.Diagnostics.Stopwatch.StartNew();
- //quickSort1(a, 0, n - 1);
- //myStopwatch.Stop();
- //var result = myStopwatch.Elapsed;
- //var resultTime = myStopwatch.ElapsedTicks;
- //var resultms = myStopwatch.ElapsedMilliseconds;
- //string elapsedTime = result.ToString(@"hh\:mm\:ss\:fff");
- //Console.WriteLine("RunTime " + resultTime + " " + "n =" + n);
- //ws1.Cells[count, 1].Value = elapsedTime;
- //ws1.Cells[count, 2].Value = n;
- //ws1.Cells[count, 3].Value = resultTime;
- count++;
- res = 1;
- }
- //Excel.Series series2 = seriesCollection1.NewSeries();
- //series2.XValues = ws1.get_Range("B" + st, "B" + (count - 1));
- //series2.Values = ws1.get_Range("C" + st, "C" + (count - 1));
- //series2.Name = algoName;
- count += 2;
- algoName = "Встроенная сортировка лучший случай"; Console.WriteLine(algoName);
- CreateTable(algoName, ref count); st = count + 1;
- ++count;
- for (n = 10; n <= 10000; n++)
- {
- //a = FillSortArray(n);
- //myStopwatch = System.Diagnostics.Stopwatch.StartNew();
- //Array.Sort(a);
- //myStopwatch.Stop();
- //var result = myStopwatch.Elapsed;
- //var resultTime = myStopwatch.ElapsedTicks;
- //var resultms = myStopwatch.ElapsedMilliseconds;
- //string elapsedTime = result.ToString(@"hh\:mm\:ss\:fff");
- //Console.WriteLine("RunTime " + resultTime + " " + "n = " + n);
- //ws1.Cells[count, 1].Value = elapsedTime;
- //ws1.Cells[count, 2].Value = n;
- //ws1.Cells[count, 3].Value = resultTime;
- count++;
- res = 1;
- }
- //Excel.Series series3 = seriesCollection1.NewSeries();
- //series3.XValues = ws1.get_Range("B" + st, "B" + (count - 1));
- //series3.Values = ws1.get_Range("C" + st, "C" + (count - 1));
- //series3.Name = algoName;
- count += 2;
- algoName = "Сортировка простыми вставками худший случай";
- Console.WriteLine(algoName);
- CreateTable(algoName, ref count);
- st = count + 1;
- ++count;
- for (n = 10; n <= 10000; n++)
- {
- //a = FillUnsortArray(n);
- //myStopwatch = System.Diagnostics.Stopwatch.StartNew();
- //InsertionSort(a);
- //myStopwatch.Stop();
- //var result = myStopwatch.Elapsed;
- //var resultTime = myStopwatch.ElapsedTicks;
- //var resultms = myStopwatch.ElapsedMilliseconds;
- //string elapsedTime = result.ToString(@"hh\:mm\:ss\:fff");
- //Console.WriteLine("RunTime " + resultTime + "n = " + n);
- //ws1.Cells[count, 1].Value = elapsedTime;
- //ws1.Cells[count, 2].Value = n;
- //ws1.Cells[count, 3].Value = resultTime;
- count++;
- res = 1;
- }
- //Excel.SeriesCollection seriesCollection2 = CreateChart(algoName, ref count, st);
- count += 2;
- algoName = "Быстрая сортировка худший случай";
- CreateTable(algoName, ref count); st = count + 1;
- ++count;
- Console.WriteLine(algoName);
- for (n = 10; n <= 10000; n++)
- {
- //a = FillSortArray(n);
- //myStopwatch = System.Diagnostics.Stopwatch.StartNew();
- //quickSort2(a, 0, n - 1);
- //Console.WriteLine();
- //myStopwatch.Stop();
- //var result = myStopwatch.Elapsed;
- //var resultTime = myStopwatch.ElapsedTicks;
- //var resultms = myStopwatch.ElapsedMilliseconds;
- //string elapsedTime = result.ToString(@"hh\:mm\:ss\:fff");
- //Console.WriteLine("RunTime " + resultTime + " " + "n =" + n);
- //ws1.Cells[count, 1].Value = elapsedTime;
- //ws1.Cells[count, 2].Value = n;
- //ws1.Cells[count, 3].Value = resultTime;
- count++;
- res = 1;
- }
- //Excel.Series series4 = seriesCollection2.NewSeries();
- //series4.XValues = ws1.get_Range("B" + st, "B" + (count - 1));
- //series4.Values = ws1.get_Range("C" + st, "C" + (count - 1));
- //series4.Name = algoName;
- count += 2;
- algoName = "Встроенная сортировка худший случай"; Console.WriteLine(algoName);
- CreateTable(algoName, ref count); st = count + 1;
- ++count;
- for (n = 10; n <= 10000; n++)
- {
- //a = FillUnsortArray(n);
- //myStopwatch = System.Diagnostics.Stopwatch.StartNew();
- //Array.Sort(a);
- //myStopwatch.Stop();
- //var result = myStopwatch.Elapsed;
- //var resultTime = myStopwatch.ElapsedTicks;
- //var resultms = myStopwatch.ElapsedMilliseconds;
- //string elapsedTime = result.ToString(@"hh\:mm\:ss\:fff");
- //Console.WriteLine("RunTime " + resultTime + " " + "n = " + n);
- //ws1.Cells[count, 1].Value = elapsedTime;
- //ws1.Cells[count, 2].Value = n;
- //ws1.Cells[count, 3].Value = resultTime;
- count++;
- res = 1;
- }
- //Excel.Series series5 = seriesCollection2.NewSeries();
- //series5.XValues = ws1.get_Range("B" + st, "B" + (count - 1));
- //series5.Values = ws1.get_Range("C" + st, "C" + (count - 1));
- //series5.Name = algoName;
- count += 2;
- algoName = "Сортировка простыми вставками средний случай";
- Console.WriteLine(algoName);
- CreateTable(algoName, ref count);
- st = count + 1;
- ++count;
- long resultTicks=0;
- for ( n = 10; n <= 10000; n++)
- {
- //for (int k = 0; k < 5; k++)
- //{
- // a = FillRandomArray(n);
- // // Show(a);
- // // Console.WriteLine();
- // myStopwatch = System.Diagnostics.Stopwatch.StartNew();
- // InsertionSort(a);
- // myStopwatch.Stop();
- // resultTicks+= myStopwatch.ElapsedTicks;
- //}
- //var result = myStopwatch.Elapsed;
- //var resultTime = resultTicks/5;
- //var resultms = myStopwatch.ElapsedMilliseconds;
- //string elapsedTime = result.ToString(@"hh\:mm\:ss\:fff");
- //Console.WriteLine("RunTime " + resultTime + "n = " + n);
- //ws1.Cells[count, 1].Value = elapsedTime;
- //ws1.Cells[count, 2].Value = n;
- //ws1.Cells[count, 3].Value = resultTime;
- count++;
- res = 1;
- }
- Excel.SeriesCollection seriesCollection3 = CreateChart(algoName, ref count, st);
- count += 2;
- algoName = "Быстрая сортировка средний случай";
- CreateTable(algoName, ref count); st = count + 1;
- ++count;
- Console.WriteLine(algoName);
- resultTicks = 0;
- resultTicks = 0;
- for (n = 10; n <= 10000; n++)
- {
- for (int k = 0; k < 5; k++)
- {
- a = FillRandomArray(n);
- // Show(a);
- // Console.WriteLine();
- myStopwatch = System.Diagnostics.Stopwatch.StartNew();
- quickSort1(a, 0, n - 1);
- myStopwatch.Stop();
- resultTicks += myStopwatch.ElapsedTicks;
- }
- var result = myStopwatch.Elapsed;
- var resultTime = resultTicks / 5;
- var resultms = myStopwatch.ElapsedMilliseconds;
- string elapsedTime = result.ToString(@"hh\:mm\:ss\:fff");
- Console.WriteLine("RunTime " + resultTime + " " + "n =" + n);
- ws1.Cells[count, 1].Value = elapsedTime;
- ws1.Cells[count, 2].Value = n;
- ws1.Cells[count, 3].Value = resultTime;
- count++;
- res = 1;
- }
- Excel.Series series6 = seriesCollection3.NewSeries();
- series6.XValues = ws1.get_Range("B" + st, "B" + (count - 1));
- series6.Values = ws1.get_Range("C" + st, "C" + (count - 1));
- series6.Name = algoName;
- count += 2;
- algoName = "Встроенная сортировка средний случай"; Console.WriteLine(algoName);
- CreateTable(algoName, ref count); st = count + 1;
- ++count;
- resultTicks = 0;
- for (n = 10; n <= 10000; n++)
- {
- for (int k = 0; k < 5; k++)
- {
- a = FillRandomArray(n);
- myStopwatch = System.Diagnostics.Stopwatch.StartNew();
- Array.Sort(a);
- myStopwatch.Stop();
- resultTicks += myStopwatch.ElapsedTicks;
- }
- var result = myStopwatch.Elapsed;
- var resultTime = resultTicks/5;
- var resultms = myStopwatch.ElapsedMilliseconds;
- string elapsedTime = result.ToString(@"hh\:mm\:ss\:fff");
- Console.WriteLine("RunTime " + resultTime + " " + "n = " + n);
- ws1.Cells[count, 1].Value = elapsedTime;
- ws1.Cells[count, 2].Value = n;
- ws1.Cells[count, 3].Value = resultTime;
- count++;
- res = 1;
- }
- Excel.Series series7 = seriesCollection3.NewSeries();
- series7.XValues = ws1.get_Range("B" + st, "B" + (count - 1));
- series7.Values = ws1.get_Range("C" + st, "C" + (count - 1));
- series7.Name = algoName;
- count += 2;
- },
- stackSize);
- th.Start();
- th.Join();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement