chore(CSharp): add quick sort (#798)
parent
e42f4bb0b1
commit
ac970481a9
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sorts an array.
|
||||
/// </summary>
|
||||
/// <param name="array">Array to be sorted.</param>
|
||||
/// <param name="low">Starting index.</param>
|
||||
/// <param name="high">Ending index.</param>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <param name="array">Array to be sorted.</param>
|
||||
/// <param name="low">Starting index.</param>
|
||||
/// <param name="high">Ending index.</param>
|
||||
/// <returns></returns>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Swaps two elements.
|
||||
/// </summary>
|
||||
/// <param name="array">Array to be sorted.</param>
|
||||
/// <param name="i">An index.</param>
|
||||
/// <param name="j">Another index.</param>
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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)));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue