From 909c10a47f9edf8152615fbb5a203e15d7afd5b5 Mon Sep 17 00:00:00 2001 From: Yusuf Duyar Date: Thu, 3 Oct 2019 16:17:47 +0300 Subject: [PATCH] Add shell sort --- ShellSort/Properties/AssemblyInfo.cs | 36 +++++++++++++++++++ ShellSort/ShellSort.cs | 48 +++++++++++++++++++++++++ ShellSort/ShellSort.csproj | 48 +++++++++++++++++++++++++ Tests/Tests.csproj | 18 +++------- Tests/search/ExponentialSearchTests.cs | 29 --------------- Tests/search/InterpolationSearchTest.cs | 35 ------------------ Tests/search/JumpSearchTest.cs | 35 ------------------ Tests/search/LinearsearchTest.cs | 34 ------------------ Tests/sort/ShellSortTests.cs | 27 ++++++++++++++ csharp-algorithms.sln | 11 ++++-- 10 files changed, 173 insertions(+), 148 deletions(-) create mode 100644 ShellSort/Properties/AssemblyInfo.cs create mode 100644 ShellSort/ShellSort.cs create mode 100644 ShellSort/ShellSort.csproj delete mode 100644 Tests/search/ExponentialSearchTests.cs delete mode 100644 Tests/search/InterpolationSearchTest.cs delete mode 100644 Tests/search/JumpSearchTest.cs delete mode 100644 Tests/search/LinearsearchTest.cs create mode 100644 Tests/sort/ShellSortTests.cs diff --git a/ShellSort/Properties/AssemblyInfo.cs b/ShellSort/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..cd5f3a63 --- /dev/null +++ b/ShellSort/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("ShellSort")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ShellSort")] +[assembly: AssemblyCopyright("Copyright © 2019")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("a6af49ea-7c5e-4265-b1d4-845788389f63")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/ShellSort/ShellSort.cs b/ShellSort/ShellSort.cs new file mode 100644 index 00000000..5fd576e6 --- /dev/null +++ b/ShellSort/ShellSort.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace shellsort +{ + public class ShellSort + { + private readonly int[] _array; + + public ShellSort(int[] array) + { + this._array = array; + } + + public int[] Sort() + { + int i, j, inc, temp; + var array_size = _array.Length; + inc = 3; + + while (inc > 0) + { + for (i = 0; i < array_size; i++) + { + j = i; + temp = _array[i]; + while ((j >= inc) && (_array[j - inc] > temp)) + { + _array[j] = _array[j - inc]; + j = j - inc; + } + _array[j] = temp; + } + if (inc / 2 != 0) + inc = inc / 2; + else if (inc == 1) + inc = 0; + else + inc = 1; + } + + return _array; + } + } +} diff --git a/ShellSort/ShellSort.csproj b/ShellSort/ShellSort.csproj new file mode 100644 index 00000000..f594aec4 --- /dev/null +++ b/ShellSort/ShellSort.csproj @@ -0,0 +1,48 @@ + + + + + Debug + AnyCPU + {A6AF49EA-7C5E-4265-B1D4-845788389F63} + Library + Properties + ShellSort + ShellSort + v4.6.1 + 512 + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index e551d4db..227167f9 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -39,16 +39,6 @@ 4 - - ..\interpolationsearch\bin\Debug\interpolationsearch.dll - - - ..\jumpsearch\bin\Debug\jumpsearch.dll - - - ..\linearsearch\bin\Debug\linearsearch.dll - - ..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll @@ -60,9 +50,6 @@ - - - @@ -73,6 +60,7 @@ + @@ -118,6 +106,10 @@ {A9022928-E9E9-45BE-BFEC-85A1A870A7EB} selectionsort + + {a6af49ea-7c5e-4265-b1d4-845788389f63} + ShellSort + {ceab3173-99c9-4f09-aee2-d31152d4270d} heapsort diff --git a/Tests/search/ExponentialSearchTests.cs b/Tests/search/ExponentialSearchTests.cs deleted file mode 100644 index f95f20fc..00000000 --- a/Tests/search/ExponentialSearchTests.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System.Collections.Generic; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using csharp_algorithms; -using exponentialsearch; - -namespace Tests.search -{ - [TestClass] - public class ExponentialSearchTest - { - [TestMethod] - public void Search() - { - // Setup - int[] arr = { 0, 1, 10, 13, 16, 20, 22, 28, 31, 39, 45, 55 }; - int searchElem = 10; - - - // Search - var result1 = ExponentialSearch.Search(arr, searchElem); - searchElem = 11; - var result2 = ExponentialSearch.Search(arr, searchElem); ; - - // Assert - Assert.AreEqual(true, result1); - Assert.AreEqual(false, result2); - } - } -} \ No newline at end of file diff --git a/Tests/search/InterpolationSearchTest.cs b/Tests/search/InterpolationSearchTest.cs deleted file mode 100644 index 2b08f669..00000000 --- a/Tests/search/InterpolationSearchTest.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System.Collections.Generic; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using csharp_algorithms; -using interpolationsearch; - -namespace Tests.search -{ - [TestClass] - public class InterpolationSearchTest - { - [TestMethod] - public void Search() - { - //Arrange - List list = new List - { - new Node(5), - new Node(1), - new Node(10), - new Node(11) - }; - InterpolationSearch s = new InterpolationSearch(list); - //Act - Node searchValue = new Node(11); - var result1 = s.Search(searchValue); - - searchValue = new Node(12); - var result2 = s.Search(searchValue); - - //Assert - Assert.AreEqual(true, result1); - Assert.AreEqual(false, result2); - } - } -} diff --git a/Tests/search/JumpSearchTest.cs b/Tests/search/JumpSearchTest.cs deleted file mode 100644 index 234a8907..00000000 --- a/Tests/search/JumpSearchTest.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System.Collections.Generic; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using csharp_algorithms; -using jumpsearch; - -namespace Tests.search -{ - [TestClass] - public class JumpSearchTest - { - [TestMethod] - public void Search() - { - //Arrange - List list = new List - { - new Node(5), - new Node(1), - new Node(10), - new Node(11) - }; - JumpSearch s = new JumpSearch(list); - //Act - Node searchValue = new Node(11); - var result1 = s.Search(searchValue); - - searchValue = new Node(12); - var result2 = s.Search(searchValue); - - //Assert - Assert.AreEqual(true, result1); - Assert.AreEqual(false, result2); - } - } -} \ No newline at end of file diff --git a/Tests/search/LinearsearchTest.cs b/Tests/search/LinearsearchTest.cs deleted file mode 100644 index a0cb708a..00000000 --- a/Tests/search/LinearsearchTest.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System.Collections.Generic; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using csharp_algorithms; -using linearsearch; - -namespace Tests.search -{ - [TestClass] - public class LinearsearchTest - { - [TestMethod] - public void Search() - { - //Arrange - List list = new List - { - new Node(5), - new Node(1), - new Node(10), - new Node(11) - }; - LinearSearch s = new LinearSearch(list); - //Act - Node searchValue = new Node(11); - var result1 = s.Search(searchValue); - - searchValue = new Node(12); - var result2 = s.Search(searchValue); - //Assert - Assert.AreEqual(true, result1); - Assert.AreEqual(false, result2); - } - } -} diff --git a/Tests/sort/ShellSortTests.cs b/Tests/sort/ShellSortTests.cs new file mode 100644 index 00000000..99bf69f9 --- /dev/null +++ b/Tests/sort/ShellSortTests.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using shellsort; + +namespace Tests.sort +{ + [TestClass] + public class ShellSortTests + { + [TestMethod] + public void SortNormally() + { + var array = new[] { 25, 17, 49, 1, 195, 58 }; + + var shellSort = new ShellSort(array); + + var result = shellSort.Sort().ToList(); + + Assert.AreEqual(1, result.First()); + Assert.AreEqual(195, result.Last()); + } + } +} diff --git a/csharp-algorithms.sln b/csharp-algorithms.sln index 34e32858..71c0b1a6 100644 --- a/csharp-algorithms.sln +++ b/csharp-algorithms.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.28010.2019 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29230.47 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "csharp-algorithms", "csharp-algorithms\csharp-algorithms.csproj", "{3511528E-8696-40B8-85AB-97456347A497}" EndProject @@ -33,6 +33,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "knapsackproblem", "knapsack EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "bellmanFordAlgorithm", "bellmanFordAlgorithm\bellmanFordAlgorithm.csproj", "{6B79C9EC-0219-44AD-B399-CD4046718E66}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShellSort", "ShellSort\ShellSort.csproj", "{A6AF49EA-7C5E-4265-B1D4-845788389F63}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -87,6 +89,10 @@ Global {6B79C9EC-0219-44AD-B399-CD4046718E66}.Debug|Any CPU.Build.0 = Debug|Any CPU {6B79C9EC-0219-44AD-B399-CD4046718E66}.Release|Any CPU.ActiveCfg = Release|Any CPU {6B79C9EC-0219-44AD-B399-CD4046718E66}.Release|Any CPU.Build.0 = Release|Any CPU + {A6AF49EA-7C5E-4265-B1D4-845788389F63}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A6AF49EA-7C5E-4265-B1D4-845788389F63}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A6AF49EA-7C5E-4265-B1D4-845788389F63}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A6AF49EA-7C5E-4265-B1D4-845788389F63}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -102,6 +108,7 @@ Global {8087DFD2-673A-499F-B130-0FAE0F31EADC} = {05BBB622-53C5-4866-869B-B45978502CB5} {8870FC35-4386-43E5-B5EA-1C764E7ADC54} = {05BBB622-53C5-4866-869B-B45978502CB5} {6B79C9EC-0219-44AD-B399-CD4046718E66} = {05BBB622-53C5-4866-869B-B45978502CB5} + {A6AF49EA-7C5E-4265-B1D4-845788389F63} = {6A9C8607-AA57-4817-B2C8-B8DD5065AC7C} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {7F2BFC66-A9D6-4C6D-A15A-984F23C11D29}