From 5cb0edc84a1f240186963e56437598f79f715c0b Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Fri, 24 Dec 2021 16:15:53 +0300 Subject: [PATCH 1/7] test --- QSort/QSort.sln | 22 ++++++++++ QSort/QSort/Program.cs | 65 +++++++++++++++++++++++++++ QSort/QSort/QSort.cs | 75 ++++++++++++++++++++++++++++++++ QSort/QSort/QSort.csproj | 12 +++++ QSort/QSort/Statistics.cs | 75 ++++++++++++++++++++++++++++++++ QSort/QSortTest/QSortTest.cs | 45 +++++++++++++++++++ QSort/QSortTest/QSortTest.csproj | 21 +++++++++ 7 files changed, 315 insertions(+) create mode 100644 QSort/QSort.sln create mode 100644 QSort/QSort/Program.cs create mode 100644 QSort/QSort/QSort.cs create mode 100644 QSort/QSort/QSort.csproj create mode 100644 QSort/QSort/Statistics.cs create mode 100644 QSort/QSortTest/QSortTest.cs create mode 100644 QSort/QSortTest/QSortTest.csproj diff --git a/QSort/QSort.sln b/QSort/QSort.sln new file mode 100644 index 0000000..508bd40 --- /dev/null +++ b/QSort/QSort.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QSort", "QSort\QSort.csproj", "{94307952-BF53-4474-BC48-C9BB49E563F3}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QSortTest", "QSortTest\QSortTest.csproj", "{1C4FF2C2-4A26-46EE-A1FB-9BC55EA704BE}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {94307952-BF53-4474-BC48-C9BB49E563F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {94307952-BF53-4474-BC48-C9BB49E563F3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {94307952-BF53-4474-BC48-C9BB49E563F3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {94307952-BF53-4474-BC48-C9BB49E563F3}.Release|Any CPU.Build.0 = Release|Any CPU + {1C4FF2C2-4A26-46EE-A1FB-9BC55EA704BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1C4FF2C2-4A26-46EE-A1FB-9BC55EA704BE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1C4FF2C2-4A26-46EE-A1FB-9BC55EA704BE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1C4FF2C2-4A26-46EE-A1FB-9BC55EA704BE}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/QSort/QSort/Program.cs b/QSort/QSort/Program.cs new file mode 100644 index 0000000..4c7f43f --- /dev/null +++ b/QSort/QSort/Program.cs @@ -0,0 +1,65 @@ +using System; +using System.Diagnostics; + +namespace QSort +{ + class Program + { + static void Main(string[] args) + { + Statistics.CollectStatisticsFromMatrix(1000, 1000); + /* + Результаты на массивах размером 1000. + Количество повторов: 1000. + Паралельная сортировка: + Матожидание = 0,005 + Среднеквадратичное отклонение = 0,1580348062927937 + Обычная сортировка: + Матожидание = 0,136 + Среднеквадратичное отклонение = 0,5399111037939502 + #1# + + Statistics.CollectStatisticsFromMatrix(1000, 500); + /* + Результаты на массивах размером 1000. + Количество повторов: 500. + Паралельная сортировка: + Матожидание = 0 + Среднеквадратичное отклонение = 0 + Обычная сортировка: + Матожидание = 0,03 + Среднеквадратичное отклонение = 0,1705872210923191 + #1# + + Statistics.CollectStatisticsFromMatrix(500, 250); + /* + Результаты на массивах размером 500. + Количество повторов: 250. + Паралельная сортировка: + Матожидание = 0 + Среднеквадратичное отклонение = 0 + Обычная сортировка: + Матожидание = 0 + Среднеквадратичное отклонение = 0 + #1# + */ + + var array = new int[100]; + var rand = new Random(); + for (int i = 0; i < 100; i++) + { + array[i] = rand.Next(100); + } + var array1 = array; + var sorter = new QSort(array); + var timer = new Stopwatch(); + timer.Start(); + sorter.SortMulti(); + timer.Stop(); + for (int i = 0; i < array.Length; i++) + { + Console.Write($"{array[i]} "); + } + } + } +} \ No newline at end of file diff --git a/QSort/QSort/QSort.cs b/QSort/QSort/QSort.cs new file mode 100644 index 0000000..93a0647 --- /dev/null +++ b/QSort/QSort/QSort.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace QSort +{ + public class QSort where T:IComparable + { + public IList List; + + public QSort(IList list) + { + this.List = list; + } + + public int Partition(int left, int right) + { + int start = right; + int end = left; + while (start != end) + { + if (List[end].CompareTo(List[start]) <= 0) + { + end++; + } + else + { + Swap(end, start); + Swap(end, start - 1); + start--; + } + } + return end; + } + + private void Swap(int posOne, int posTwo) + { + (List[posOne], List[posTwo]) = (List[posTwo], List[posOne]); + } + + public void SortCall(int start, int end) + { + if (start >= end) + { + return; + } + int partition = Partition(start, end); + SortCall(start, partition - 1); + SortCall(partition + 1, end); + } + + public void Sort() + { + SortCall(0, List.Count - 1); + } + + public void SortCallMulti(int start, int end) + { + if (start >= end) + { + return; + } + int partition = Partition(start, end); + var task = new Task[2]; + task[0] = Task.Run(() => SortCallMulti(start, partition - 1)); + task[1] = Task.Run(() => SortCallMulti(partition + 1, end)); + + } + + public void SortMulti() + { + SortCallMulti(0, List.Count - 1); + } + } +} \ No newline at end of file diff --git a/QSort/QSort/QSort.csproj b/QSort/QSort/QSort.csproj new file mode 100644 index 0000000..5ce0476 --- /dev/null +++ b/QSort/QSort/QSort.csproj @@ -0,0 +1,12 @@ + + + + Exe + net5.0 + enable + enable + Windows + preview + + + diff --git a/QSort/QSort/Statistics.cs b/QSort/QSort/Statistics.cs new file mode 100644 index 0000000..64ddd46 --- /dev/null +++ b/QSort/QSort/Statistics.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; + +namespace QSort +{ + public class Statistics + { + public static void CollectStatisticsFromMatrix(int lenght, int count) + { + (var timeParallel, var timeNotParallel) = GetTimeArraySort(count,lenght); + var result = GetStatistics(timeParallel, timeNotParallel); + Console.WriteLine($"Результаты на массивах размером {lenght}."); + Console.WriteLine($"Количество повторов: {count}."); + Console.WriteLine("Паралельная сортировка:"); + Console.WriteLine( + $"Матожидание = {result[0].average}\nСреднеквадратичное отклонение = {result[0].standardDeviation}"); + Console.WriteLine("Обычная сортировка:"); + Console.WriteLine( + $"Матожидание = {result[1].average}\nСреднеквадратичное отклонение = {result[1].standardDeviation}"); + } + + private static (double average, double standardDeviation)[] GetStatistics(List timeParallel, + List timeNotParallel) + { + var averageParallel = timeParallel.Average(); + var averageNotParallel = timeNotParallel.Average(); + var dispersionParallel = timeParallel.Select(x => Math.Pow(x - averageParallel, 2)).Average(); + var dispersionNotParallel = timeNotParallel.Select(x => Math.Pow(x - averageNotParallel, 2)).Average(); + var standardDeviationParallel = Math.Sqrt(dispersionParallel); + var standardDeviationNotParallel = Math.Sqrt(dispersionNotParallel); + var results = new (double, double)[2]; + results[0] = (averageParallel, standardDeviationParallel); + results[1] = (averageNotParallel, standardDeviationNotParallel); + return results; + } + + private static (List, List) GetTimeArraySort(int count, int lenght) + { + var timeParallel = new List(); + var timeNotParallel = new List(); + var timer = new Stopwatch(); + for (int i = 0; i < count; i++) + { + var array = GenerateArray(lenght); + var sorter = new QSort(array); + timer.Restart(); + sorter.SortMulti(); + timer.Stop(); + timeParallel.Add(timer.ElapsedMilliseconds); + array = GenerateArray(lenght); + sorter = new QSort(array); + timer.Restart(); + sorter.Sort(); + timer.Stop(); + timeNotParallel.Add(timer.ElapsedMilliseconds); + } + + return (timeParallel, timeNotParallel); + } + + private static int[] GenerateArray(int lenght) + { + var array = new int[lenght]; + var random = new Random(); + for (int i = 0; i < lenght; i++) + { + array[i] = random.Next(0, 100); + } + + return array; + } + } +} \ No newline at end of file diff --git a/QSort/QSortTest/QSortTest.cs b/QSort/QSortTest/QSortTest.cs new file mode 100644 index 0000000..a6f9a75 --- /dev/null +++ b/QSort/QSortTest/QSortTest.cs @@ -0,0 +1,45 @@ +using System; +using NUnit.Framework; +using QSort; + +namespace QSortTest +{ + public class Tests + { + private int[] _array; + + [SetUp] + public void Setup() + { + var array = new int[100]; + var rand = new Random(); + for (int i = 0; i < 100; i++) + { + array[i] = rand.Next(100); + } + _array = array; + } + + [Test] + public void QSortTest() + { + var sorter = new QSort(_array); + sorter.Sort(); + for (int i = 0; i < _array.Length - 1; ++i) + { + Assert.IsTrue(_array[i] <= _array[i + 1]); + } + } + + [Test] + public void QSortMultiTest() + { + var sorter = new QSort(_array); + sorter.SortMulti(); + for (int i = 0; i < _array.Length - 1; ++i) + { + Assert.IsTrue(_array[i] <= _array[i + 1]); + } + } + } +} \ No newline at end of file diff --git a/QSort/QSortTest/QSortTest.csproj b/QSort/QSortTest/QSortTest.csproj new file mode 100644 index 0000000..7bcaeba --- /dev/null +++ b/QSort/QSortTest/QSortTest.csproj @@ -0,0 +1,21 @@ + + + + net5.0 + enable + + false + + + + + + + + + + + + + + From 3e129253156aa95438c9f4ad031e6a8464011bdf Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Fri, 24 Dec 2021 16:20:54 +0300 Subject: [PATCH 2/7] correct --- QSort/QSort/QSort.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/QSort/QSort/QSort.cs b/QSort/QSort/QSort.cs index 93a0647..e66dfd0 100644 --- a/QSort/QSort/QSort.cs +++ b/QSort/QSort/QSort.cs @@ -6,14 +6,14 @@ namespace QSort { public class QSort where T:IComparable { - public IList List; + private IList List; public QSort(IList list) { this.List = list; } - public int Partition(int left, int right) + private int Partition(int left, int right) { int start = right; int end = left; @@ -38,7 +38,7 @@ private void Swap(int posOne, int posTwo) (List[posOne], List[posTwo]) = (List[posTwo], List[posOne]); } - public void SortCall(int start, int end) + private void SortCall(int start, int end) { if (start >= end) { @@ -54,7 +54,7 @@ public void Sort() SortCall(0, List.Count - 1); } - public void SortCallMulti(int start, int end) + private void SortCallMulti(int start, int end) { if (start >= end) { @@ -64,7 +64,6 @@ public void SortCallMulti(int start, int end) var task = new Task[2]; task[0] = Task.Run(() => SortCallMulti(start, partition - 1)); task[1] = Task.Run(() => SortCallMulti(partition + 1, end)); - } public void SortMulti() From 8a120efdfef268d7fbf0ffde82ea4279c1082780 Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Fri, 24 Dec 2021 16:23:32 +0300 Subject: [PATCH 3/7] fix --- QSort/QSort/QSort.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/QSort/QSort/QSort.cs b/QSort/QSort/QSort.cs index e66dfd0..1a908cc 100644 --- a/QSort/QSort/QSort.cs +++ b/QSort/QSort/QSort.cs @@ -56,13 +56,10 @@ public void Sort() private void SortCallMulti(int start, int end) { - if (start >= end) - { - return; - } int partition = Partition(start, end); var task = new Task[2]; task[0] = Task.Run(() => SortCallMulti(start, partition - 1)); + task[0].Wait(); task[1] = Task.Run(() => SortCallMulti(partition + 1, end)); } From b6cb988b607f6d5dace8e33d611d664c4abe5537 Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Fri, 24 Dec 2021 16:25:20 +0300 Subject: [PATCH 4/7] fix --- QSort/QSort/QSort.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/QSort/QSort/QSort.cs b/QSort/QSort/QSort.cs index 1a908cc..361341e 100644 --- a/QSort/QSort/QSort.cs +++ b/QSort/QSort/QSort.cs @@ -61,6 +61,7 @@ private void SortCallMulti(int start, int end) task[0] = Task.Run(() => SortCallMulti(start, partition - 1)); task[0].Wait(); task[1] = Task.Run(() => SortCallMulti(partition + 1, end)); + task[1].Wait(); } public void SortMulti() From c8f27d10aa32356dd3ba9ce6a6b0392397b69767 Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Fri, 24 Dec 2021 16:25:42 +0300 Subject: [PATCH 5/7] fix --- QSort/QSort/QSort.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/QSort/QSort/QSort.cs b/QSort/QSort/QSort.cs index 361341e..7bd5a6d 100644 --- a/QSort/QSort/QSort.cs +++ b/QSort/QSort/QSort.cs @@ -59,8 +59,8 @@ private void SortCallMulti(int start, int end) int partition = Partition(start, end); var task = new Task[2]; task[0] = Task.Run(() => SortCallMulti(start, partition - 1)); - task[0].Wait(); task[1] = Task.Run(() => SortCallMulti(partition + 1, end)); + task[0].Wait(); task[1].Wait(); } From 0e0c2392e9c80ebb0a0c0ee9cb41cb086488ece2 Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Fri, 24 Dec 2021 16:32:02 +0300 Subject: [PATCH 6/7] correct --- QSort/QSort/QSort.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/QSort/QSort/QSort.cs b/QSort/QSort/QSort.cs index 7bd5a6d..4f58861 100644 --- a/QSort/QSort/QSort.cs +++ b/QSort/QSort/QSort.cs @@ -7,7 +7,7 @@ namespace QSort public class QSort where T:IComparable { private IList List; - + public QSort(IList list) { this.List = list; @@ -60,8 +60,6 @@ private void SortCallMulti(int start, int end) var task = new Task[2]; task[0] = Task.Run(() => SortCallMulti(start, partition - 1)); task[1] = Task.Run(() => SortCallMulti(partition + 1, end)); - task[0].Wait(); - task[1].Wait(); } public void SortMulti() From af688b85ffd31ed9de340b6ccabb2d62ebb527e8 Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Fri, 24 Dec 2021 16:36:29 +0300 Subject: [PATCH 7/7] fix --- QSort/QSort/QSort.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/QSort/QSort/QSort.cs b/QSort/QSort/QSort.cs index 4f58861..ea55511 100644 --- a/QSort/QSort/QSort.cs +++ b/QSort/QSort/QSort.cs @@ -56,6 +56,10 @@ public void Sort() private void SortCallMulti(int start, int end) { + if (start >= end) + { + return; + } int partition = Partition(start, end); var task = new Task[2]; task[0] = Task.Run(() => SortCallMulti(start, partition - 1));