diff --git a/algorithms/CSharp/README.md b/algorithms/CSharp/README.md index 44fd6457..5f17d0b7 100644 --- a/algorithms/CSharp/README.md +++ b/algorithms/CSharp/README.md @@ -1,45 +1,57 @@ # C# + To run the `.cs` file, kindly use [.Net Finddle](https://dotnetfiddle.net/) ## Arrays + - [Single Number](src/Arrays/single-number.cs) ## Dynamic Programming + - [Longest Common Subsequence](src/Dynamic-Programming/longest-common-subsequence.cs) ## Number Theory + - [Big Mod Algorithm](src/Number-Theory/big-mod.cs) - [Sieve of Eratosthenes](src/Number-Theory/sieve-of-eratosthenes.cs) - [Bitwise Sieve of Eratosthenes](src/Number-Theory/bitwise-sieve-of-eratosthenes.cs) ## Sorts + - [Bubble Sort](src/Sorts/bubble-sort.cs) - [Insertion Sort](src/Sorts/insertion-sort.cs) - [Selection Sort](src/Sorts/selection-sort.cs) - [Counting Sort](src/Sorts/counting-sort.cs) - [Merge Sort](src/Sorts/merge-sort.cs) +- [Quick Sort](src/Sorts/quick-sort.cs) ## Strings + - [Palindrome](src/Strings/palindrome.cs) - [Trie](src/Strings/trie.cs) - [Character Limit](src/Strings/character-limit.cs) ## Search + - [Binary Search](src/Search/binary-search.cs) - [Linear Search](src/Search/linear-search.cs) - [Minima Maxima](src/Search/minima-maxima.cs) ## Maths + - [Abundant Number](src/Maths/abundant-number.cs) - [Naismith's Rule](src/Maths/naismith-rule.cs) ## Queues + - [Queue Implementation Using Two Stacks](src/Queues/queue-implementation-using-two-stacks.cs) ## Recursion + - [Factorial](src/Recursion/factorial.cs) ## Graph + - [Breadth First Search](src/Graph/breadth-first-search.cs) - [Depth First Search](src/Graph/depth-first-search.cs) - [Kruskals Algorithm to Find Minimum Spanning Tree](src/Graph/kruskals-algorithm.cs) diff --git a/algorithms/CSharp/src/Sorts/quick-sort.cs b/algorithms/CSharp/src/Sorts/quick-sort.cs new file mode 100644 index 00000000..0c95ee59 --- /dev/null +++ b/algorithms/CSharp/src/Sorts/quick-sort.cs @@ -0,0 +1,93 @@ +using System; + +namespace Algorithms.Sorts +{ + public class QuickSort + { + public static void Main() + { + int[] array = { 10, 7, 8, 9, 1, 5 }; + + Sort(array, 0, array.Length - 1); + PrintArray(array); + } + + /// + /// Sorts an array. + /// + /// Array to be sorted. + /// Starting index. + /// Ending index. + public static int[] Sort(int[] array, int low, int high) + { + if (low < high) + { + // Select a pivot + int pivot = Partition(array, low, high); + + // Sort each subarray + Sort(array, low, pivot - 1); + Sort(array, pivot + 1, high); + } + + return array; + } + + /// + /// This method takes the last element as pivot, places + /// it at its correct position in sorted array, and + /// places all smaller (smaller than pivot) + /// to left of it and all greater elements to its right. + /// + /// Array to be sorted. + /// Starting index. + /// Ending index. + /// + private static int Partition(int[] array, int low, int high) + { + int pivot = array[high]; + int i = low - 1; + + for (int j = low; j <= high - 1; j++) + { + // If current element is smaller than the pivot + if (array[j] < pivot) + { + // Increment index of smaller element and swap them + i++; + Swap(array, i, j); + } + } + + Swap(array, i + 1, high); + + return (i + 1); + } + + /// + /// Swaps two elements. + /// + /// Array to be sorted. + /// An index. + /// Another index. + private static void Swap(int[] array, int i, int j) + { + int temp = array[i]; + array[i] = array[j]; + array[j] = temp; + } + + static void PrintArray(int[] array) + { + Console.Write("Sorted array: "); + + for (int i = 0; i < array.Length; i++) + { + Console.Write($"{array[i]} "); + } + + Console.WriteLine(); + } + } +} + diff --git a/algorithms/CSharp/test/Sorts/quick-sort.cs b/algorithms/CSharp/test/Sorts/quick-sort.cs new file mode 100644 index 00000000..ade0bcf9 --- /dev/null +++ b/algorithms/CSharp/test/Sorts/quick-sort.cs @@ -0,0 +1,39 @@ +using NUnit.Framework; + +namespace Algorithms.Tests.Sorts +{ + [TestFixture] + public class QuickSort + { + static readonly object[] TestCasesForQuickSort = + { + new object[] { + new int[] { 0, 19, 12, 22, 107, 118, 0, 1, 2}, + "0, 0, 1, 2, 12, 19, 22, 107, 118" + }, + + new object[] { + new int[] { 10, 11, 19, 0, -1, -19, -12, 1, 2, 1, 16, -100}, + "-100, -19, -12, -1, 0, 1, 1, 2, 10, 11, 16, 19" + }, + + new object[] { + new int[] { -1, -2, -3, -4, -5, -10}, + "-10, -5, -4, -3, -2, -1" + }, + + new object[] { + new int[] { -1 }, + "-1" + } + }; + + [TestCaseSource(nameof(TestCasesForQuickSort))] + public void TestQuickSort_ShouldGetExpected(int[] array, string expected) + { + var results = Algorithms.Sorts.QuickSort.Sort(array, 0, array.Length - 1); + + Assert.That(expected, Is.EqualTo(string.Join(", ", results))); + } + } +}