├── .gitattributes ├── .gitignore ├── BeadSort ├── BeadSort.csproj └── Program.cs ├── BitonicMergeSort ├── BitonicMergeSort.csproj └── Program.cs ├── BogoSort ├── BogoSort.csproj └── Program.cs ├── BubbleSort ├── BubbleSort.csproj └── Program.cs ├── BucketSort ├── BucketSort.csproj └── Program.cs ├── CocktailShakerSort ├── CocktailShakerSort.csproj └── Program.cs ├── CombSort ├── CombSort.csproj └── Program.cs ├── Common ├── Common.csproj └── CommonFunctions.cs ├── CountingSort ├── CountingSort.csproj └── Program.cs ├── GnomeSort ├── GnomeSort.csproj └── Program.cs ├── HeapSort ├── HeapSort.csproj └── Program.cs ├── InsertionSort ├── InsertionSort.csproj └── Program.cs ├── LibrarySort ├── LibrarySort.csproj └── Program.cs ├── MergeSort ├── MergeSort.csproj └── Program.cs ├── OddEvenSort ├── OddEvenSort.csproj └── Program.cs ├── PigeonholeSort ├── PigeonholeSort.csproj └── Program.cs ├── QuickSort ├── Program.cs └── QuickSort.csproj ├── README.md ├── RadixSort ├── Program.cs └── RadixSort.csproj ├── SelectionSort ├── Program.cs └── SelectionSort.csproj ├── ShellSort ├── Program.cs └── ShellSort.csproj ├── SortExtravaganza.sln ├── TimSort ├── Program.cs └── TimSort.csproj └── TreeSort ├── Program.cs └── TreeSort.csproj /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Build results 17 | [Dd]ebug/ 18 | [Dd]ebugPublic/ 19 | [Rr]elease/ 20 | [Rr]eleases/ 21 | x64/ 22 | x86/ 23 | [Aa][Rr][Mm]/ 24 | [Aa][Rr][Mm]64/ 25 | bld/ 26 | [Bb]in/ 27 | [Oo]bj/ 28 | [Ll]og/ 29 | 30 | # Visual Studio 2015/2017 cache/options directory 31 | .vs/ 32 | # Uncomment if you have tasks that create the project's static files in wwwroot 33 | #wwwroot/ 34 | 35 | # Visual Studio 2017 auto generated files 36 | Generated\ Files/ 37 | 38 | # MSTest test Results 39 | [Tt]est[Rr]esult*/ 40 | [Bb]uild[Ll]og.* 41 | 42 | # NUNIT 43 | *.VisualState.xml 44 | TestResult.xml 45 | 46 | # Build Results of an ATL Project 47 | [Dd]ebugPS/ 48 | [Rr]eleasePS/ 49 | dlldata.c 50 | 51 | # Benchmark Results 52 | BenchmarkDotNet.Artifacts/ 53 | 54 | # .NET Core 55 | project.lock.json 56 | project.fragment.lock.json 57 | artifacts/ 58 | 59 | # StyleCop 60 | StyleCopReport.xml 61 | 62 | # Files built by Visual Studio 63 | *_i.c 64 | *_p.c 65 | *_h.h 66 | *.ilk 67 | *.meta 68 | *.obj 69 | *.iobj 70 | *.pch 71 | *.pdb 72 | *.ipdb 73 | *.pgc 74 | *.pgd 75 | *.rsp 76 | *.sbr 77 | *.tlb 78 | *.tli 79 | *.tlh 80 | *.tmp 81 | *.tmp_proj 82 | *_wpftmp.csproj 83 | *.log 84 | *.vspscc 85 | *.vssscc 86 | .builds 87 | *.pidb 88 | *.svclog 89 | *.scc 90 | 91 | # Chutzpah Test files 92 | _Chutzpah* 93 | 94 | # Visual C++ cache files 95 | ipch/ 96 | *.aps 97 | *.ncb 98 | *.opendb 99 | *.opensdf 100 | *.sdf 101 | *.cachefile 102 | *.VC.db 103 | *.VC.VC.opendb 104 | 105 | # Visual Studio profiler 106 | *.psess 107 | *.vsp 108 | *.vspx 109 | *.sap 110 | 111 | # Visual Studio Trace Files 112 | *.e2e 113 | 114 | # TFS 2012 Local Workspace 115 | $tf/ 116 | 117 | # Guidance Automation Toolkit 118 | *.gpState 119 | 120 | # ReSharper is a .NET coding add-in 121 | _ReSharper*/ 122 | *.[Rr]e[Ss]harper 123 | *.DotSettings.user 124 | 125 | # JustCode is a .NET coding add-in 126 | .JustCode 127 | 128 | # TeamCity is a build add-in 129 | _TeamCity* 130 | 131 | # DotCover is a Code Coverage Tool 132 | *.dotCover 133 | 134 | # AxoCover is a Code Coverage Tool 135 | .axoCover/* 136 | !.axoCover/settings.json 137 | 138 | # Visual Studio code coverage results 139 | *.coverage 140 | *.coveragexml 141 | 142 | # NCrunch 143 | _NCrunch_* 144 | .*crunch*.local.xml 145 | nCrunchTemp_* 146 | 147 | # MightyMoose 148 | *.mm.* 149 | AutoTest.Net/ 150 | 151 | # Web workbench (sass) 152 | .sass-cache/ 153 | 154 | # Installshield output folder 155 | [Ee]xpress/ 156 | 157 | # DocProject is a documentation generator add-in 158 | DocProject/buildhelp/ 159 | DocProject/Help/*.HxT 160 | DocProject/Help/*.HxC 161 | DocProject/Help/*.hhc 162 | DocProject/Help/*.hhk 163 | DocProject/Help/*.hhp 164 | DocProject/Help/Html2 165 | DocProject/Help/html 166 | 167 | # Click-Once directory 168 | publish/ 169 | 170 | # Publish Web Output 171 | *.[Pp]ublish.xml 172 | *.azurePubxml 173 | # Note: Comment the next line if you want to checkin your web deploy settings, 174 | # but database connection strings (with potential passwords) will be unencrypted 175 | *.pubxml 176 | *.publishproj 177 | 178 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 179 | # checkin your Azure Web App publish settings, but sensitive information contained 180 | # in these scripts will be unencrypted 181 | PublishScripts/ 182 | 183 | # NuGet Packages 184 | *.nupkg 185 | # The packages folder can be ignored because of Package Restore 186 | **/[Pp]ackages/* 187 | # except build/, which is used as an MSBuild target. 188 | !**/[Pp]ackages/build/ 189 | # Uncomment if necessary however generally it will be regenerated when needed 190 | #!**/[Pp]ackages/repositories.config 191 | # NuGet v3's project.json files produces more ignorable files 192 | *.nuget.props 193 | *.nuget.targets 194 | 195 | # Microsoft Azure Build Output 196 | csx/ 197 | *.build.csdef 198 | 199 | # Microsoft Azure Emulator 200 | ecf/ 201 | rcf/ 202 | 203 | # Windows Store app package directories and files 204 | AppPackages/ 205 | BundleArtifacts/ 206 | Package.StoreAssociation.xml 207 | _pkginfo.txt 208 | *.appx 209 | 210 | # Visual Studio cache files 211 | # files ending in .cache can be ignored 212 | *.[Cc]ache 213 | # but keep track of directories ending in .cache 214 | !?*.[Cc]ache/ 215 | 216 | # Others 217 | ClientBin/ 218 | ~$* 219 | *~ 220 | *.dbmdl 221 | *.dbproj.schemaview 222 | *.jfm 223 | *.pfx 224 | *.publishsettings 225 | orleans.codegen.cs 226 | 227 | # Including strong name files can present a security risk 228 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 229 | #*.snk 230 | 231 | # Since there are multiple workflows, uncomment next line to ignore bower_components 232 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 233 | #bower_components/ 234 | 235 | # RIA/Silverlight projects 236 | Generated_Code/ 237 | 238 | # Backup & report files from converting an old project file 239 | # to a newer Visual Studio version. Backup files are not needed, 240 | # because we have git ;-) 241 | _UpgradeReport_Files/ 242 | Backup*/ 243 | UpgradeLog*.XML 244 | UpgradeLog*.htm 245 | ServiceFabricBackup/ 246 | *.rptproj.bak 247 | 248 | # SQL Server files 249 | *.mdf 250 | *.ldf 251 | *.ndf 252 | 253 | # Business Intelligence projects 254 | *.rdl.data 255 | *.bim.layout 256 | *.bim_*.settings 257 | *.rptproj.rsuser 258 | *- Backup*.rdl 259 | 260 | # Microsoft Fakes 261 | FakesAssemblies/ 262 | 263 | # GhostDoc plugin setting file 264 | *.GhostDoc.xml 265 | 266 | # Node.js Tools for Visual Studio 267 | .ntvs_analysis.dat 268 | node_modules/ 269 | 270 | # Visual Studio 6 build log 271 | *.plg 272 | 273 | # Visual Studio 6 workspace options file 274 | *.opt 275 | 276 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 277 | *.vbw 278 | 279 | # Visual Studio LightSwitch build output 280 | **/*.HTMLClient/GeneratedArtifacts 281 | **/*.DesktopClient/GeneratedArtifacts 282 | **/*.DesktopClient/ModelManifest.xml 283 | **/*.Server/GeneratedArtifacts 284 | **/*.Server/ModelManifest.xml 285 | _Pvt_Extensions 286 | 287 | # Paket dependency manager 288 | .paket/paket.exe 289 | paket-files/ 290 | 291 | # FAKE - F# Make 292 | .fake/ 293 | 294 | # JetBrains Rider 295 | .idea/ 296 | *.sln.iml 297 | 298 | # CodeRush personal settings 299 | .cr/personal 300 | 301 | # Python Tools for Visual Studio (PTVS) 302 | __pycache__/ 303 | *.pyc 304 | 305 | # Cake - Uncomment if you are using it 306 | # tools/** 307 | # !tools/packages.config 308 | 309 | # Tabs Studio 310 | *.tss 311 | 312 | # Telerik's JustMock configuration file 313 | *.jmconfig 314 | 315 | # BizTalk build output 316 | *.btp.cs 317 | *.btm.cs 318 | *.odx.cs 319 | *.xsd.cs 320 | 321 | # OpenCover UI analysis results 322 | OpenCover/ 323 | 324 | # Azure Stream Analytics local run output 325 | ASALocalRun/ 326 | 327 | # MSBuild Binary and Structured Log 328 | *.binlog 329 | 330 | # NVidia Nsight GPU debugger configuration file 331 | *.nvuser 332 | 333 | # MFractors (Xamarin productivity tool) working folder 334 | .mfractor/ 335 | 336 | # Local History for Visual Studio 337 | .localhistory/ 338 | 339 | # BeatPulse healthcheck temp database 340 | healthchecksdb -------------------------------------------------------------------------------- /BeadSort/BeadSort.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /BeadSort/Program.cs: -------------------------------------------------------------------------------- 1 | using SortExtravaganza.Common; 2 | using System; 3 | using System.Linq; 4 | 5 | namespace BeadSort 6 | { 7 | class BeadSort 8 | { 9 | 10 | static int[] Sort(int[] array) 11 | { 12 | bool[,] beads = new bool[10,100]; 13 | //Find maximum element 14 | int max = array.Max(); 15 | 16 | for (int i = 0; i < array.Length; i++) 17 | { 18 | for (int j = 0; j < array[i]; j++) 19 | { 20 | beads[i, j] = true; 21 | } 22 | } 23 | 24 | for (int j = 0; j < max; j++) 25 | { 26 | int sum = 0; 27 | for (int i = 0; i < array.Length; i++) 28 | { 29 | if (beads[i, j] != false) 30 | { 31 | sum++; 32 | beads[i, j] = false; 33 | } 34 | } 35 | 36 | for(int i = array.Length - sum; i < array.Length; i++) 37 | { 38 | beads[i, j] = true; 39 | } 40 | } 41 | 42 | //Put sorted values in array 43 | for(int i = 0; i < array.Length; i++) 44 | { 45 | int j; 46 | for (j = 0; j < max && beads[i, j] != false; j++) ; 47 | array[i] = j; 48 | } 49 | 50 | return array; 51 | } 52 | 53 | static void Main(string[] args) 54 | { 55 | int[] array = { 53, 12, 18, 94, 62, 20, 38, 61, 71, 7 }; 56 | CommonFunctions.PrintInitial(array); 57 | var sortedArray = Sort(array); 58 | CommonFunctions.PrintFinal(sortedArray); 59 | Console.ReadKey(); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /BitonicMergeSort/BitonicMergeSort.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /BitonicMergeSort/Program.cs: -------------------------------------------------------------------------------- 1 | using SortExtravaganza.Common; 2 | using System; 3 | 4 | namespace BitonicMergeSort 5 | { 6 | /// 7 | /// Bitonic merge sort only works with collections that are powers of 2. 8 | /// 9 | class BitonicMergeSort 10 | { 11 | static void Swap(ref T leftHandSide, ref T rightHandSide) 12 | { 13 | T temp; 14 | temp = leftHandSide; 15 | leftHandSide = rightHandSide; 16 | rightHandSide = temp; 17 | } 18 | static void CompareAndSwap(int[] array, int i, int j, int direction) 19 | { 20 | int k; 21 | 22 | k = array[i] > array[j] ? 1 : 0; 23 | 24 | if (direction == k) //If the order the elements are currently in DOES NOT match the sort direction (array[i] > array[j])... 25 | { 26 | //...Swap the elements so they DO match the sort direction 27 | Swap(ref array[i], ref array[j]); 28 | } 29 | } 30 | 31 | //This method recursively sorts a bitonic sequence in ascending order, 32 | //if dir = 1, and in descending order otherwise (means dir=0). 33 | //The sequence to be sorted starts at index position low, 34 | //the parameter count is the number of elements to be sorted. 35 | static void BitonicMerge(int[] array, int low, int count, int direction) 36 | { 37 | if (count > 1) 38 | { 39 | int k = count / 2; 40 | for (int i = low; i < low + k; i++) 41 | { 42 | CompareAndSwap(array, i, i + k, direction); 43 | } 44 | BitonicMerge(array, low, k, direction); 45 | BitonicMerge(array, low + k, k, direction); 46 | } 47 | } 48 | 49 | //This function first produces a bitonic sequence by recursively 50 | //sorting its two halves in opposite sorting directions, and then 51 | //calls BitonicMerge to make them in the same direction 52 | static void BitonicSort(int[] array, int low, int count, int direction) 53 | { 54 | if (count > 1) 55 | { 56 | int k = count / 2; 57 | 58 | // sort left side in ascending order 59 | BitonicSort(array, low, k, 1); 60 | 61 | // sort right side in descending order 62 | BitonicSort(array, low + k, k, 0); 63 | 64 | //Merge entire sequence in ascending order 65 | BitonicMerge(array, low, count, direction); 66 | } 67 | } 68 | 69 | public static void Main() 70 | { 71 | int[] array = { 66, 98, 11, 43, 7, 28, 14, 49, 77, 61, 31, 12, 71, 93, 15, 2 }; 72 | int length = array.Length; 73 | 74 | Console.WriteLine("Bitonic Merge Sort"); 75 | 76 | CommonFunctions.PrintInitial(array); 77 | 78 | BitonicSort(array, 0 /*low value*/, length, 1); //1 is for sorting in ascending order 79 | 80 | CommonFunctions.PrintFinal(array); 81 | Console.ReadLine(); 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /BogoSort/BogoSort.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /BogoSort/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace BogoSort 6 | { 7 | class BogoSort 8 | { 9 | static void Main(string[] args) 10 | { 11 | List list = new List() { 2, 1, 3, 0 }; 12 | Console.WriteLine("BogoSort"); 13 | Console.WriteLine("Sorting..."); 14 | Sort(list); 15 | Console.WriteLine("Press any key to exit."); 16 | Console.ReadKey(); 17 | } 18 | 19 | static void Sort(List list) 20 | { 21 | int iteration = 0; 22 | while (!IsSorted(list)) 23 | { 24 | PrintIteration(list, iteration); 25 | list = Shuffle(list); //Shuffle the numbers randomly 26 | iteration++; 27 | } 28 | 29 | PrintIteration(list, iteration); //Print the final, sorted iteration 30 | Console.WriteLine(); 31 | Console.WriteLine("BogoSort completed after {0} iterations.", iteration); 32 | } 33 | 34 | static void PrintIteration(List list, int iteration) 35 | { 36 | Console.Write("BogoSort iteration #{0}: ", iteration); 37 | foreach (var value in list) 38 | { 39 | Console.Write($"{value} "); 40 | } 41 | Console.WriteLine(); 42 | } 43 | static bool IsSorted(List list) 44 | { 45 | for (int i = 0; i < list.Count - 1; i++) 46 | { 47 | if (list[i] > list[i + 1]) 48 | return false; 49 | } 50 | 51 | return true; 52 | } 53 | 54 | //Shuffle algorithm based on Fisher-Yates method. 55 | static List Shuffle(List numbers) 56 | { 57 | Random r = new Random(); 58 | //Step 1: For each unshuffled item in the collection 59 | for (int n = numbers.Count - 1; n > 0; --n) 60 | { 61 | //Step 2: Randomly pick an item which has not been shuffled 62 | int k = r.Next(n + 1); 63 | 64 | //Step 3: Swap the selected item with the last "unstruck" item in the collection 65 | int temp = numbers[n]; 66 | numbers[n] = numbers[k]; 67 | numbers[k] = temp; 68 | } 69 | return numbers; 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /BubbleSort/BubbleSort.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /BubbleSort/Program.cs: -------------------------------------------------------------------------------- 1 | using SortExtravaganza.Common; 2 | using System; 3 | 4 | namespace BubbleSort 5 | { 6 | class BubbleSort 7 | { 8 | public static void Main(string[] args) 9 | { 10 | int[] array = { 92, 28, 3, 71, 50, 14, 24, 20, 66, 70, 45, 17, 9, 99, 38 }; 11 | int temp; 12 | Console.WriteLine("Bubble Sort"); 13 | CommonFunctions.PrintInitial(array); 14 | //1. For each item in the array... 15 | for (int i = 0; i <= array.Length - 2; i++) 16 | { 17 | for (int j = 0; j <= array.Length - 2; j++) 18 | { 19 | //2. ...if two adjoining elements are in the wrong order, swap them. 20 | if (array[j] > array[j + 1]) 21 | { 22 | temp = array[j + 1]; 23 | array[j + 1] = array[j]; 24 | array[j] = temp; 25 | } 26 | //3. Repeat this for all pairs of elements. 27 | } 28 | } 29 | CommonFunctions.PrintFinal(array); 30 | Console.ReadLine(); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /BucketSort/BucketSort.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /BucketSort/Program.cs: -------------------------------------------------------------------------------- 1 | using SortExtravaganza.Common; 2 | using System; 3 | using System.Collections.Generic; 4 | 5 | namespace BucketSort 6 | { 7 | //Bucket Sort breaks a list of items into sublists, so that you can use another sorting 8 | //algorithm to sort the sublists. In this case, we will use insertion sort. 9 | //To use this algorithm, we must determine how many buckets our numbers can be sorted into. 10 | //Because our starting array has numbers in the range 1-99, we will use 10 buckets based on 11 | //the least-significant digit of the value. 12 | class BucketSort 13 | { 14 | public static List Sort(params int[] x) 15 | { 16 | List sortedArray = new List(); 17 | 18 | int numOfBuckets = 10; 19 | 20 | //Create buckets 21 | List[] buckets = new List[numOfBuckets]; 22 | for (int i = 0; i < numOfBuckets; i++) 23 | { 24 | buckets[i] = new List(); 25 | } 26 | 27 | //Iterate through the passed array and add each integer to the appropriate bucket 28 | for (int i = 0; i < x.Length; i++) 29 | { 30 | int bucket = (x[i] / numOfBuckets); 31 | buckets[bucket].Add(x[i]); 32 | } 33 | 34 | //Sort each bucket and add it to the result List 35 | for (int i = 0; i < numOfBuckets; i++) 36 | { 37 | List temp = InsertionSort(buckets[i]); 38 | sortedArray.AddRange(temp); 39 | } 40 | return sortedArray; 41 | } 42 | 43 | //Insertion Sort 44 | public static List InsertionSort(List input) 45 | { 46 | for (int i = 1; i < input.Count; i++) 47 | { 48 | //2. Store the current value in a variable 49 | int currentValue = input[i]; 50 | int pointer = i - 1; 51 | 52 | //3. As long as we are pointing to a valid value in the array... 53 | while (pointer >= 0) 54 | { 55 | //4. If the current value is less than the value we are pointing at... 56 | if (currentValue < input[pointer]) 57 | { 58 | //5. Move the pointed-at value up one space, and insert the current value at the pointed-at position. 59 | input[pointer + 1] = input[pointer]; 60 | input[pointer] = currentValue; 61 | } 62 | else break; 63 | } 64 | } 65 | 66 | return input; 67 | } 68 | static void Main(string[] args) 69 | { 70 | int[] array = new int[] { 43, 17, 87, 92, 31, 6, 96, 13, 66, 62, 4 }; 71 | 72 | Console.WriteLine("Bucket Sort"); 73 | 74 | CommonFunctions.PrintInitial(array); 75 | 76 | List sorted = Sort(array); 77 | 78 | CommonFunctions.PrintFinal(sorted); 79 | Console.ReadLine(); 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /CocktailShakerSort/CocktailShakerSort.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /CocktailShakerSort/Program.cs: -------------------------------------------------------------------------------- 1 | using SortExtravaganza.Common; 2 | using System; 3 | 4 | namespace CocktailShakerSort 5 | { 6 | //Cocktail Shaker Sort is pretty much like Bubble Sort, except that it moves items both ways 7 | //(higher to lower and lower to higher) on each pass. 8 | class CocktailShakerSort 9 | { 10 | 11 | static void Sort(int[] array) 12 | { 13 | bool isSwapped = true; 14 | int start = 0; 15 | int end = array.Length; 16 | 17 | while (isSwapped == true) 18 | { 19 | 20 | //Reset this flag. It is possible for this to be true from a prior iteration. 21 | isSwapped = false; 22 | 23 | //Do a bubble sort on this array, from low to high. If something changed, make isSwapped true. 24 | for (int i = start; i < end - 1; ++i) 25 | { 26 | if (array[i] > array[i + 1]) 27 | { 28 | int temp = array[i]; 29 | array[i] = array[i + 1]; 30 | array[i + 1] = temp; 31 | isSwapped = true; 32 | } 33 | } 34 | 35 | //If no swaps are made, the array is sorted. 36 | if (isSwapped == false) 37 | break; 38 | 39 | //We need to reset the isSwapped flag for the high-to-low pass 40 | isSwapped = false; 41 | 42 | //The item we just moved is in its rightful place, so we no longer need to consider it unsorted. 43 | end -= 1; 44 | 45 | //Now we bubble sort from high to low 46 | for (int i = end - 1; i >= start; i--) 47 | { 48 | if (array[i] > array[i + 1]) 49 | { 50 | int temp = array[i]; 51 | array[i] = array[i + 1]; 52 | array[i + 1] = temp; 53 | isSwapped = true; 54 | } 55 | } 56 | 57 | //Finally, we need to increase the starting point for the next low-to-high pass. 58 | start += 1; 59 | } 60 | } 61 | 62 | public static void Main() 63 | { 64 | int[] array = { 15, 10, 83, 5, 7, 42, 65, 50, 58, 71, 61 }; 65 | 66 | CommonFunctions.PrintInitial(array); 67 | 68 | Sort(array); 69 | 70 | CommonFunctions.PrintFinal(array); 71 | 72 | Console.ReadKey(); 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /CombSort/CombSort.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /CombSort/Program.cs: -------------------------------------------------------------------------------- 1 | using SortExtravaganza.Common; 2 | using System; 3 | 4 | namespace CombSort 5 | { 6 | //Comb Sort sorts a list by comparing values across a "gap" and switching them if they are 7 | //out of order. On each iteration through the list, the gap shrinks by a certain amount, 8 | //until it is comparing elements that are next to each other. 9 | //The gap is length of the list N divided by shrink factor K. 10 | //K has been empirically proven to be most efficient at 1.3. 11 | class CombSort 12 | { 13 | static int GetNextGap(int gap) 14 | { 15 | //The "shrink factor", empirically shown to be 1.3 16 | gap = (gap * 10) / 13; 17 | if (gap < 1) 18 | { 19 | return 1; 20 | } 21 | return gap; 22 | } 23 | 24 | static void Sort(int[] array) 25 | { 26 | int length = array.Length; 27 | 28 | int gap = length; 29 | 30 | //We initialize this as true to enter the while loop. 31 | bool swapped = true; 32 | 33 | while (gap != 1 || swapped == true) 34 | { 35 | gap = GetNextGap(gap); 36 | 37 | //Set swapped as false. Will go to true when two values are swapped. 38 | swapped = false; 39 | 40 | //Compare all elements with current gap 41 | for (int i = 0; i < length - gap; i++) 42 | { 43 | if (array[i] > array[i + gap]) 44 | { 45 | //Swap 46 | int temp = array[i]; 47 | array[i] = array[i + gap]; 48 | array[i + gap] = temp; 49 | 50 | swapped = true; 51 | } 52 | } 53 | } 54 | } 55 | 56 | public static void Main() 57 | { 58 | int[] array = { 10, 28, 1, 55, 6, 21, 36, 3, 45, 15, 0 }; 59 | 60 | Console.WriteLine("Comb Sort"); 61 | 62 | CommonFunctions.PrintInitial(array); 63 | 64 | Sort(array); 65 | 66 | CommonFunctions.PrintFinal(array); 67 | Console.ReadLine(); 68 | 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Common/Common.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.0 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Common/CommonFunctions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace SortExtravaganza.Common 5 | { 6 | public static class CommonFunctions 7 | { 8 | public static void PrintInitial(List list) 9 | { 10 | Console.Write("Initial array is: "); 11 | Print(list.ToArray()); 12 | } 13 | 14 | public static void PrintInitial(int[] array) 15 | { 16 | Console.Write("Initial array is: "); 17 | Print(array); 18 | } 19 | 20 | public static void PrintFinal(List list) 21 | { 22 | Console.Write("Sorted array is: "); 23 | Print(list.ToArray()); 24 | } 25 | 26 | public static void PrintFinal(int[] array) 27 | { 28 | Console.Write("Sorted array is: "); 29 | Print(array); 30 | } 31 | 32 | private static void Print(int[] array) 33 | { 34 | int length = array.Length; 35 | for (int i = 0; i < length; ++i) 36 | { 37 | Console.Write(array[i] + " "); 38 | } 39 | Console.WriteLine(); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /CountingSort/CountingSort.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /CountingSort/Program.cs: -------------------------------------------------------------------------------- 1 | using SortExtravaganza.Common; 2 | using System; 3 | 4 | namespace CountingSort 5 | { 6 | //This algorithm assumes we know the range of correct values. 7 | //For this demo,we are assuming a range of 0-100. 8 | class CountingSort 9 | { 10 | static void Sort(int[] array) 11 | { 12 | int length = array.Length; 13 | 14 | //Create a new "output" array 15 | int[] output = new int[length]; 16 | 17 | //Create a new "counting" array which stores the count of each unique number 18 | int[] count = new int[100]; 19 | for (int i = 0; i < 100; ++i) 20 | { 21 | count[i] = 0; 22 | } 23 | for (int i = 0; i < length; ++i) 24 | { 25 | ++count[array[i]]; 26 | } 27 | 28 | //Change count[i] so that count[i] now contains actual position of 29 | //this character in the output array. 30 | for (int i = 1; i <= 99; ++i) 31 | { 32 | count[i] += count[i - 1]; 33 | } 34 | 35 | //Build the output array. 36 | //To make this sorting algorithm stable, we are operating in reverse order. 37 | for (int i = length - 1; i >= 0; i--) 38 | { 39 | output[count[array[i]] - 1] = array[i]; 40 | --count[array[i]]; 41 | } 42 | 43 | //Copy the output array to the final array. 44 | for (int i = 0; i < length; ++i) 45 | { 46 | array[i] = output[i]; 47 | } 48 | } 49 | 50 | public static void Main() 51 | { 52 | int[] array = { 64, 11, 83, 8, 13, 45, 92, 98, 55, 17, 41, 81, 11, 64, 14, 41, 11, 92 }; 53 | 54 | Console.WriteLine("Counting Sort"); 55 | 56 | CommonFunctions.PrintInitial(array); 57 | 58 | Sort(array); 59 | 60 | CommonFunctions.PrintFinal(array); 61 | Console.ReadLine(); 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /GnomeSort/GnomeSort.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /GnomeSort/Program.cs: -------------------------------------------------------------------------------- 1 | using SortExtravaganza.Common; 2 | using System; 3 | 4 | namespace GnomeSort 5 | { 6 | class GnomeSort 7 | { 8 | 9 | static void Sort(int[] arr, int length) 10 | { 11 | int index = 0; 12 | 13 | while (index < length)//If there is no pot next to the gnome, he is done. 14 | { 15 | if (index == 0) //If the gnome is at the start of the line... 16 | { 17 | index++;//he steps forward 18 | } 19 | 20 | if (arr[index] >= arr[index - 1])//If the pots next to the gnome are in the correct order... 21 | { 22 | index++;//he goes to the next pot 23 | } 24 | else //If the pots are in the wrong order, he switches them. 25 | { 26 | int temp = arr[index]; 27 | arr[index] = arr[index - 1]; 28 | arr[index - 1] = temp; 29 | index--; 30 | } 31 | } 32 | return; 33 | } 34 | 35 | public static void Main() 36 | { 37 | int[] array = { 84, 61, 15, 2, 7, 55, 19, 40, 78, 33 }; 38 | 39 | Console.WriteLine("Gnome Sort"); 40 | 41 | CommonFunctions.PrintInitial(array); 42 | 43 | Sort(array, array.Length); 44 | 45 | CommonFunctions.PrintFinal(array); 46 | 47 | Console.ReadKey(); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /HeapSort/HeapSort.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /HeapSort/Program.cs: -------------------------------------------------------------------------------- 1 | using SortExtravaganza.Common; 2 | using System; 3 | namespace HeapSortDemo 4 | { 5 | //HeapSort takes advantage of a heap data structure to sort an unsorted list. 6 | //It can be thought of as an improved version of selection sort. 7 | //ALGORITHM: 8 | //1. Build a "max heap" out of the unsorted data (a heap with the largest value as the first node). 9 | //2. Swap the first element of the heap with the final element. That element (now at final position) is considered sorted. 10 | // In effect, this makes the largest element the last one in the considered range. 11 | //3. Decrease the range of considered elements (those still needing to be sorted) by 1. 12 | //4. Continue until the considered range of elements is 1. 13 | public class HeapSort 14 | { 15 | static void Sort(int[] array) 16 | { 17 | var length = array.Length; 18 | for (int i = length / 2 - 1; i >= 0; i--) 19 | { 20 | Heapify(array, length, i); 21 | } 22 | for (int i = length - 1; i >= 0; i--) 23 | { 24 | int temp = array[0]; 25 | array[0] = array[i]; 26 | array[i] = temp; 27 | Heapify(array, i, 0); 28 | } 29 | } 30 | 31 | //Rebuilds the heap 32 | static void Heapify(int[] array, int length, int i) 33 | { 34 | int largest = i; 35 | int left = 2 * i + 1; 36 | int right = 2 * i + 2; 37 | if (left < length && array[left] > array[largest]) 38 | { 39 | largest = left; 40 | } 41 | if (right < length && array[right] > array[largest]) 42 | { 43 | largest = right; 44 | } 45 | if (largest != i) 46 | { 47 | int swap = array[i]; 48 | array[i] = array[largest]; 49 | array[largest] = swap; 50 | Heapify(array, length, largest); 51 | } 52 | } 53 | 54 | public static void Main() 55 | { 56 | int[] arr = { 74, 19, 24, 5, 8, 79, 42, 15, 20, 53, 11 }; 57 | Console.WriteLine("Heap Sort"); 58 | 59 | CommonFunctions.PrintInitial(arr); 60 | 61 | Sort(arr); 62 | 63 | CommonFunctions.PrintFinal(arr); 64 | 65 | Console.ReadKey(); 66 | } 67 | } 68 | } -------------------------------------------------------------------------------- /InsertionSort/InsertionSort.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /InsertionSort/Program.cs: -------------------------------------------------------------------------------- 1 | using SortExtravaganza.Common; 2 | using System; 3 | 4 | namespace InsertionSort 5 | { 6 | class InsertionSort 7 | { 8 | static void Main(string[] args) 9 | { 10 | int[] array = new int[15] { 55, 97, 76, 60, 4, 18, 37, 34, 88, 51, 43, 49, 19, 12, 63 }; 11 | Console.WriteLine("Insertion Sort"); 12 | 13 | CommonFunctions.PrintInitial(array); 14 | 15 | //1. For each value in the array... 16 | for (int i = 1; i < array.Length; ++i) 17 | { 18 | //2. Store the current value in a variable. 19 | int currentValue = array[i]; 20 | int pointer = i - 1; 21 | 22 | //3. While we are pointing to a valid value... 23 | //4. If the current value is less than the value we are pointing at... 24 | while (pointer >= 0 && array[pointer] > currentValue) 25 | { 26 | //5. Then move the pointed-at value up one space, and store the 27 | // current value at the pointed-at position. 28 | array[pointer + 1] = array[pointer]; 29 | pointer -= 1; 30 | } 31 | array[pointer + 1] = currentValue; 32 | } 33 | 34 | CommonFunctions.PrintFinal(array); 35 | 36 | Console.ReadLine(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /LibrarySort/LibrarySort.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /LibrarySort/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace LibrarySort 4 | { 5 | class Program 6 | { 7 | static void Main(string[] args) 8 | { 9 | Console.WriteLine("Hello World!"); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /MergeSort/MergeSort.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /MergeSort/Program.cs: -------------------------------------------------------------------------------- 1 | using SortExtravaganza.Common; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | 6 | namespace MergeSort 7 | { 8 | class MergeSort 9 | { 10 | static void Main(string[] args) 11 | { 12 | List unsorted = new List() { 42, 13, 86, 9, 10, 55, 71 }; 13 | List sorted; 14 | 15 | Console.WriteLine("Merge Sort"); 16 | 17 | CommonFunctions.PrintInitial(unsorted); 18 | 19 | sorted = Sort(unsorted); 20 | 21 | CommonFunctions.PrintFinal(sorted); 22 | Console.ReadLine(); 23 | } 24 | 25 | //Uses recursion to break the collection into progressively smaller collections. 26 | //Eventually, each collection will have just one element. 27 | private static List Sort(List unsorted) 28 | { 29 | if (unsorted.Count <= 1) 30 | return unsorted; 31 | 32 | List left = new List(); 33 | List right = new List(); 34 | 35 | int median = unsorted.Count / 2; 36 | for (int i = 0; i < median; i++) //Dividing the unsorted list 37 | { 38 | left.Add(unsorted[i]); 39 | } 40 | for (int i = median; i < unsorted.Count; i++) 41 | { 42 | right.Add(unsorted[i]); 43 | } 44 | 45 | left = Sort(left); 46 | right = Sort(right); 47 | return Merge(left, right); 48 | } 49 | 50 | //Method takes two sorted "sublists" (left and right) of original list and merges them into a new colletion 51 | private static List Merge(List left, List right) 52 | { 53 | List result = new List(); //The new collection 54 | 55 | while (left.Any() || right.Any()) 56 | { 57 | if (left.Any() && right.Any()) 58 | { 59 | if (left.First() <= right.First()) //Comparing the first element of each sublist to see which is smaller 60 | { 61 | result.Add(left.First()); 62 | left.Remove(left.First()); 63 | } 64 | else 65 | { 66 | result.Add(right.First()); 67 | right.Remove(right.First()); 68 | } 69 | } 70 | else if (left.Any()) 71 | { 72 | result.Add(left.First()); 73 | left.Remove(left.First()); 74 | } 75 | else if (right.Any()) 76 | { 77 | result.Add(right.First()); 78 | 79 | right.Remove(right.First()); 80 | } 81 | } 82 | return result; 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /OddEvenSort/OddEvenSort.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /OddEvenSort/Program.cs: -------------------------------------------------------------------------------- 1 | using SortExtravaganza.Common; 2 | using System; 3 | 4 | namespace OddEvenSort 5 | { 6 | //Odd Even Sort is a variation on Bubble Sort. On each traversal of the collection, 7 | //the algorithm compares the values next to each other. On the first traversal, it will compare 8 | //the values in odd-numbered positions against the next-highest position. On the second traversal, 9 | //it will compare values in even numbered positions against their next-higher position. 10 | class OddEvenSort 11 | { 12 | public static void Sort(int[] array, int length) 13 | { 14 | bool isSorted = false; 15 | 16 | while (!isSorted) 17 | { 18 | isSorted = true; 19 | 20 | //Swap i and i+1 if they are out of order, for i == odd numbers 21 | for (int i = 1; i <= length - 2; i = i + 2) 22 | { 23 | if (array[i] > array[i + 1]) 24 | { 25 | int temp = array[i]; 26 | array[i] = array[i + 1]; 27 | array[i + 1] = temp; 28 | isSorted = false; 29 | } 30 | } 31 | 32 | //Swap i and i+1 if they are out of order, for i == even numbers 33 | for (int i = 0; i <= length - 2; i = i + 2) 34 | { 35 | if (array[i] > array[i + 1]) 36 | { 37 | int temp = array[i]; 38 | array[i] = array[i + 1]; 39 | array[i + 1] = temp; 40 | isSorted = false; 41 | } 42 | } 43 | } 44 | return; 45 | } 46 | 47 | public static void Main() 48 | { 49 | int[] array = { 71, 42, 19, 3, 33, 28, 0, 89, 44, 2, 81 }; 50 | int length = array.Length; 51 | 52 | Console.WriteLine("Odd-Even Sort"); 53 | 54 | CommonFunctions.PrintInitial(array); 55 | 56 | Sort(array, length); 57 | 58 | CommonFunctions.PrintFinal(array); 59 | 60 | Console.ReadKey(); 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /PigeonholeSort/PigeonholeSort.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /PigeonholeSort/Program.cs: -------------------------------------------------------------------------------- 1 | using SortExtravaganza.Common; 2 | using System; 3 | using System.Linq; 4 | 5 | namespace PigeonholeSort 6 | { 7 | class PigeonholeSort 8 | { 9 | public static void Sort(int[] array) 10 | { 11 | int length = array.Length; 12 | 13 | //Find the range of values in the array 14 | int min = array.Min(); 15 | int max = array.Max(); 16 | int range = max - min + 1; 17 | 18 | //Create a set of pigeonholes with the size of the range of values 19 | int[] pigeonholes = new int[range]; 20 | for (int i = 0; i < length; i++) 21 | { 22 | pigeonholes[i] = 0; 23 | } 24 | 25 | //For each value in the array, mark how many times the index of the pigeonhole appeared in the root array. 26 | for (int i = 0; i < length; i++) 27 | { 28 | pigeonholes[array[i] - min]++; 29 | } 30 | 31 | int index = 0; 32 | 33 | //Use the pigeonhole array to sort the main array. 34 | for (int j = 0; j < range; j++) 35 | { 36 | //We are using a post-decrement here to keep track of the number of values we've already added to the array. 37 | while (pigeonholes[j]-- > 0) 38 | { 39 | array[index] = j + min; 40 | index++; 41 | } 42 | } 43 | 44 | } 45 | 46 | static void Main() 47 | { 48 | int[] array = { 51, 18, 99, 23, 40, 1, 82, 85, 18, 12, 76 }; 49 | 50 | Console.WriteLine("Pigeonhole Sort"); 51 | 52 | CommonFunctions.PrintInitial(array); 53 | 54 | Sort(array); 55 | 56 | CommonFunctions.PrintFinal(array); 57 | 58 | Console.ReadLine(); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /QuickSort/Program.cs: -------------------------------------------------------------------------------- 1 | using SortExtravaganza.Common; 2 | using System; 3 | 4 | namespace QuickSort 5 | { 6 | //QuickSort is an efficient sorting algorithm that divides the collection of numbers 7 | //into progressively smaller "partitions" within the collection and sorts those partitions 8 | //in place. The result is a fully sorted collection. 9 | //ALGORITHM: 10 | //1. Select a pivot point (implementation below selects the last value). 11 | //2. Reorder the collection so that all values less than the pivot are before that pivot, and all values 12 | // greater than the pivot are after the pivot. 13 | // After this partitioning, the pivot element is in its final position. 14 | //3. Recursively do this partitioning on the "less than pivot" set and the "greater than pivot" set. 15 | // Continue recursively applying this algorithm until the set is sorted. 16 | class QuickSort 17 | { 18 | static int Partition(int[] array, int low, 19 | int high) 20 | { 21 | //1. Select a pivot point. 22 | int pivot = array[high]; 23 | 24 | int lowIndex = (low - 1); 25 | 26 | //2. Reorder the collection. 27 | for (int j = low; j < high; j++) 28 | { 29 | if (array[j] <= pivot) 30 | { 31 | lowIndex++; 32 | 33 | int temp = array[lowIndex]; 34 | array[lowIndex] = array[j]; 35 | array[j] = temp; 36 | } 37 | } 38 | 39 | int temp1 = array[lowIndex + 1]; 40 | array[lowIndex + 1] = array[high]; 41 | array[high] = temp1; 42 | 43 | return lowIndex + 1; 44 | } 45 | 46 | static void Sort(int[] array, int low, int high) 47 | { 48 | if (low < high) 49 | { 50 | int partitionIndex = Partition(array, low, high); 51 | 52 | //3. Recursively continue sorting the array 53 | Sort(array, low, partitionIndex - 1); 54 | Sort(array, partitionIndex + 1, high); 55 | } 56 | } 57 | 58 | public static void Main() 59 | { 60 | int[] array = { 72, 12, 6, 33, 81, 97, 37, 59, 52, 1, 20 }; 61 | int length = array.Length; 62 | 63 | Console.WriteLine("QuickSort"); 64 | CommonFunctions.PrintInitial(array); 65 | Sort(array, 0, length - 1); 66 | 67 | CommonFunctions.PrintFinal(array); 68 | 69 | Console.ReadKey(); 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /QuickSort/QuickSort.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # The Sorting Algorithm Family Reunion 2 | 3 | This project contains many example C# projects demonstrating how can implement various sorting algorithms. 4 | 5 | Here is the publishing schedule for this series: 6 | 7 | Jul 18: [Selection Sort](https://exceptionnotfound.net/selection-sort-csharp-the-sorting-algorithm-family-reunion/) 8 | 9 | Jul 22: [Merge Sort](https://exceptionnotfound.net/merge-sort-csharp-the-sorting-algorithm-family-reunion/) 10 | 11 | Jul 25: [Insertion Sort](https://exceptionnotfound.net/insertion-sort-csharp-the-sorting-algorithm-family-reunion/) 12 | 13 | Jul 29: [Shell Sort](https://exceptionnotfound.net/shell-sort-csharp-the-sorting-algorithm-family-reunion/) 14 | 15 | Aug 1: [Comb Sort](https://exceptionnotfound.net/comb-sort-csharp-the-sorting-algorithm-family-reunion/) 16 | 17 | Aug 5: [Quick Sort](https://exceptionnotfound.net/quick-sort-csharp-the-sorting-algorithm-family-reunion/) 18 | 19 | Aug 8: [Heap Sort](https://exceptionnotfound.net/heap-sort-csharp-the-sorting-algorithm-family-reunion/) 20 | 21 | Aug 12: [Cocktail Shaker Sort](https://exceptionnotfound.net/cocktail-shaker-sort-csharp-the-sorting-algorithm-family-reunion/) 22 | 23 | Aug 15: [Bogo Sort](https://exceptionnotfound.net/bogosort-csharp-the-sorting-algorithm-family-reunion/) 24 | 25 | Aug 19: [Bubble Sort](https://exceptionnotfound.net/bubble-sort-csharp-the-sorting-algorithm-family-reunion/) 26 | 27 | Aug 22: [Odd-Even Sort](https://exceptionnotfound.net/odd-even-sort-csharp-the-sorting-algorithm-family-reunion/) 28 | 29 | Aug 26: [Counting Sort](https://exceptionnotfound.net/counting-sort-csharp-the-sorting-algorithm-family-reunion/) 30 | 31 | Aug 29: [Bitonic Merge Sort](https://exceptionnotfound.net/bitonic-merge-sort-csharp-the-sorting-algorithm-family-reunion/) 32 | 33 | Sep 5: [Pigeonhole Sort](https://exceptionnotfound.net/pigeonhole-sort-csharp-the-sorting-algorithm-family-reunion/) 34 | 35 | Sep 9: [Gnome Sort](https://exceptionnotfound.net/gnome-sort-csharp-the-sorting-algorithm-family-reunion/) 36 | 37 | Sep 12: [Radix Sort](https://exceptionnotfound.net/radix-sort-csharp-the-sorting-algorithm-family-reunion/) 38 | 39 | Sep 16: [Bucket Sort](https://exceptionnotfound.net/bucket-sort-csharp-the-sorting-algorithm-family-reunion/) 40 | 41 | If you like this series, or these helped you out in some way, would you consider [buying me a coffee](https://www.buymeacoffee.com/exceptionnotfnd)? 42 | -------------------------------------------------------------------------------- /RadixSort/Program.cs: -------------------------------------------------------------------------------- 1 | using SortExtravaganza.Common; 2 | using System; 3 | using System.Linq; 4 | 5 | namespace RadixSort 6 | { 7 | class RadixSort 8 | { 9 | public static void Main() 10 | { 11 | int[] array = { 330, 8, 27, 4419, 55, 816, 419, 77, 622, 1234, 6, 9, 241, 1, 35, 7733, 4, 69 }; 12 | int length = array.Length; 13 | 14 | Console.WriteLine("Radix Sort"); 15 | CommonFunctions.PrintInitial(array); 16 | 17 | int max = array.Max(); 18 | for (int exp = 1; max / exp > 0; exp *= 10) 19 | { 20 | CountingSort(array, length, exp); 21 | } 22 | 23 | CommonFunctions.PrintFinal(array); 24 | Console.ReadLine(); 25 | } 26 | 27 | //This is a modified version of Counting Sort from an earlier post 28 | //We need to do Counting Sort against each group of integers, 29 | //where the groups are made based on the position of significant digits. 30 | //So, we use Counting Sort on the least-significant digit, then the next-least, etc. 31 | //After that, we concatenate the groups together to form the final array. 32 | public static void CountingSort(int[] array, int length, int exponent) 33 | { 34 | //Create a new "output" array 35 | int[] output = new int[length]; // output array 36 | int i; 37 | 38 | //Create a new "counting" array which stores the count of each unique number 39 | int[] count = new int[10]; 40 | for (i = 0; i < 10; i++) 41 | { 42 | count[i] = 0; 43 | } 44 | for (i = 0; i < length; i++) 45 | { 46 | count[(array[i] / exponent) % 10]++; 47 | } 48 | 49 | //Change count[i] so that count[i] now contains actual position of 50 | //this character in the output array. 51 | for (i = 1; i < 10; i++) 52 | { 53 | count[i] += count[i - 1]; 54 | } 55 | 56 | //Build the output array. 57 | //This is the tricky part. 58 | for (i = length - 1; i >= 0; i--) 59 | { 60 | output[count[(array[i] / exponent) % 10] - 1] = array[i]; 61 | count[(array[i] / exponent) % 10]--; 62 | } 63 | 64 | //Copy the output array to the final array. 65 | for (i = 0; i < length; i++) 66 | { 67 | array[i] = output[i]; 68 | } 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /RadixSort/RadixSort.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /SelectionSort/Program.cs: -------------------------------------------------------------------------------- 1 | using SortExtravaganza.Common; 2 | using System; 3 | 4 | namespace SelectionSort 5 | { 6 | class SelectionSort 7 | { 8 | static void Main(string[] args) 9 | { 10 | int[] array = new int[15] { 45, 72, 58, 92, 26, 4, 13, 90, 81, 15, 33, 36, 47, 8, 54 }; 11 | int count = 15; 12 | Console.WriteLine("Selection Sort"); 13 | 14 | //First, output starting state of the array 15 | CommonFunctions.PrintInitial(array); 16 | 17 | int temp, smallest; 18 | 19 | //The algorithm builds the sorted list from the left. 20 | //1. For each item in the array... 21 | for (int i = 0; i < count - 1; i++) 22 | { 23 | //2. ...assume the first item is the smallest value 24 | smallest = i; 25 | //3. Cycle through the rest of the array 26 | for (int j = i + 1; j < count; j++) 27 | { 28 | //4. If any of the remaining values are smaller, find the smallest of these 29 | if (array[j] < array[smallest]) 30 | { 31 | smallest = j; 32 | } 33 | } 34 | //5. Swap the found-smallest value with the current value 35 | temp = array[smallest]; 36 | array[smallest] = array[i]; 37 | array[i] = temp; 38 | } 39 | 40 | //Output final state of the array 41 | CommonFunctions.PrintFinal(array); 42 | 43 | //We call ReadLine here to avoid noisy output in the command line. 44 | Console.ReadLine(); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /SelectionSort/SelectionSort.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /ShellSort/Program.cs: -------------------------------------------------------------------------------- 1 | using SortExtravaganza.Common; 2 | using System; 3 | 4 | namespace ShellSort 5 | { 6 | //The main idea behind Shell Sort is to exchange items which 7 | //are far apart. To that end, we do a "gapped" insertion sort, 8 | //in which the gap is the number of "subarrays" we will sort 9 | //independently of each other. 10 | class ShellSort 11 | { 12 | static int Sort(int[] array) 13 | { 14 | int length = array.Length; 15 | 16 | for (int h = length / 2; h > 0; h /= 2) 17 | { 18 | for (int i = h; i < length; i += 1) 19 | { 20 | int temp = array[i]; 21 | 22 | int j; 23 | for (j = i; j >= h && array[j - h] > temp; j -= h) 24 | { 25 | array[j] = array[j - h]; 26 | } 27 | 28 | array[j] = temp; 29 | } 30 | } 31 | return 0; 32 | } 33 | 34 | public static void Main() 35 | { 36 | int[] array = { 53, 19, 71, 3, 66, 62, 20, 84 }; 37 | 38 | Console.WriteLine("Shell Sort"); 39 | 40 | CommonFunctions.PrintInitial(array); 41 | 42 | Sort(array); 43 | 44 | CommonFunctions.PrintFinal(array); 45 | Console.ReadKey(); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /ShellSort/ShellSort.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /SortExtravaganza.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.28803.156 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SelectionSort", "SelectionSort\SelectionSort.csproj", "{B44301FC-95A2-4F6F-8084-6E6E6B09D4BD}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MergeSort", "MergeSort\MergeSort.csproj", "{20A04105-F66E-438D-886E-7748399196ED}" 9 | EndProject 10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BubbleSort", "BubbleSort\BubbleSort.csproj", "{4CE92EF7-25D0-4439-8E8B-74A07EDC1809}" 11 | EndProject 12 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InsertionSort", "InsertionSort\InsertionSort.csproj", "{5E80700F-79D7-4775-9F3D-DD3A0CE2E039}" 13 | EndProject 14 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BitonicMergeSort", "BitonicMergeSort\BitonicMergeSort.csproj", "{CD770637-866F-4AF5-A441-BF97FC1EE8CC}" 15 | EndProject 16 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PigeonholeSort", "PigeonholeSort\PigeonholeSort.csproj", "{439DD393-CBB0-47A3-B79E-41E0F3598DE0}" 17 | EndProject 18 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CountingSort", "CountingSort\CountingSort.csproj", "{8AE5B721-DB8E-461E-8D33-3C78E62E393D}" 19 | EndProject 20 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RadixSort", "RadixSort\RadixSort.csproj", "{C0836A01-E6C2-48B1-882B-D5255811CDFF}" 21 | EndProject 22 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CombSort", "CombSort\CombSort.csproj", "{CE0C8A45-81B5-4B1B-8E2E-7E979A65D67B}" 23 | EndProject 24 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BucketSort", "BucketSort\BucketSort.csproj", "{0B6104D1-4F64-4132-B8B3-651B1A8DBF06}" 25 | EndProject 26 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BogoSort", "BogoSort\BogoSort.csproj", "{829F9F8A-C5BD-4241-BC4C-0AAD7EE75C6F}" 27 | EndProject 28 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShellSort", "ShellSort\ShellSort.csproj", "{B47A24E0-6E4D-40C8-8F57-BC63E4706546}" 29 | EndProject 30 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Common", "Common\Common.csproj", "{23F445B8-CD5E-4221-84C2-06C7F61E11AA}" 31 | EndProject 32 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GnomeSort", "GnomeSort\GnomeSort.csproj", "{13E44E37-0C2E-4D68-8605-4D4C33147776}" 33 | EndProject 34 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CocktailShakerSort", "CocktailShakerSort\CocktailShakerSort.csproj", "{AE2D2AE7-AD30-4968-98BF-36553D749383}" 35 | EndProject 36 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HeapSort", "HeapSort\HeapSort.csproj", "{BDEA1C53-5092-44B6-9124-CED4EF97453A}" 37 | EndProject 38 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TimSort", "TimSort\TimSort.csproj", "{850D7C4C-1D54-40AF-8E3F-D43E5311D5DB}" 39 | EndProject 40 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuickSort", "QuickSort\QuickSort.csproj", "{C45BE097-7746-4FB2-A1B1-A2909C318EA0}" 41 | EndProject 42 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OddEvenSort", "OddEvenSort\OddEvenSort.csproj", "{F121AAFB-DEAA-473D-AFA6-AC0C9CBCE692}" 43 | EndProject 44 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TreeSort", "TreeSort\TreeSort.csproj", "{A4C7862F-0455-49BE-B2E1-D010C721364B}" 45 | EndProject 46 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BeadSort", "BeadSort\BeadSort.csproj", "{0B87CAFC-7EE2-48C2-9FE8-CD9F6257D9AB}" 47 | EndProject 48 | Global 49 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 50 | Debug|Any CPU = Debug|Any CPU 51 | Release|Any CPU = Release|Any CPU 52 | EndGlobalSection 53 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 54 | {B44301FC-95A2-4F6F-8084-6E6E6B09D4BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 55 | {B44301FC-95A2-4F6F-8084-6E6E6B09D4BD}.Debug|Any CPU.Build.0 = Debug|Any CPU 56 | {B44301FC-95A2-4F6F-8084-6E6E6B09D4BD}.Release|Any CPU.ActiveCfg = Release|Any CPU 57 | {B44301FC-95A2-4F6F-8084-6E6E6B09D4BD}.Release|Any CPU.Build.0 = Release|Any CPU 58 | {20A04105-F66E-438D-886E-7748399196ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 59 | {20A04105-F66E-438D-886E-7748399196ED}.Debug|Any CPU.Build.0 = Debug|Any CPU 60 | {20A04105-F66E-438D-886E-7748399196ED}.Release|Any CPU.ActiveCfg = Release|Any CPU 61 | {20A04105-F66E-438D-886E-7748399196ED}.Release|Any CPU.Build.0 = Release|Any CPU 62 | {4CE92EF7-25D0-4439-8E8B-74A07EDC1809}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 63 | {4CE92EF7-25D0-4439-8E8B-74A07EDC1809}.Debug|Any CPU.Build.0 = Debug|Any CPU 64 | {4CE92EF7-25D0-4439-8E8B-74A07EDC1809}.Release|Any CPU.ActiveCfg = Release|Any CPU 65 | {4CE92EF7-25D0-4439-8E8B-74A07EDC1809}.Release|Any CPU.Build.0 = Release|Any CPU 66 | {5E80700F-79D7-4775-9F3D-DD3A0CE2E039}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 67 | {5E80700F-79D7-4775-9F3D-DD3A0CE2E039}.Debug|Any CPU.Build.0 = Debug|Any CPU 68 | {5E80700F-79D7-4775-9F3D-DD3A0CE2E039}.Release|Any CPU.ActiveCfg = Release|Any CPU 69 | {5E80700F-79D7-4775-9F3D-DD3A0CE2E039}.Release|Any CPU.Build.0 = Release|Any CPU 70 | {CD770637-866F-4AF5-A441-BF97FC1EE8CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 71 | {CD770637-866F-4AF5-A441-BF97FC1EE8CC}.Debug|Any CPU.Build.0 = Debug|Any CPU 72 | {CD770637-866F-4AF5-A441-BF97FC1EE8CC}.Release|Any CPU.ActiveCfg = Release|Any CPU 73 | {CD770637-866F-4AF5-A441-BF97FC1EE8CC}.Release|Any CPU.Build.0 = Release|Any CPU 74 | {439DD393-CBB0-47A3-B79E-41E0F3598DE0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 75 | {439DD393-CBB0-47A3-B79E-41E0F3598DE0}.Debug|Any CPU.Build.0 = Debug|Any CPU 76 | {439DD393-CBB0-47A3-B79E-41E0F3598DE0}.Release|Any CPU.ActiveCfg = Release|Any CPU 77 | {439DD393-CBB0-47A3-B79E-41E0F3598DE0}.Release|Any CPU.Build.0 = Release|Any CPU 78 | {8AE5B721-DB8E-461E-8D33-3C78E62E393D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 79 | {8AE5B721-DB8E-461E-8D33-3C78E62E393D}.Debug|Any CPU.Build.0 = Debug|Any CPU 80 | {8AE5B721-DB8E-461E-8D33-3C78E62E393D}.Release|Any CPU.ActiveCfg = Release|Any CPU 81 | {8AE5B721-DB8E-461E-8D33-3C78E62E393D}.Release|Any CPU.Build.0 = Release|Any CPU 82 | {C0836A01-E6C2-48B1-882B-D5255811CDFF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 83 | {C0836A01-E6C2-48B1-882B-D5255811CDFF}.Debug|Any CPU.Build.0 = Debug|Any CPU 84 | {C0836A01-E6C2-48B1-882B-D5255811CDFF}.Release|Any CPU.ActiveCfg = Release|Any CPU 85 | {C0836A01-E6C2-48B1-882B-D5255811CDFF}.Release|Any CPU.Build.0 = Release|Any CPU 86 | {CE0C8A45-81B5-4B1B-8E2E-7E979A65D67B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 87 | {CE0C8A45-81B5-4B1B-8E2E-7E979A65D67B}.Debug|Any CPU.Build.0 = Debug|Any CPU 88 | {CE0C8A45-81B5-4B1B-8E2E-7E979A65D67B}.Release|Any CPU.ActiveCfg = Release|Any CPU 89 | {CE0C8A45-81B5-4B1B-8E2E-7E979A65D67B}.Release|Any CPU.Build.0 = Release|Any CPU 90 | {0B6104D1-4F64-4132-B8B3-651B1A8DBF06}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 91 | {0B6104D1-4F64-4132-B8B3-651B1A8DBF06}.Debug|Any CPU.Build.0 = Debug|Any CPU 92 | {0B6104D1-4F64-4132-B8B3-651B1A8DBF06}.Release|Any CPU.ActiveCfg = Release|Any CPU 93 | {0B6104D1-4F64-4132-B8B3-651B1A8DBF06}.Release|Any CPU.Build.0 = Release|Any CPU 94 | {829F9F8A-C5BD-4241-BC4C-0AAD7EE75C6F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 95 | {829F9F8A-C5BD-4241-BC4C-0AAD7EE75C6F}.Debug|Any CPU.Build.0 = Debug|Any CPU 96 | {829F9F8A-C5BD-4241-BC4C-0AAD7EE75C6F}.Release|Any CPU.ActiveCfg = Release|Any CPU 97 | {829F9F8A-C5BD-4241-BC4C-0AAD7EE75C6F}.Release|Any CPU.Build.0 = Release|Any CPU 98 | {B47A24E0-6E4D-40C8-8F57-BC63E4706546}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 99 | {B47A24E0-6E4D-40C8-8F57-BC63E4706546}.Debug|Any CPU.Build.0 = Debug|Any CPU 100 | {B47A24E0-6E4D-40C8-8F57-BC63E4706546}.Release|Any CPU.ActiveCfg = Release|Any CPU 101 | {B47A24E0-6E4D-40C8-8F57-BC63E4706546}.Release|Any CPU.Build.0 = Release|Any CPU 102 | {23F445B8-CD5E-4221-84C2-06C7F61E11AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 103 | {23F445B8-CD5E-4221-84C2-06C7F61E11AA}.Debug|Any CPU.Build.0 = Debug|Any CPU 104 | {23F445B8-CD5E-4221-84C2-06C7F61E11AA}.Release|Any CPU.ActiveCfg = Release|Any CPU 105 | {23F445B8-CD5E-4221-84C2-06C7F61E11AA}.Release|Any CPU.Build.0 = Release|Any CPU 106 | {13E44E37-0C2E-4D68-8605-4D4C33147776}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 107 | {13E44E37-0C2E-4D68-8605-4D4C33147776}.Debug|Any CPU.Build.0 = Debug|Any CPU 108 | {13E44E37-0C2E-4D68-8605-4D4C33147776}.Release|Any CPU.ActiveCfg = Release|Any CPU 109 | {13E44E37-0C2E-4D68-8605-4D4C33147776}.Release|Any CPU.Build.0 = Release|Any CPU 110 | {AE2D2AE7-AD30-4968-98BF-36553D749383}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 111 | {AE2D2AE7-AD30-4968-98BF-36553D749383}.Debug|Any CPU.Build.0 = Debug|Any CPU 112 | {AE2D2AE7-AD30-4968-98BF-36553D749383}.Release|Any CPU.ActiveCfg = Release|Any CPU 113 | {AE2D2AE7-AD30-4968-98BF-36553D749383}.Release|Any CPU.Build.0 = Release|Any CPU 114 | {BDEA1C53-5092-44B6-9124-CED4EF97453A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 115 | {BDEA1C53-5092-44B6-9124-CED4EF97453A}.Debug|Any CPU.Build.0 = Debug|Any CPU 116 | {BDEA1C53-5092-44B6-9124-CED4EF97453A}.Release|Any CPU.ActiveCfg = Release|Any CPU 117 | {BDEA1C53-5092-44B6-9124-CED4EF97453A}.Release|Any CPU.Build.0 = Release|Any CPU 118 | {850D7C4C-1D54-40AF-8E3F-D43E5311D5DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 119 | {850D7C4C-1D54-40AF-8E3F-D43E5311D5DB}.Debug|Any CPU.Build.0 = Debug|Any CPU 120 | {850D7C4C-1D54-40AF-8E3F-D43E5311D5DB}.Release|Any CPU.ActiveCfg = Release|Any CPU 121 | {850D7C4C-1D54-40AF-8E3F-D43E5311D5DB}.Release|Any CPU.Build.0 = Release|Any CPU 122 | {C45BE097-7746-4FB2-A1B1-A2909C318EA0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 123 | {C45BE097-7746-4FB2-A1B1-A2909C318EA0}.Debug|Any CPU.Build.0 = Debug|Any CPU 124 | {C45BE097-7746-4FB2-A1B1-A2909C318EA0}.Release|Any CPU.ActiveCfg = Release|Any CPU 125 | {C45BE097-7746-4FB2-A1B1-A2909C318EA0}.Release|Any CPU.Build.0 = Release|Any CPU 126 | {F121AAFB-DEAA-473D-AFA6-AC0C9CBCE692}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 127 | {F121AAFB-DEAA-473D-AFA6-AC0C9CBCE692}.Debug|Any CPU.Build.0 = Debug|Any CPU 128 | {F121AAFB-DEAA-473D-AFA6-AC0C9CBCE692}.Release|Any CPU.ActiveCfg = Release|Any CPU 129 | {F121AAFB-DEAA-473D-AFA6-AC0C9CBCE692}.Release|Any CPU.Build.0 = Release|Any CPU 130 | {A4C7862F-0455-49BE-B2E1-D010C721364B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 131 | {A4C7862F-0455-49BE-B2E1-D010C721364B}.Debug|Any CPU.Build.0 = Debug|Any CPU 132 | {A4C7862F-0455-49BE-B2E1-D010C721364B}.Release|Any CPU.ActiveCfg = Release|Any CPU 133 | {A4C7862F-0455-49BE-B2E1-D010C721364B}.Release|Any CPU.Build.0 = Release|Any CPU 134 | {0B87CAFC-7EE2-48C2-9FE8-CD9F6257D9AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 135 | {0B87CAFC-7EE2-48C2-9FE8-CD9F6257D9AB}.Debug|Any CPU.Build.0 = Debug|Any CPU 136 | {0B87CAFC-7EE2-48C2-9FE8-CD9F6257D9AB}.Release|Any CPU.ActiveCfg = Release|Any CPU 137 | {0B87CAFC-7EE2-48C2-9FE8-CD9F6257D9AB}.Release|Any CPU.Build.0 = Release|Any CPU 138 | EndGlobalSection 139 | GlobalSection(SolutionProperties) = preSolution 140 | HideSolutionNode = FALSE 141 | EndGlobalSection 142 | GlobalSection(ExtensibilityGlobals) = postSolution 143 | SolutionGuid = {6BEC2847-ECAB-47F3-853E-164383BF8CCA} 144 | EndGlobalSection 145 | EndGlobal 146 | -------------------------------------------------------------------------------- /TimSort/Program.cs: -------------------------------------------------------------------------------- 1 | // C# program to perform TimSort. 2 | using SortExtravaganza.Common; 3 | using System; 4 | 5 | //TimSort is a stable sorting algorithm based on Merge Sort and Insertion Sort. 6 | //This algorithm takes advantage of the fact that InsertionSort performs well on small collections. 7 | //ALGORITHM: 8 | //1. Divide the unsorted collection into smaller collections, where the size of those collections is a power of 2. 9 | //These blocks are known as "run" 10 | //2. For each run, sort the run using insertion sort. 11 | //3. Once all runs are sorted, merge the runs together using merge sort. 12 | class TimSort 13 | { 14 | public const int RUN = 32; 15 | 16 | public static void InsertionSort(int[] array, int left, int right) 17 | { 18 | for (int i = left + 1; i <= right; i++) 19 | { 20 | int temp = array[i]; 21 | int j = i - 1; 22 | while (array[j] > temp && j >= left) 23 | { 24 | array[j + 1] = array[j]; 25 | j--; 26 | } 27 | array[j + 1] = temp; 28 | } 29 | } 30 | 31 | // merge function merges the sorted runs 32 | public static void Merge(int[] array, int l, int m, int r) 33 | { 34 | // original array is broken in two parts 35 | // left and right array 36 | int len1 = m - l + 1, len2 = r - m; 37 | int[] left = new int[len1]; 38 | int[] right = new int[len2]; 39 | for (int x = 0; x < len1; x++) 40 | left[x] = array[l + x]; 41 | for (int x = 0; x < len2; x++) 42 | right[x] = array[m + 1 + x]; 43 | 44 | int i = 0; 45 | int j = 0; 46 | int k = l; 47 | 48 | // after comparing, we merge those two array 49 | // in larger sub array 50 | while (i < len1 && j < len2) 51 | { 52 | if (left[i] <= right[j]) 53 | { 54 | array[k] = left[i]; 55 | i++; 56 | } 57 | else 58 | { 59 | array[k] = right[j]; 60 | j++; 61 | } 62 | k++; 63 | } 64 | 65 | // copy remaining elements of left, if any 66 | while (i < len1) 67 | { 68 | array[k] = left[i]; 69 | k++; 70 | i++; 71 | } 72 | 73 | // copy remaining element of right, if any 74 | while (j < len2) 75 | { 76 | array[k] = right[j]; 77 | k++; 78 | j++; 79 | } 80 | } 81 | 82 | // iterative Timsort function to sort the 83 | // array[0...n-1] (similar to merge sort) 84 | public static void Sort(int[] arr, int n) 85 | { 86 | // Sort individual subarrays of size RUN 87 | for (int i = 0; i < n; i += RUN) 88 | { 89 | InsertionSort(arr, i, Math.Min((i + 31), (n - 1))); 90 | } 91 | 92 | // start merging from size RUN (or 32). It will merge 93 | // to form size 64, then 128, 256 and so on .... 94 | for (int size = RUN; size < n; size = 2 * size) 95 | { 96 | // pick starting point of left sub array. We 97 | // are going to merge arr[left..left+size-1] 98 | // and arr[left+size, left+2*size-1] 99 | // After every merge, we increase left by 2*size 100 | for (int left = 0; left < n; left += 2 * size) 101 | { 102 | // find ending point of left sub array 103 | // mid+1 is starting point of right sub array 104 | int mid = left + size - 1; 105 | int right = Math.Min((left + 2 * size - 1), (n - 1)); 106 | 107 | // merge sub array arr[left.....mid] & 108 | // arr[mid+1....right] 109 | Merge(arr, left, mid, right); 110 | } 111 | } 112 | } 113 | 114 | public static void Main() 115 | { 116 | int[] array = { 68, 14, 27, 91, 32, 18, 45, 71, 5, 9, 32 }; 117 | int length = array.Length; 118 | 119 | Console.WriteLine("Timsort"); 120 | 121 | CommonFunctions.PrintInitial(array); 122 | 123 | Sort(array, length); 124 | 125 | CommonFunctions.PrintFinal(array); 126 | 127 | Console.ReadKey(); 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /TimSort/TimSort.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /TreeSort/Program.cs: -------------------------------------------------------------------------------- 1 | using SortExtravaganza.Common; 2 | using System; 3 | using System.Linq; 4 | 5 | namespace TreeSort 6 | { 7 | public class Node 8 | { 9 | public int Key { get; set; } 10 | public Node LeftNode, RightNode; 11 | 12 | public Node(int item) 13 | { 14 | Key = item; 15 | LeftNode = RightNode = null; 16 | } 17 | } 18 | 19 | class TreeSort 20 | { 21 | // Root of Binary Search Tree 22 | Node Root = null; 23 | 24 | 25 | Node Insert(Node root, int key) 26 | { 27 | 28 | /* If the tree is empty, 29 | return a new node */ 30 | if (root == null) 31 | { 32 | root = new Node(key); 33 | return root; 34 | } 35 | 36 | /* Otherwise, recur 37 | down the tree */ 38 | if (key < root.Key) 39 | { 40 | root.LeftNode = Insert(root.LeftNode, key); 41 | } 42 | else if (key > root.Key) 43 | { 44 | root.RightNode = Insert(root.RightNode, key); 45 | } 46 | 47 | return root; 48 | } 49 | 50 | void inorderRec(Node root) 51 | { 52 | if (root != null) 53 | { 54 | inorderRec(root.LeftNode); 55 | Console.Write(root.Key + " "); 56 | inorderRec(root.RightNode); 57 | } 58 | } 59 | 60 | void InsertToTree(int[] arr) 61 | { 62 | for (int i = 0; i < arr.Length; i++) 63 | { 64 | Root = Insert(Root, arr[i]); 65 | } 66 | 67 | } 68 | 69 | public static void Main(String[] args) 70 | { 71 | TreeSort tree = new TreeSort(); 72 | int[] arr = { 41, 93, 18, 2, 74, 56, 60, 19, 24, 63, 29 }; 73 | CommonFunctions.PrintInitial(arr); 74 | 75 | tree.InsertToTree(arr); 76 | tree.inorderRec(tree.Root); 77 | 78 | Console.WriteLine("Root value: " + tree.Root.Key); 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /TreeSort/TreeSort.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | --------------------------------------------------------------------------------