Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added gnome sort #34

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
<Reference Include="linearsearch">
<HintPath>..\linearsearch\bin\Debug\linearsearch.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
</Reference>
Expand All @@ -63,6 +62,7 @@
<Compile Include="search\JumpSearchTest.cs" />
<Compile Include="search\LinearsearchTest.cs" />
<Compile Include="search\InterpolationSearchTest.cs" />
<Compile Include="sort\GnomeSortTest.cs" />
<Compile Include="sort\QuickSortTests.cs" />
<Compile Include="greedy\BellmanFordAlgorithmTests.cs" />
<Compile Include="sort\BogoSortTests.cs" />
Expand Down Expand Up @@ -102,6 +102,10 @@
<Project>{92DB864E-DF8A-470C-9746-69B7C41AB80C}</Project>
<Name>fibonaccisearch</Name>
</ProjectReference>
<ProjectReference Include="..\gnomesort\gnomesort.csproj">
<Project>{1593D8D2-1C16-4204-BCF3-FB79E1CE5ABC}</Project>
<Name>gnomesort</Name>
</ProjectReference>
<ProjectReference Include="..\quicksort\quicksort.csproj">
<Project>{9199c1cc-0966-40c9-8acc-7ab1baf6512a}</Project>
<Name>quicksort</Name>
Expand All @@ -123,9 +127,6 @@
<Name>heapsort</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<WCFMetadata Include="Service References\" />
</ItemGroup>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
Expand Down
54 changes: 54 additions & 0 deletions Tests/sort/GnomeSortTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Collections.Generic;
using System.Linq;
using csharp_algorithms;
using gnomesort;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Tests.sort
{
[TestClass]
public class GnomeSortTest
{
[TestMethod]
public void NormalSort()
{
// Arrange
var list = new List<Node>
{
new Node(5),
new Node(1)
};
var b = new GnomeSort(list);

// Act
List<Node> result = b.Sort().ToList();

// Assert
Assert.AreEqual(1, result.ElementAt(0).Number);
Assert.AreEqual(5, result.ElementAt(1).Number);
}

[TestMethod]
public void LongerSort()
{
// Arrange
var list = new List<Node>
{
new Node(3),
new Node(5),
new Node(1),
new Node(8)
};
var b = new GnomeSort(list);

// Act
var result = b.Sort().ToList();

// Assert
Assert.AreEqual(1, result.ElementAt(0).Number);
Assert.AreEqual(3, result.ElementAt(1).Number);
Assert.AreEqual(5, result.ElementAt(2).Number);
Assert.AreEqual(8, result.ElementAt(3).Number);
}
}
}
7 changes: 7 additions & 0 deletions csharp-algorithms.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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}") = "gnomesort", "gnomesort\gnomesort.csproj", "{1593D8D2-1C16-4204-BCF3-FB79E1CE5ABC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -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
{1593D8D2-1C16-4204-BCF3-FB79E1CE5ABC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1593D8D2-1C16-4204-BCF3-FB79E1CE5ABC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1593D8D2-1C16-4204-BCF3-FB79E1CE5ABC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1593D8D2-1C16-4204-BCF3-FB79E1CE5ABC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -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}
{1593D8D2-1C16-4204-BCF3-FB79E1CE5ABC} = {6A9C8607-AA57-4817-B2C8-B8DD5065AC7C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7F2BFC66-A9D6-4C6D-A15A-984F23C11D29}
Expand Down
43 changes: 43 additions & 0 deletions gnomesort/GnomeSort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using csharp_algorithms;

namespace gnomesort
{
public class GnomeSort
{
private readonly IEnumerable<Node> _list;

public GnomeSort(IEnumerable<Node> list)
{
_list = list;
}

private void Swap(Node left, Node right)
{
int originalLeft = left.Number;
left.Number = right.Number;
right.Number = originalLeft;
}

public IEnumerable<Node> Sort()
{
var i = 0;
while (i < _list.Count())
if (i == 0 || _list.ElementAt(i).Number > _list.ElementAt(i - 1).Number)
{
i++;
}
else
{
Swap(_list.ElementAt(i), _list.ElementAt(i - 1));
i--;
}

return _list;
}
}
}
36 changes: 36 additions & 0 deletions gnomesort/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// Allgemeine Informationen über eine Assembly werden über die folgenden
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
// die einer Assembly zugeordnet sind.
[assembly: AssemblyTitle("gnomesort")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("gnomesort")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly
// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von
// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen.
[assembly: ComVisible(false)]

// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird
[assembly: Guid("1593d8d2-1c16-4204-bcf3-fb79e1ce5abc")]

// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
//
// Hauptversion
// Nebenversion
// Buildnummer
// Revision
//
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
// indem Sie "*" wie unten gezeigt eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
54 changes: 54 additions & 0 deletions gnomesort/gnomesort.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{1593D8D2-1C16-4204-BCF3-FB79E1CE5ABC}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>gnomesort</RootNamespace>
<AssemblyName>gnomesort</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="GnomeSort.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\csharp-algorithms\csharp-algorithms.csproj">
<Project>{3511528e-8696-40b8-85ab-97456347a497}</Project>
<Name>csharp-algorithms</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>