├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── Sorting.cs └── coffee.png /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /[Ll]ibrary/ 2 | /[Tt]emp/ 3 | /[Oo]bj/ 4 | /[Bb]uild/ 5 | /[Bb]uilds/ 6 | /Assets/AssetStoreTools* 7 | 8 | # Autogenerated VS/MD/Consulo solution and project files 9 | ExportedObj/ 10 | .consulo/ 11 | *.csproj 12 | *.unityproj 13 | *.sln 14 | *.suo 15 | *.tmp 16 | *.user 17 | *.userprefs 18 | *.pidb 19 | *.booproj 20 | *.svd 21 | 22 | 23 | # Unity3D generated meta files 24 | *.pidb.meta 25 | 26 | # Unity3D Generated File On Crash Reports 27 | sysinfo.txt 28 | 29 | # Builds 30 | *.apk 31 | *.unitypackage 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 João Pedro Costa 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sorting Algorithms 2 | 3 | Implementations of common sorting algorithms in C#. 4 | 5 | Makes use of generics, so you can sort anything that implements IComparable. 6 | 7 | # Includes 8 | 9 | - Bubble Sort 10 | - Comb Sort 11 | - Insertion Sort 12 | 13 | # Todo 14 | 15 | - Merge Sort 16 | - Quicksort 17 | - Slowsort ('cause why not) 18 | - Heapsort 19 | 20 | # Consider buying me a coffee if you like my work (click the image) 21 | [![Foo](coffee.png)](https://www.buymeacoffee.com/ZcRuWpUBf) 22 | -------------------------------------------------------------------------------- /Sorting.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System; 3 | using UnityEngine; 4 | 5 | public static class Sorting 6 | { 7 | public static void Swap(ref T src, ref T dst) where T : IComparable 8 | { 9 | T temp = dst; 10 | dst = src; 11 | src = temp; 12 | } 13 | 14 | public static T[] CombSort(float shrinkFactor, params T[] source) where T : IComparable 15 | { 16 | bool sorted = false; 17 | 18 | int gap = source.Length; 19 | 20 | for(int iteration = 0; iteration <= source.Length * source.Length; iteration++) //Worst case is n^2 21 | { 22 | bool swapped = false; 23 | for(int i = 0; i + gap < source.Length; i++) 24 | { 25 | if(source[i].CompareTo(source[i+gap]) == 1) 26 | { 27 | SortingHelper.Swap(ref source[i], ref source[i+gap]); 28 | swapped = true; 29 | } 30 | } 31 | 32 | gap = Mathf.FloorToInt(gap / shrinkFactor); 33 | gap = Mathf.Clamp(gap, 1, source.Length); 34 | 35 | if(!swapped && gap == 1) //Gap must be 1(regular bubble sort) to make sure everything was sorted 36 | { 37 | sorted = true; 38 | break; 39 | } 40 | 41 | } 42 | 43 | if(!sorted) //MaxIterations were hit 44 | throw new ArgumentException("Too few iterations to sort array."); 45 | 46 | return source; 47 | } 48 | 49 | public static T[] BubbleSort(params T[] source) where T : IComparable 50 | { 51 | bool sorted = false; 52 | 53 | for(int iteration = 0; iteration <= source.Length * source.Length; iteration++) //Worst case is n^2 54 | { 55 | bool swapped = false; 56 | for(int i = 0; i < source.Length - 1; i++) 57 | { 58 | if(source[i].CompareTo(source[i+1]) == 1) 59 | { 60 | SortingHelper.Swap(ref source[i], ref source[i+1]); 61 | swapped = true; 62 | } 63 | } 64 | 65 | if(!swapped) 66 | { 67 | sorted = true; 68 | Debug.Log(iteration); 69 | break; 70 | } 71 | 72 | } 73 | 74 | if(!sorted) 75 | throw new ArgumentException("Too few iterations to sort array."); 76 | 77 | return source; 78 | } 79 | 80 | public static T[] InsertionSort(params T[] source) where T : IComparable 81 | { 82 | for(int iteration = 1; iteration < source.Length; iteration++) 83 | { 84 | int currentIndex = iteration; 85 | while(currentIndex > 0 && source[currentIndex-1].CompareTo(source[currentIndex]) == 1) //X.CompareTo(Y) returns: -1 smaller, 0 equal, 1 bigger. 86 | { 87 | Swap(ref source[currentIndex], ref source[currentIndex-1]); 88 | currentIndex--; 89 | } 90 | } 91 | 92 | return source; 93 | } 94 | } -------------------------------------------------------------------------------- /coffee.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JPBotelho/Sorting-Algorithms/95a98215e96131aa2326392ccc8c03c6b1edaf72/coffee.png --------------------------------------------------------------------------------