From a0a8184dec1a401bd01a65e0ada9e916cd9b6597 Mon Sep 17 00:00:00 2001 From: Wouter Groeneveld Date: Wed, 21 Mar 2018 16:50:46 +0100 Subject: [PATCH] heap sort in C# tussen de soep en pattatt'n --- .gitignore | 307 ++++++++++++++++++ .../Sorting.Tests/Properties/AssemblyInfo.cs | 20 ++ .../dotnet/Sorting.Tests/Sorting.Tests.csproj | 73 +++++ sorting/dotnet/Sorting.Tests/SortingTests.cs | 46 +++ sorting/dotnet/Sorting.Tests/packages.config | 5 + sorting/dotnet/Sorting.sln | 31 ++ sorting/dotnet/Sorting/App.config | 6 + sorting/dotnet/Sorting/HeapSort.cs | 96 ++++++ sorting/dotnet/Sorting/ISortable.cs | 9 + sorting/dotnet/Sorting/Program.cs | 15 + .../dotnet/Sorting/Properties/AssemblyInfo.cs | 37 +++ sorting/dotnet/Sorting/Sorting.csproj | 54 +++ 12 files changed, 699 insertions(+) create mode 100644 sorting/dotnet/Sorting.Tests/Properties/AssemblyInfo.cs create mode 100644 sorting/dotnet/Sorting.Tests/Sorting.Tests.csproj create mode 100644 sorting/dotnet/Sorting.Tests/SortingTests.cs create mode 100644 sorting/dotnet/Sorting.Tests/packages.config create mode 100644 sorting/dotnet/Sorting.sln create mode 100644 sorting/dotnet/Sorting/App.config create mode 100644 sorting/dotnet/Sorting/HeapSort.cs create mode 100644 sorting/dotnet/Sorting/ISortable.cs create mode 100644 sorting/dotnet/Sorting/Program.cs create mode 100644 sorting/dotnet/Sorting/Properties/AssemblyInfo.cs create mode 100644 sorting/dotnet/Sorting/Sorting.csproj diff --git a/.gitignore b/.gitignore index 6143e53..235c4b5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ # Compiled class file *.class +*.dll + # Log file *.log @@ -20,3 +22,308 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* + + + + + +# Created by https://www.gitignore.io/api/visualstudio + +### VisualStudio ### +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ +**/Properties/launchSettings.json + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# TODO: Uncomment the next line to ignore your web deploy settings. +# By default, sensitive information, such as encrypted password +# should be stored in the .pubxml.user file. +#*.pubxml +*.pubxml.user +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Typescript v1 declaration files +typings/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush +.cr/ + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +### VisualStudio Patch ### +# By default, sensitive information, such as encrypted password +# should be stored in the .pubxml.user file. + + +# End of https://www.gitignore.io/api/visualstudio diff --git a/sorting/dotnet/Sorting.Tests/Properties/AssemblyInfo.cs b/sorting/dotnet/Sorting.Tests/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..b63bc26 --- /dev/null +++ b/sorting/dotnet/Sorting.Tests/Properties/AssemblyInfo.cs @@ -0,0 +1,20 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +[assembly: AssemblyTitle("Sorting.Tests")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Sorting.Tests")] +[assembly: AssemblyCopyright("Copyright © 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +[assembly: ComVisible(false)] + +[assembly: Guid("397473bf-da8a-4c75-8224-b525f71260da")] + +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/sorting/dotnet/Sorting.Tests/Sorting.Tests.csproj b/sorting/dotnet/Sorting.Tests/Sorting.Tests.csproj new file mode 100644 index 0000000..583f1e1 --- /dev/null +++ b/sorting/dotnet/Sorting.Tests/Sorting.Tests.csproj @@ -0,0 +1,73 @@ + + + + + Debug + AnyCPU + {397473BF-DA8A-4C75-8224-B525F71260DA} + Library + Properties + Sorting.Tests + Sorting.Tests + v4.6.1 + 512 + {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 15.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages + False + UnitTest + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\..\..\..\..\..\nuget\packages\MSTest.TestFramework.1.2.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll + + + ..\..\..\..\..\..\nuget\packages\MSTest.TestFramework.1.2.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll + + + + + + + + + + + + + + {5AF0C5C2-36F1-466D-9B0A-74DAF4D726BB} + Sorting + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + \ No newline at end of file diff --git a/sorting/dotnet/Sorting.Tests/SortingTests.cs b/sorting/dotnet/Sorting.Tests/SortingTests.cs new file mode 100644 index 0000000..26a0e47 --- /dev/null +++ b/sorting/dotnet/Sorting.Tests/SortingTests.cs @@ -0,0 +1,46 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Collections.Generic; + +namespace Sorting.Tests +{ + [TestClass] + public class SortingTests + { + [TestMethod] + public void HeapSorting_MergeSortedLists_EachListHasDifferentAmountOfNumbers() + { + var sort = new HeapSort(); + var result = sort.MergeSortedLists(new List { 3 }, new List { 2, 4 }); + + CollectionAssert.AreEqual(new List { 2, 3, 4 }, result); + } + + [TestMethod] + public void HeapSorting_MergeSortedLists_EachListHasSameAmountOfNumbers() + { + var sort = new HeapSort(); + var result = sort.MergeSortedLists(new List { 1, 3 }, new List { 2, 4 }); + + CollectionAssert.AreEqual(new List { 1, 2, 3, 4 }, result); + } + + [TestMethod] + public void HeapSorting_SimpleListDividesNeatlyIntoTwo() + { + ExecuteTestWithSortMethod(new HeapSort(), new List { 4, 3, 2, 1 }, new List { 1, 2, 3, 4 }); + } + + [TestMethod] + public void HeapSorting_SimpleListUnevenNumbers() + { + ExecuteTestWithSortMethod(new HeapSort(), new List { 5, 7, 6, 4, 3, 2, 1 }, new List { 1, 2, 3, 4, 5, 6, 7 }); + } + + private void ExecuteTestWithSortMethod(ISortable sortable, List toSort, List expected) + { + var result = sortable.Sort(toSort); + + CollectionAssert.AreEqual(expected, result, "actual: " + result.AsString()); + } + } +} \ No newline at end of file diff --git a/sorting/dotnet/Sorting.Tests/packages.config b/sorting/dotnet/Sorting.Tests/packages.config new file mode 100644 index 0000000..cf2d094 --- /dev/null +++ b/sorting/dotnet/Sorting.Tests/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/sorting/dotnet/Sorting.sln b/sorting/dotnet/Sorting.sln new file mode 100644 index 0000000..b1f3518 --- /dev/null +++ b/sorting/dotnet/Sorting.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27428.2011 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sorting", "Sorting\Sorting.csproj", "{5AF0C5C2-36F1-466D-9B0A-74DAF4D726BB}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sorting.Tests", "Sorting.Tests\Sorting.Tests.csproj", "{397473BF-DA8A-4C75-8224-B525F71260DA}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {5AF0C5C2-36F1-466D-9B0A-74DAF4D726BB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5AF0C5C2-36F1-466D-9B0A-74DAF4D726BB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5AF0C5C2-36F1-466D-9B0A-74DAF4D726BB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5AF0C5C2-36F1-466D-9B0A-74DAF4D726BB}.Release|Any CPU.Build.0 = Release|Any CPU + {397473BF-DA8A-4C75-8224-B525F71260DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {397473BF-DA8A-4C75-8224-B525F71260DA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {397473BF-DA8A-4C75-8224-B525F71260DA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {397473BF-DA8A-4C75-8224-B525F71260DA}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {C5184B8F-5851-42ED-8631-F2FD55088D85} + EndGlobalSection +EndGlobal diff --git a/sorting/dotnet/Sorting/App.config b/sorting/dotnet/Sorting/App.config new file mode 100644 index 0000000..731f6de --- /dev/null +++ b/sorting/dotnet/Sorting/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/sorting/dotnet/Sorting/HeapSort.cs b/sorting/dotnet/Sorting/HeapSort.cs new file mode 100644 index 0000000..288732d --- /dev/null +++ b/sorting/dotnet/Sorting/HeapSort.cs @@ -0,0 +1,96 @@ +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; + +namespace Sorting +{ + public static class ListExt + { + public static string AsString(this List list) + { + return "(" + string.Join(", ", list.Select(x => x.ToString())) + ")"; + } + } + + public class HeapSort : ISortable + { + public List Sort(List list) + { + return SortRecur(list); + } + + internal List MergeSortedLists(List a, List b) + { + var result = new List(); + var aCounter = 0; + var bCounter = 0; + + Debug.WriteLine("merging" + a.AsString() + " with " + b.AsString()); + + while (aCounter < a.Count && bCounter < b.Count) + { + var currA = a[aCounter]; + var currB = b[bCounter]; + + if (currA < currB) + { + result.Add(currA); + aCounter++; + } + else + { + result.Add(currB); + bCounter++; + } + } + + if (aCounter < a.Count) + { + result.AddRange(AddRemainder(a, aCounter)); + } + if (bCounter < b.Count) + { + result.AddRange(AddRemainder(b, bCounter)); + } + + return result; + } + + private List AddRemainder(List list, int index) + { + List result = new List(); + for (var i = index; i < list.Count; i++) + { + result.Add(list[i]); + } + + return result; + } + + private List SortRecur(List list) + { + Debug.WriteLine("sorting " + list.AsString()); + + if (list.Count == 2) + { + return new List + { + list[0] < list[1] ? list[0] : list[1], + list[0] < list[1] ? list[1] : list[0] + }; + } + if (list.Count <= 1) + { + return list; + } + + var halfIndex = list.Count / 2; + + var left = list.TakeWhile((number, i) => i < halfIndex).ToList(); + var right = list.Except(left).ToList(); + return MergeSortedLists( + SortRecur(left), + SortRecur(right)); + } + } +} \ No newline at end of file diff --git a/sorting/dotnet/Sorting/ISortable.cs b/sorting/dotnet/Sorting/ISortable.cs new file mode 100644 index 0000000..858055f --- /dev/null +++ b/sorting/dotnet/Sorting/ISortable.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace Sorting +{ + public interface ISortable + { + List Sort(List list); + } +} \ No newline at end of file diff --git a/sorting/dotnet/Sorting/Program.cs b/sorting/dotnet/Sorting/Program.cs new file mode 100644 index 0000000..d888435 --- /dev/null +++ b/sorting/dotnet/Sorting/Program.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Sorting +{ + class Program + { + static void Main(string[] args) + { + } + } +} diff --git a/sorting/dotnet/Sorting/Properties/AssemblyInfo.cs b/sorting/dotnet/Sorting/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..1970e3c --- /dev/null +++ b/sorting/dotnet/Sorting/Properties/AssemblyInfo.cs @@ -0,0 +1,37 @@ +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("Sorting")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Sorting")] +[assembly: AssemblyCopyright("Copyright © 2018")] +[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("5af0c5c2-36f1-466d-9b0a-74daf4d726bb")] + +// 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")] +[assembly: InternalsVisibleTo("Sorting.Tests")] \ No newline at end of file diff --git a/sorting/dotnet/Sorting/Sorting.csproj b/sorting/dotnet/Sorting/Sorting.csproj new file mode 100644 index 0000000..4fe2e8a --- /dev/null +++ b/sorting/dotnet/Sorting/Sorting.csproj @@ -0,0 +1,54 @@ + + + + + Debug + AnyCPU + {5AF0C5C2-36F1-466D-9B0A-74DAF4D726BB} + Exe + Sorting + Sorting + v4.6.1 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file