├── .gitignore
├── Algorithms.sln
├── Algorithms.suo
├── Algorithms.v11.suo
├── Algoritms.suo
├── Other
├── Factorial.cs
├── Fibonacci.cs
├── Other.csproj
└── Properties
│ └── AssemblyInfo.cs
├── README.md
├── SearchAlgoritms
├── App.config
├── BinarySearch.cs
├── BoyerMooreSearchClass.cs
├── DirectSearch.cs
├── KPMSearch.cs
├── LinearSearch.cs
├── Program.cs
├── Properties
│ └── AssemblyInfo.cs
├── SearchAlgorithms.csproj
└── SearchHelper.cs
└── SortingAlgoritms
├── App.config
├── BogoSort.cs
├── BubbleSort.cs
├── BucketSort.cs
├── CocktailSort.cs
├── CombSort.cs
├── GnomeSort.cs
├── HeapSort.cs
├── InsertionSort.cs
├── MergeSort.cs
├── OddEvenSort.cs
├── Program.cs
├── Properties
└── AssemblyInfo.cs
├── QuickSort.cs
├── SelectionSort.cs
├── ShellSort.cs
├── SortingAlgorithms.csproj
├── SortingHelper.cs
├── SortingTimer.cs
├── StupidSort.cs
├── Timer.cs
└── СountingSort.cs
/.gitignore:
--------------------------------------------------------------------------------
1 | Debug
2 |
--------------------------------------------------------------------------------
/Algorithms.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 2012
4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SortingAlgoritms", "SortingAlgoritms\SortingAlgorithms.csproj", "{0C929368-DCD5-40D7-8D71-8D21184F1790}"
5 | EndProject
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SearchAlgoritms", "SearchAlgoritms\SearchAlgorithms.csproj", "{1FADD51F-47F7-4887-A60E-9AD65593CBA5}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {0C929368-DCD5-40D7-8D71-8D21184F1790}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {0C929368-DCD5-40D7-8D71-8D21184F1790}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {0C929368-DCD5-40D7-8D71-8D21184F1790}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {0C929368-DCD5-40D7-8D71-8D21184F1790}.Release|Any CPU.Build.0 = Release|Any CPU
18 | {1FADD51F-47F7-4887-A60E-9AD65593CBA5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19 | {1FADD51F-47F7-4887-A60E-9AD65593CBA5}.Debug|Any CPU.Build.0 = Debug|Any CPU
20 | {1FADD51F-47F7-4887-A60E-9AD65593CBA5}.Release|Any CPU.ActiveCfg = Release|Any CPU
21 | {1FADD51F-47F7-4887-A60E-9AD65593CBA5}.Release|Any CPU.Build.0 = Release|Any CPU
22 | EndGlobalSection
23 | GlobalSection(SolutionProperties) = preSolution
24 | HideSolutionNode = FALSE
25 | EndGlobalSection
26 | EndGlobal
27 |
--------------------------------------------------------------------------------
/Algorithms.suo:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shkolovy/classic-algorithms-c-sharp/07943651a1e554f500b9bd47a9889a00eaf13d25/Algorithms.suo
--------------------------------------------------------------------------------
/Algorithms.v11.suo:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shkolovy/classic-algorithms-c-sharp/07943651a1e554f500b9bd47a9889a00eaf13d25/Algorithms.v11.suo
--------------------------------------------------------------------------------
/Algoritms.suo:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shkolovy/classic-algorithms-c-sharp/07943651a1e554f500b9bd47a9889a00eaf13d25/Algoritms.suo
--------------------------------------------------------------------------------
/Other/Factorial.cs:
--------------------------------------------------------------------------------
1 | namespace Other
2 | {
3 | //7! = 1·2·3·4·5·6·7 = 5040
4 | public static class Factorial
5 | {
6 | public static int CalculateWithRecursion(int number)
7 | {
8 | return Calculate(number, number);
9 | }
10 |
11 | private static int Calculate(int number, int result)
12 | {
13 | if (number > 1)
14 | {
15 | return Calculate(--number, result*number);
16 | }
17 |
18 | return result;
19 | }
20 |
21 | public static int Calculate(int number)
22 | {
23 | int result = number;
24 |
25 | while (number > 1)
26 | {
27 | result = result*--number;
28 | }
29 |
30 | return result;
31 | }
32 | }
33 | }
--------------------------------------------------------------------------------
/Other/Fibonacci.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Other
4 | {
5 | //0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584 ....
6 | public static class Fibonacci
7 | {
8 | public static int[] Calculate(int numbersCount)
9 | {
10 | if (numbersCount < 2)
11 | {
12 | throw new ArgumentException("numbersCount cannot be less than 2");
13 | }
14 |
15 | var result = new int[numbersCount];
16 |
17 | result[0] = 0;
18 | result[1] = 1;
19 |
20 | for (int i = 1; i < numbersCount - 1; i++)
21 | {
22 | result[i + 1] = result[i] + result[i - 1];
23 | }
24 |
25 | return result;
26 | }
27 |
28 | public static int[] CalculateWithRecursion(int numbersCount)
29 | {
30 | if (numbersCount < 2)
31 | {
32 | throw new ArgumentException("numbersCount cannot be less than 2");
33 | }
34 |
35 | var result = new int[numbersCount];
36 |
37 | result[0] = 0;
38 | result[1] = 1;
39 |
40 | CalculateWithRecursion(result, numbersCount, 1);
41 |
42 | return result;
43 | }
44 |
45 | private static void CalculateWithRecursion(int[] arr, int numbersCount, int currentNemberIndex)
46 | {
47 | if (currentNemberIndex < numbersCount - 1)
48 | {
49 | arr[currentNemberIndex + 1] = arr[currentNemberIndex] + arr[currentNemberIndex - 1];
50 | CalculateWithRecursion(arr, numbersCount, currentNemberIndex + 1);
51 | }
52 | }
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/Other/Other.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | 19c8e0d3-979a-4b21-a6d9-a8514409d8ad
8 | Library
9 | Properties
10 | Other
11 | Other
12 | v4.5
13 | 512
14 |
15 |
16 | true
17 | full
18 | false
19 | bin\Debug\
20 | DEBUG;TRACE
21 | prompt
22 | 4
23 |
24 |
25 | pdbonly
26 | true
27 | bin\Release\
28 | TRACE
29 | prompt
30 | 4
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
58 |
59 |
60 |
--------------------------------------------------------------------------------
/Other/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Other")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Other")]
13 | [assembly: AssemblyCopyright("Copyright © 2014")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("19c8e0d3-979a-4b21-a6d9-a8514409d8ad")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Classic algorithms on C#
2 |
3 | ### Sorting
4 | - Bogo Sort
5 | - Bubble Sort
6 | - Bucket Sort
7 | - Cocktail Sort
8 | - Comb Sort
9 | - Gnome Sort
10 | - Heap Sort
11 | - Insertion Sort
12 | - Merge Sort
13 | - OddEven Sort
14 | - Quick Sort
15 | - Selection Sort
16 | - Shell Sort
17 | - Counting Sort
18 | - Stupid Sort
19 |
20 | ### Search
21 | - Binary Search
22 | - BoyerMoore Search
23 | - Direct Search
24 | - KPM Search
25 | - Linear Search
26 |
27 | ### Other
28 | - Factorial
29 | - Fibonacci
30 |
--------------------------------------------------------------------------------
/SearchAlgoritms/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/SearchAlgoritms/BinarySearch.cs:
--------------------------------------------------------------------------------
1 | namespace SearchAlgorithms
2 | {
3 | //делением пополам
4 | public static class BinarySearchClass
5 | {
6 | public static int? BinarySearch(this int[] array, int key)
7 | {
8 | return array.Search(key, 0, array.Length - 1);
9 | }
10 |
11 | private static int? Search(this int[] array, int key, int leftIndex, int rightIndex)
12 | {
13 | if (leftIndex > rightIndex)
14 | {
15 | return null;
16 | }
17 |
18 | int midIndex = (leftIndex + rightIndex)/2;
19 |
20 | if (array[midIndex] == key)
21 | {
22 | return array[midIndex];
23 | }
24 |
25 | if (array[midIndex] > key)
26 | {
27 | return array.Search(key, leftIndex, midIndex - 1);
28 | }
29 |
30 | return array.Search(key, midIndex + 1, rightIndex);
31 | }
32 | }
33 | }
--------------------------------------------------------------------------------
/SearchAlgoritms/BoyerMooreSearchClass.cs:
--------------------------------------------------------------------------------
1 | using System.Linq;
2 |
3 | namespace SearchAlgorithms
4 | {
5 | public static class BoyerMooreSearchClass
6 | {
7 | /*
8 | hoola-hoola girls like hooligans
9 | hooligan
10 | *****hooligan
11 | *******hooligan
12 | ***************hooligan
13 | ***********************hooligan
14 | */
15 | public static int? BoyerMooreSearch(this string originalStr, string str)
16 | {
17 | char[] strArray = originalStr.ToArray();
18 | char[] searchStrArray = str.ToArray();
19 |
20 | int indexStart = 0;
21 |
22 | while (indexStart <= strArray.Length - searchStrArray.Length)
23 | {
24 | int j = indexStart + searchStrArray.Length - 1;
25 | int i = searchStrArray.Length - 1;
26 |
27 | while (i >= 0)
28 | {
29 | if (searchStrArray[i] == strArray[j])
30 | {
31 | if (i == 0)
32 | {
33 | return indexStart;
34 | }
35 |
36 | i--;
37 | j--;
38 | }
39 | else
40 | {
41 | break;
42 | }
43 | }
44 |
45 | int shiftIndex = GetShiftIndex(strArray[indexStart + searchStrArray.Length - 1], searchStrArray);
46 |
47 | indexStart = indexStart + shiftIndex;
48 | }
49 |
50 | return -1;
51 | }
52 |
53 | private static int GetShiftIndex(char charToMatch, char[] searchStrArray)
54 | {
55 | int index = 0;
56 |
57 | for (int i = searchStrArray.Length - 1; i >= 0; i--)
58 | {
59 | if (searchStrArray[i] == charToMatch)
60 | {
61 | if (index == 0)
62 | {
63 | return i;
64 | }
65 |
66 | return index;
67 | }
68 |
69 | index++;
70 | }
71 |
72 | return index;
73 | }
74 | }
75 | }
--------------------------------------------------------------------------------
/SearchAlgoritms/DirectSearch.cs:
--------------------------------------------------------------------------------
1 | using System.Linq;
2 |
3 | namespace SearchAlgorithms
4 | {
5 | public static class DirectSearchClass
6 | {
7 | public static int? DirectSearch(this string originalStr, string str)
8 | {
9 | char[] strArray = originalStr.ToArray();
10 | char[] searchStrArray = str.ToArray();
11 |
12 | for (int i = 0; i < strArray.Length; i++)
13 | {
14 | var j = 0;
15 |
16 | while (j < searchStrArray.Length)
17 | {
18 | if (searchStrArray[j] == strArray[i + j])
19 | {
20 | j++;
21 | }
22 | else
23 | {
24 | break;
25 | }
26 | }
27 |
28 | if (j == searchStrArray.Length)
29 | {
30 | return i;
31 | }
32 | }
33 |
34 | return null;
35 | }
36 | }
37 | }
--------------------------------------------------------------------------------
/SearchAlgoritms/KPMSearch.cs:
--------------------------------------------------------------------------------
1 | using System.Linq;
2 |
3 | namespace SearchAlgorithms
4 | {
5 | public static class KPMSearchClass
6 | {
7 | public static int? KPMSearch(this string originalStr, string str)
8 | {
9 | char[] strArray = originalStr.ToArray();
10 | char[] searchStrArray = str.ToArray();
11 |
12 | var i = 0;
13 | var j = 0;
14 |
15 | while (i < strArray.Length)
16 | {
17 | if (strArray[i] == searchStrArray[j])
18 | {
19 | if (j == searchStrArray.Length - 1)
20 | {
21 | return i - j;
22 | }
23 |
24 | j++;
25 | }
26 | else
27 | {
28 | j = 0;
29 | }
30 |
31 | i++;
32 | }
33 |
34 | return null;
35 | }
36 | }
37 | }
--------------------------------------------------------------------------------
/SearchAlgoritms/LinearSearch.cs:
--------------------------------------------------------------------------------
1 | namespace SearchAlgorithms
2 | {
3 | public static class LinearSearchClass
4 | {
5 | public static int? LinearSearch(this int[] array, int key)
6 | {
7 | foreach (var item in array)
8 | {
9 | if (item == key)
10 | return item;
11 | }
12 |
13 | return null;
14 | }
15 | }
16 | }
--------------------------------------------------------------------------------
/SearchAlgoritms/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace SearchAlgorithms
4 | {
5 | class Program
6 | {
7 | static void Main(string[] args)
8 | {
9 | //var array = SearchHelper.GetRandomArrayWithValue(30, arrayLength: 20);
10 |
11 | //var a = array.LinearSearch(30);
12 |
13 | //var array1 = SearchHelper.GetSequenceRandomArrayWithValue(15, arrayLength: 20); ;
14 |
15 | //var b = array1.BinarySearch(15);
16 |
17 | const string str = "hoola-hoola girls like hooligans";
18 |
19 | var a = str.DirectSearch("hooligan");
20 | var b = str.KPMSearch("hooligan");
21 | var c = str.BoyerMooreSearch("hooligan");
22 |
23 | Console.ReadKey();
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/SearchAlgoritms/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("SearchAlgorithms")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("SearchAlgorithms")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("ab3c6019-b01e-472d-b231-632d2716e866")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/SearchAlgoritms/SearchAlgorithms.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {1FADD51F-47F7-4887-A60E-9AD65593CBA5}
8 | Exe
9 | Properties
10 | SearchAlgorithms
11 | SearchAlgorithms
12 | v4.5
13 | 512
14 |
15 |
16 | AnyCPU
17 | true
18 | full
19 | false
20 | bin\Debug\
21 | DEBUG;TRACE
22 | prompt
23 | 4
24 |
25 |
26 | AnyCPU
27 | pdbonly
28 | true
29 | bin\Release\
30 | TRACE
31 | prompt
32 | 4
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 | {0c929368-dcd5-40d7-8d71-8d21184f1790}
59 | SortingAlgorithms
60 |
61 |
62 |
63 |
70 |
--------------------------------------------------------------------------------
/SearchAlgoritms/SearchHelper.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using SortingAlgorithms;
3 |
4 | namespace SearchAlgorithms
5 | {
6 | public static class SearchHelper
7 | {
8 | public static int[] GetRandomArrayWithValue(int value, int arrayLength = 100, int minValue = 0, int maxValue = 100, bool uniq = true)
9 | {
10 | var array = GetRandomArray(arrayLength, minValue, maxValue, uniq);
11 |
12 | if (!IsHasValue(array, value))
13 | {
14 | var r = new Random();
15 | array[r.Next(0, arrayLength)] = value;
16 | }
17 |
18 | return array;
19 | }
20 |
21 | public static int[] GetRandomArray(int arrayLength = 100, int minValue = 0, int maxValue = 100, bool uniq = true)
22 | {
23 | var array = new int[arrayLength];
24 | var r = new Random();
25 |
26 | for (int i = 0; i < arrayLength; i++)
27 | {
28 | var value = r.Next(minValue, maxValue);
29 |
30 | while (uniq && IsHasValue(array, value))
31 | {
32 | value = r.Next(minValue, maxValue);
33 | }
34 |
35 | array[i] = value;
36 | }
37 |
38 | return array;
39 | }
40 |
41 | public static int[] GetSequenceRandomArrayWithValue(int value, int arrayLength = 100, int minValue = 0, int maxValue = 100, bool uniq = true)
42 | {
43 | var array = GetRandomArrayWithValue(value, arrayLength, minValue, maxValue, uniq);
44 | array.QuickSortAsc();
45 |
46 | return array;
47 | }
48 |
49 |
50 | private static bool IsHasValue(int[] array, int value)
51 | {
52 | foreach (var item in array)
53 | {
54 | if (item == value)
55 | {
56 | return true;
57 | }
58 | }
59 |
60 | return false;
61 | }
62 | }
63 | }
--------------------------------------------------------------------------------
/SortingAlgoritms/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/SortingAlgoritms/BogoSort.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace SortingAlgorithms
4 | {
5 | public static class BogoSort
6 | {
7 | public static void BogoSortAsc(this int[] array)
8 | {
9 | if (array.Length <= 1)
10 | return;
11 |
12 | while (!array.IsSorted())
13 | {
14 | var random = new Random();
15 |
16 | for (var i = 0; i < array.Length; i++)
17 | {
18 | array.Swap(i, random.Next(0, array.Length));
19 | }
20 | }
21 | }
22 | }
23 | }
--------------------------------------------------------------------------------
/SortingAlgoritms/BubbleSort.cs:
--------------------------------------------------------------------------------
1 | namespace SortingAlgorithms
2 | {
3 | public static class BubbleSort
4 | {
5 | public static void BubbleSortEnhancedAsc(this int[] array)
6 | {
7 | if (array.Length <= 1)
8 | return;
9 |
10 | var i = array.Length - 1;
11 |
12 | var isSorted = false;
13 |
14 | while (i > 0 && !isSorted)
15 | {
16 | var j = 0;
17 |
18 | isSorted = true;
19 |
20 | while (j < i)
21 | {
22 | if (array[j] > array[j + 1])
23 | {
24 | array.Swap(j + 1, j);
25 | isSorted = false;
26 | }
27 |
28 | j++;
29 | }
30 |
31 | i--;
32 | }
33 | }
34 |
35 | public static void BubbleSortAsc(this int[] array)
36 | {
37 | if (array.Length <= 1)
38 | return;
39 |
40 | var itemsLength = array.Length;
41 |
42 | bool isSorted;
43 |
44 | do
45 | {
46 | isSorted = true;
47 |
48 | for (var i = 0; i < itemsLength - 1; i++)
49 | {
50 | if (i < itemsLength - 1)
51 | {
52 | if (array[i + 1] < array[i])
53 | {
54 | isSorted = false;
55 | array.Swap(i + 1, i);
56 | }
57 | }
58 | }
59 | } while (!isSorted);
60 | }
61 | }
62 | }
--------------------------------------------------------------------------------
/SortingAlgoritms/BucketSort.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace SortingAlgorithms
4 | {
5 | public static class BucketSort
6 | {
7 | public static void BucketSortAsc(this int[] array, int bucketsNumber = 10)
8 | {
9 | if (array.Length <= 1)
10 | return;
11 |
12 | int maxValue = array[0];
13 | int minValue = array[0];
14 |
15 | for (var i = 0; i < array.Length - 1; i++)
16 | {
17 | if (array[i + 1] > maxValue)
18 | {
19 | maxValue = array[i + 1];
20 | }
21 | else if (array[i + 1] < minValue)
22 | {
23 | minValue = array[i + 1];
24 | }
25 | }
26 |
27 | int bucketRange = (maxValue - minValue) / bucketsNumber;
28 |
29 | var bucketsArray = new int[bucketsNumber][];
30 |
31 | var currentBucketRangeStartWith = minValue;
32 |
33 | for (var i = 0; i < bucketsArray.Length; i++)
34 | {
35 | var bucket = new List();
36 |
37 | foreach (int item in array)
38 | {
39 | if (item >= currentBucketRangeStartWith && item <= currentBucketRangeStartWith + bucketRange)
40 | {
41 | bucket.Add(item);
42 | }
43 | }
44 |
45 | bucketsArray[i] = bucket.ToArray();
46 |
47 | bucketsArray[i].QuickSortAsc();
48 |
49 | currentBucketRangeStartWith = currentBucketRangeStartWith + bucketRange + 1;
50 | }
51 |
52 | var c = 0;
53 |
54 | foreach (int[] bucket in bucketsArray)
55 | {
56 | foreach (int item in bucket)
57 | {
58 | array[c++] = item;
59 | }
60 | }
61 | }
62 | }
63 | }
--------------------------------------------------------------------------------
/SortingAlgoritms/CocktailSort.cs:
--------------------------------------------------------------------------------
1 | namespace SortingAlgorithms
2 | {
3 | public static class CocktailSort
4 | {
5 | public static void CocktailSortAsc(this int[] array)
6 | {
7 | if (array.Length <= 1)
8 | return;
9 |
10 | var leftIndex = 0;
11 | var rightIndex = array.Length - 1;
12 |
13 | while (leftIndex < rightIndex)
14 | {
15 | var i = leftIndex;
16 | var isSorted = true;
17 |
18 | while (i < rightIndex)
19 | {
20 | if (array[i] > array[i + 1])
21 | {
22 | array.Swap(i + 1, i);
23 | isSorted = false;
24 | }
25 |
26 | i++;
27 | }
28 |
29 | if(isSorted)
30 | break;
31 |
32 | var j = rightIndex - 1;
33 | isSorted = true;
34 |
35 | while (j > leftIndex)
36 | {
37 | if (array[j] < array[j - 1])
38 | {
39 | array.Swap(j - 1, j);
40 | isSorted = false;
41 | }
42 |
43 | j--;
44 | }
45 |
46 | if (isSorted)
47 | break;
48 |
49 | leftIndex++;
50 | rightIndex--;
51 | }
52 | }
53 | }
54 | }
--------------------------------------------------------------------------------
/SortingAlgoritms/CombSort.cs:
--------------------------------------------------------------------------------
1 | namespace SortingAlgorithms
2 | {
3 | public static class CombSort
4 | {
5 | public static void CombSortAsc(this int[] array)
6 | {
7 | if (array.Length <= 1)
8 | return;
9 |
10 | const double shrink = 1.3;
11 |
12 | bool isSorted = false;
13 | int step = array.Length - 1;
14 |
15 | while (step >= 1 && !isSorted)
16 | {
17 | var i = 0;
18 |
19 | //use isSorted only for bubble sort (step = 1)
20 | if (step == 1)
21 | {
22 | isSorted = true;
23 | }
24 |
25 | while (i + step < array.Length)
26 | {
27 | if (array[i] > array[i + step])
28 | {
29 | array.Swap(i, i + step);
30 | isSorted = false;
31 | }
32 |
33 | i++;
34 | }
35 |
36 | if (step > 1)
37 | {
38 | step = (int)(step / shrink);
39 | }
40 | }
41 | }
42 | }
43 | }
--------------------------------------------------------------------------------
/SortingAlgoritms/GnomeSort.cs:
--------------------------------------------------------------------------------
1 | namespace SortingAlgorithms
2 | {
3 | public static class GnomeSort
4 | {
5 | public static void GnomeSortAsc(this int[] array)
6 | {
7 | if (array.Length <= 1)
8 | return;
9 |
10 | if(array.Length <= 1)
11 | return;
12 |
13 | var i = 1;
14 |
15 | while (i < array.Length)
16 | {
17 | if (array[i] < array[i - 1])
18 | {
19 | array.Swap(i, i - 1);
20 |
21 | if (i - 1 == 0)
22 | {
23 | i++;
24 | }
25 | else
26 | {
27 | i--;
28 | }
29 | }
30 | else
31 | {
32 | i++;
33 | }
34 | }
35 | }
36 | }
37 | }
--------------------------------------------------------------------------------
/SortingAlgoritms/HeapSort.cs:
--------------------------------------------------------------------------------
1 | namespace SortingAlgorithms
2 | {
3 | public static class HeapSort
4 | {
5 | public static void HeapSortAsc(this int[] array)
6 | {
7 | if (array.Length <= 1)
8 | return;
9 |
10 | var arrayLength = array.Length;
11 |
12 | for (int i = arrayLength / 2 - 1; i >= 0; i--)
13 | {
14 | ShiftDown(array, i, arrayLength - 1);
15 | }
16 |
17 | var lastItemIndex = arrayLength - 1;
18 |
19 | while (lastItemIndex > 0)
20 | {
21 | array.Swap(0, lastItemIndex);
22 |
23 | ShiftDown(array, 0, --lastItemIndex);
24 | }
25 | }
26 |
27 | private static void ShiftDown(int[] array, int startWithIndex, int endWithIndex)
28 | {
29 | int maxItemIndex = startWithIndex;
30 |
31 | var leftChildIndex = startWithIndex*2 + 1;
32 |
33 | if (leftChildIndex <= endWithIndex && array[startWithIndex] < array[leftChildIndex])
34 | {
35 | maxItemIndex = leftChildIndex;
36 | }
37 |
38 | var rightChildIndex = leftChildIndex + 1;
39 |
40 | if (rightChildIndex <= endWithIndex && array[maxItemIndex] < array[rightChildIndex])
41 | {
42 | maxItemIndex = rightChildIndex;
43 | }
44 |
45 | if (maxItemIndex != startWithIndex)
46 | {
47 | array.Swap(startWithIndex, maxItemIndex);
48 | }
49 |
50 | if (maxItemIndex != startWithIndex)
51 | {
52 | ShiftDown(array, maxItemIndex, endWithIndex);
53 | }
54 | }
55 | }
56 | }
--------------------------------------------------------------------------------
/SortingAlgoritms/InsertionSort.cs:
--------------------------------------------------------------------------------
1 | namespace SortingAlgorithms
2 | {
3 | public static class InsertionSort
4 | {
5 | public static void InsertionSortAsc(this int[] array)
6 | {
7 | if (array.Length <= 1)
8 | return;
9 |
10 | for (int i = 1; i < array.Length; i++)
11 | {
12 | var j = i;
13 |
14 | while (j > 0 && array[j] < array[j - 1])
15 | {
16 | if (array[j] < array[j - 1])
17 | {
18 | array.Swap(j - 1, j);
19 | }
20 |
21 | j--;
22 | }
23 | }
24 | }
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/SortingAlgoritms/MergeSort.cs:
--------------------------------------------------------------------------------
1 | namespace SortingAlgorithms
2 | {
3 | public static class MergeSort
4 | {
5 | public static void MergeSortAsc(this int[] array)
6 | {
7 | if (array.Length <= 1)
8 | return;
9 |
10 | array.MergeSortAscTemp(0, array.Length - 1);
11 | }
12 |
13 | private static void MergeSortAscTemp(this int[] array, int leftIndex, int rightIndex)
14 | {
15 | if (rightIndex > leftIndex)
16 | {
17 | var midIndex = (leftIndex + rightIndex) / 2;
18 |
19 | array.MergeSortAscTemp(leftIndex, midIndex);
20 | array.MergeSortAscTemp(midIndex + 1, rightIndex);
21 |
22 | array.Merge(leftIndex, rightIndex, midIndex);
23 | }
24 | }
25 |
26 | private static void Merge(this int[] array, int leftIndex, int rightIndex, int midIndex)
27 | {
28 | var mergedArrayLength = rightIndex - leftIndex + 1;
29 |
30 | var firstArrayLeftIndex = leftIndex;
31 | var firstArrayRightIndex = midIndex;
32 |
33 | var secondArrayLeftIndex = midIndex + 1;
34 | var secondArrayRightIndex = rightIndex;
35 |
36 | var tempArray = new int[mergedArrayLength];
37 | var tempArrayCurrentIndex = 0;
38 |
39 | while (firstArrayLeftIndex <= firstArrayRightIndex && secondArrayLeftIndex <= secondArrayRightIndex)
40 | {
41 | if (array[firstArrayLeftIndex] > array[secondArrayLeftIndex])
42 | {
43 | tempArray[tempArrayCurrentIndex] = array[secondArrayLeftIndex];
44 | secondArrayLeftIndex++;
45 | }
46 | else
47 | {
48 | tempArray[tempArrayCurrentIndex] = array[firstArrayLeftIndex];
49 | firstArrayLeftIndex++;
50 | }
51 |
52 | tempArrayCurrentIndex++;
53 | }
54 |
55 | while (firstArrayLeftIndex <= firstArrayRightIndex)
56 | {
57 | tempArray[tempArrayCurrentIndex] = array[firstArrayLeftIndex];
58 | firstArrayLeftIndex++;
59 | tempArrayCurrentIndex++;
60 | }
61 |
62 | while (secondArrayLeftIndex <= secondArrayRightIndex)
63 | {
64 | tempArray[tempArrayCurrentIndex] = array[secondArrayLeftIndex];
65 | secondArrayLeftIndex++;
66 | tempArrayCurrentIndex++;
67 | }
68 |
69 | for (var i = 0; i < tempArray.Length; i++)
70 | {
71 | array[leftIndex + i] = tempArray[i];
72 | }
73 | }
74 | }
75 | }
--------------------------------------------------------------------------------
/SortingAlgoritms/OddEvenSort.cs:
--------------------------------------------------------------------------------
1 | namespace SortingAlgorithms
2 | {
3 | public static class OddEvenSort
4 | {
5 | public static void OddEvenSortAsc(this int[] array)
6 | {
7 | if (array.Length <= 1)
8 | return;
9 |
10 | var isSorted = false;
11 |
12 | while (!isSorted)
13 | {
14 | isSorted = true;
15 |
16 | for (int i = 0; i < array.Length - 1; i = i + 2)
17 | {
18 | if (array[i] > array[i + 1])
19 | {
20 | array.Swap(i, i + 1);
21 | isSorted = false;
22 | }
23 | }
24 |
25 | for (int i = 1; i < array.Length - 1; i = i + 2)
26 | {
27 | if (array[i] > array[i + 1])
28 | {
29 | array.Swap(i, i + 1);
30 | isSorted = false;
31 | }
32 | }
33 | }
34 | }
35 | }
36 | }
--------------------------------------------------------------------------------
/SortingAlgoritms/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace SortingAlgorithms
4 | {
5 | class Program
6 | {
7 | static void Main()
8 | {
9 | int[] array = SortingHelper.GetRandomArray(10000);
10 |
11 | //Distribution
12 | SortingTimer.Go(ar => ar.СountingSortAsc(), SortingHelper.GetRandomArray(1000, 0, 255), "Сounting Sort");
13 | SortingTimer.Go(ar => ar.BucketSortAsc(), SortingHelper.GetRandomArray(1000, 0, 255), "Bucket Sort");
14 |
15 | //Exchange
16 | SortingTimer.Go(ar => ar.StupidSortAsc(), SortingHelper.GetRandomArray(100), "Stupid Sort");
17 | SortingTimer.Go(ar => ar.BubbleSortAsc(), SortingHelper.GetRandomArray(100), "Bubble Sort");
18 | SortingTimer.Go(ar => ar.BubbleSortEnhancedAsc(), SortingHelper.GetRandomArray(100), "Bubble Sort Enhanced");
19 | SortingTimer.Go(ar => ar.OddEvenSortAsc(), array.CloneArray(), "OddEven Sort");
20 | SortingTimer.Go(ar => ar.GnomeSortAsc(), array.CloneArray(), "Gnome Sort");
21 | SortingTimer.Go(ar => ar.CocktailSortAsc(), array.CloneArray(), "Cocktail Sort");
22 | SortingTimer.Go(ar => ar.CombSortAsc(), array.CloneArray(), "Comb Sort");
23 | SortingTimer.Go(ar => ar.QuickSortAsc(), array.CloneArray(), "Quick Sort");
24 | SortingTimer.Go(ar => ar.BogoSortAsc(), SortingHelper.GetRandomArray(5, 0, 5), "Bogo Sort");
25 |
26 | //Merge
27 | SortingTimer.Go(ar => ar.MergeSortAsc(), array.CloneArray(), "Merge Sort");
28 |
29 | //Insertion
30 | SortingTimer.Go(ar => ar.InsertionSortAsc(), array.CloneArray(), "Insertion Sort");
31 | SortingTimer.Go(ar => ar.ShellSortAsc(), array.CloneArray(), "Shell Sort");
32 | SortingTimer.Go(ar => ar.ShellCiuraSortAsc(), array.CloneArray(), "Shell Ciura Sort");
33 |
34 | //Selection
35 | SortingTimer.Go(ar => ar.SelectionSortAsc(), array.CloneArray(), "Selection Sort");
36 | SortingTimer.Go(ar => ar.HeapSortAsc(), array.CloneArray(), "Heap Sort");
37 |
38 |
39 | Console.WriteLine("\n-----END-----");
40 | Console.Read();
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/SortingAlgoritms/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("SortingAlgorithms")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("SortingAlgorithms")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("265ed57b-1ded-4a73-b518-865d51975220")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/SortingAlgoritms/QuickSort.cs:
--------------------------------------------------------------------------------
1 | namespace SortingAlgorithms
2 | {
3 | public static class QuickSort
4 | {
5 | public static void QuickSortAsc(this int[] array)
6 | {
7 | if (array.Length <= 1)
8 | return;
9 |
10 | QuickSortTemp(array, 0, array.Length - 1);
11 | }
12 |
13 | private static void QuickSortTemp(int[] collection, int leftIndex, int rightIndex)
14 | {
15 | var i = leftIndex;
16 | var j = rightIndex;
17 | var separateItem = collection[((leftIndex + rightIndex) / 2)];
18 |
19 | do
20 | {
21 | while (collection[i] < separateItem)
22 | {
23 | i++;
24 | }
25 |
26 | while (collection[j] > separateItem)
27 | {
28 | j--;
29 | }
30 |
31 | if (i <= j)
32 | {
33 | if (i < j)
34 | {
35 | collection.Swap(i, j);
36 | }
37 |
38 | i++; j--;
39 | }
40 |
41 | } while (i < j);
42 |
43 | if (leftIndex < j)
44 | QuickSortTemp(collection, leftIndex, j);
45 |
46 | if (i < rightIndex)
47 | QuickSortTemp(collection, i, rightIndex);
48 | }
49 |
50 | }
51 | }
--------------------------------------------------------------------------------
/SortingAlgoritms/SelectionSort.cs:
--------------------------------------------------------------------------------
1 | namespace SortingAlgorithms
2 | {
3 | public static class SelectionSort
4 | {
5 | public static void SelectionSortAsc(this int[] array)
6 | {
7 | if (array.Length <= 1)
8 | return;
9 |
10 | var arrayLength = array.Length;
11 |
12 | for (int i = 0; i < arrayLength - 1; i++)
13 | {
14 | int minIndex = i;
15 |
16 | for (int j = i + 1; j < arrayLength; j++)
17 | {
18 | if (array[j] < array[minIndex])
19 | {
20 | minIndex = j;
21 | }
22 | }
23 |
24 | if (minIndex != i)
25 | {
26 | array.Swap(i, minIndex);
27 | }
28 | }
29 | }
30 | }
31 | }
--------------------------------------------------------------------------------
/SortingAlgoritms/ShellSort.cs:
--------------------------------------------------------------------------------
1 | namespace SortingAlgorithms
2 | {
3 | public static class ShellSort
4 | {
5 | public static void ShellCiuraSortAsc(this int[] array)
6 | {
7 | if (array.Length <= 1)
8 | return;
9 |
10 | var arrayLength = array.Length;
11 |
12 | var stepNumber = 0;
13 |
14 | var step = GetStep(arrayLength, 0);
15 |
16 | while (step > 0)
17 | {
18 | for (int i = 0; i <= arrayLength - 1 - step; i++)
19 | {
20 | var j = i + step;
21 |
22 | while (j - step >= 0 && array[j] < array[j - step])
23 | {
24 | array.Swap(j - step, j);
25 |
26 | j--;
27 | }
28 | }
29 |
30 | step = GetStep(arrayLength, ++stepNumber);
31 | }
32 | }
33 |
34 | private static readonly int[] CiuraStepsIncrements = { 1, 4, 10, 23, 57, 132, 301, 701, 1750 };
35 |
36 | private static int GetStep(int itemsLength, int stepNumber)
37 | {
38 | if (stepNumber >= CiuraStepsIncrements.Length)
39 | {
40 | return 0;
41 | }
42 |
43 | var a = itemsLength / 2;
44 |
45 | var maxStepIndex = 0;
46 |
47 | while (maxStepIndex < CiuraStepsIncrements.Length - 1 && CiuraStepsIncrements[maxStepIndex] < a)
48 | {
49 | maxStepIndex++;
50 | }
51 |
52 | if (maxStepIndex < stepNumber)
53 | {
54 | return 0;
55 | }
56 |
57 | return CiuraStepsIncrements[maxStepIndex - stepNumber];
58 | }
59 |
60 | public static void ShellSortAsc(this int[] array)
61 | {
62 | if (array.Length <= 1)
63 | return;
64 |
65 | var arrayLength = array.Length;
66 |
67 | var step = arrayLength / 2;
68 |
69 | while (step > 0)
70 | {
71 | for (int i = 0; i <= arrayLength - 1 - step; i++)
72 | {
73 | var j = i + step;
74 |
75 | while (j - step >= 0 && array[j] < array[j - step])
76 | {
77 | array.Swap(j - step, j);
78 |
79 | j--;
80 | }
81 | }
82 |
83 | step = step / 2;
84 | }
85 | }
86 | }
87 | }
--------------------------------------------------------------------------------
/SortingAlgoritms/SortingAlgorithms.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {0C929368-DCD5-40D7-8D71-8D21184F1790}
8 | Exe
9 | Properties
10 | SortingAlgorithms
11 | SortingAlgorithms
12 | v4.5
13 | 512
14 |
15 |
16 | AnyCPU
17 | true
18 | full
19 | false
20 | bin\Debug\
21 | DEBUG;TRACE
22 | prompt
23 | 4
24 |
25 |
26 | AnyCPU
27 | pdbonly
28 | true
29 | bin\Release\
30 | TRACE
31 | prompt
32 | 4
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
76 |
--------------------------------------------------------------------------------
/SortingAlgoritms/SortingHelper.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace SortingAlgorithms
4 | {
5 | public static class SortingHelper
6 | {
7 | public static bool IsSorted(this int[] array)
8 | {
9 | for (var i = 0; i < array.Length - 1; i++)
10 | {
11 | if (array[i] > array[i + 1])
12 | {
13 | return false;
14 | }
15 | }
16 |
17 | return true;
18 | }
19 |
20 | public static void InverseSort(this int[] array)
21 | {
22 | if (array.Length <= 1)
23 | {
24 | return;
25 | }
26 |
27 | var i = 0;
28 | var j = array.Length - 1;
29 |
30 | while (i < j)
31 | {
32 | array.Swap(j, i);
33 |
34 | i++;
35 | j--;
36 | }
37 | }
38 |
39 | public static void Swap(this int[] array, int index1, int index2)
40 | {
41 | var el = array[index1];
42 | array[index1] = array[index2];
43 | array[index2] = el;
44 | }
45 |
46 | public static int[] GetRandomArray(int arrayLength, int minValue = 0, int maxValue = 100)
47 | {
48 | var array = new int[arrayLength];
49 | var r = new Random();
50 |
51 | for (int i = 0; i < arrayLength; i++)
52 | {
53 | array[i] = r.Next(minValue, maxValue);
54 | }
55 |
56 | return array;
57 | }
58 |
59 | public static int[] CloneArray(this int[] collection)
60 | {
61 | return (int[])collection.Clone();
62 | }
63 | }
64 | }
--------------------------------------------------------------------------------
/SortingAlgoritms/SortingTimer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace SortingAlgorithms
4 | {
5 | public static class SortingTimer
6 | {
7 | public static void Go(Action sort, int[] array, string displayName)
8 | {
9 | Console.WriteLine("{0} ({1} elements)", displayName, array.Length);
10 | using (new Timer())
11 | {
12 | sort(array);
13 | }
14 |
15 | Console.WriteLine("---------------------------");
16 | }
17 | }
18 | }
--------------------------------------------------------------------------------
/SortingAlgoritms/StupidSort.cs:
--------------------------------------------------------------------------------
1 | namespace SortingAlgorithms
2 | {
3 | public static class StupidSort
4 | {
5 | public static void StupidSortAsc(this int[] array)
6 | {
7 | if (array.Length <= 1)
8 | return;
9 |
10 | var i = 0;
11 |
12 | while (i < array.Length - 1)
13 | {
14 | if (array[i] > array[i + 1])
15 | {
16 | array.Swap(i + 1, i);
17 |
18 | i = 0;
19 | }
20 | else
21 | {
22 | i++;
23 | }
24 | }
25 | }
26 | }
27 | }
--------------------------------------------------------------------------------
/SortingAlgoritms/Timer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Diagnostics;
3 |
4 | namespace SortingAlgorithms
5 | {
6 | public class Timer : IDisposable
7 | {
8 | private readonly Stopwatch _stopwatch;
9 |
10 | public Timer()
11 | {
12 | _stopwatch = new Stopwatch();
13 | _stopwatch.Start();
14 | }
15 |
16 | public void Dispose()
17 | {
18 | _stopwatch.Stop();
19 | DisplayTimespan();
20 | }
21 |
22 | private void DisplayTimespan()
23 | {
24 | TimeSpan ts = _stopwatch.Elapsed;
25 |
26 | string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
27 | ts.Hours, ts.Minutes, ts.Seconds,
28 | ts.Milliseconds);
29 |
30 | Console.WriteLine(elapsedTime);
31 | }
32 | }
33 | }
--------------------------------------------------------------------------------
/SortingAlgoritms/СountingSort.cs:
--------------------------------------------------------------------------------
1 | namespace SortingAlgorithms
2 | {
3 | public static class СountingSort
4 | {
5 | public static void СountingSortAsc(this int[] array)
6 | {
7 | if (array.Length <= 1)
8 | return;
9 |
10 | int maxValue = array[0];
11 | int minValue = array[0];
12 |
13 | for(var i = 0; i < array.Length - 1; i++)
14 | {
15 | if (array[i + 1] > maxValue)
16 | {
17 | maxValue = array[i + 1];
18 | }
19 | else if (array[i + 1] < minValue)
20 | {
21 | minValue = array[i + 1];
22 | }
23 | }
24 |
25 | var countsArray = new int[maxValue - minValue + 1];
26 |
27 | foreach (int item in array)
28 | {
29 | countsArray[item - minValue]++;
30 | }
31 |
32 | var currentIndex = 0;
33 |
34 | for (var i = 0; i < countsArray.Length; i++)
35 | {
36 | for (var j = 0; j < countsArray[i]; j++)
37 | {
38 | array[currentIndex++] = i + minValue;
39 | }
40 | }
41 | }
42 | }
43 | }
--------------------------------------------------------------------------------