From 779016541c89e2214b13838f0b79f6e69b3d927d Mon Sep 17 00:00:00 2001 From: Mahdi Saffari Date: Sat, 19 Jan 2019 11:45:34 +0330 Subject: [PATCH] QuickSort Added. --- Exercises and Samples/Program.cs | 11 +++++++--- Exercises and Samples/SortMethods.cs | 31 +++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/Exercises and Samples/Program.cs b/Exercises and Samples/Program.cs index 0baca74..7061ea5 100644 --- a/Exercises and Samples/Program.cs +++ b/Exercises and Samples/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; namespace Exercises_and_Samples { class Program { @@ -11,12 +12,16 @@ static void Main(string[] args) { //int Number = Convert.ToInt32(Console.ReadLine()); //Console.WriteLine("Index of {0} in the List is {1}", Number, BinarySearch_Recursive(Number, List, 0, List.Length)); - int[] List = new int[] { 10, 41, 15, 132, 102, 23, 265, 176, 90 ,10}; + int[] List = new int[] { 10, 41, 15, 132, 102, 23, 265, 176, 90, 10 }; Console.WriteLine("UnSorted: [{0}]", string.Join(", ", List)); //List = SortMethods.InsertionSort(List); //Console.WriteLine("Insertion Sorted: [{0}]", string.Join(", ", List)); - List = SortMethods.MergeSort(List); - Console.WriteLine("Merge Sorted: [{0}]", string.Join(", ", List)); + //List = SortMethods.MergeSort(List); + //Console.WriteLine("Merge Sorted: [{0}]", string.Join(", ", List)); + List = SortMethods.QuickSort(List); + List = SortMethods.QuickSort(List); + List = SortMethods.QuickSort(List.Reverse().ToArray()); + Console.WriteLine("Quick Sorted: [{0}]", string.Join(", ", List)); //for (int i = 0; i < List.Length; i++) { diff --git a/Exercises and Samples/SortMethods.cs b/Exercises and Samples/SortMethods.cs index 0277e76..437439f 100644 --- a/Exercises and Samples/SortMethods.cs +++ b/Exercises and Samples/SortMethods.cs @@ -16,7 +16,6 @@ public static T[] InsertionSort(T[] input) where T : IComparable { return result; } - public static T[] MergeSort(T[] input) where T : IComparable { int n = input.Length; @@ -47,6 +46,36 @@ public static T[] MergeSort(T[] input) where T : IComparable { return input; } + + public static T[] QuickSort(T[] input) where T : IComparable { + T[] result = input.Clone() as T[]; + QuickSort(result, 0, result.Length - 1); + return result; + } + private static void QuickSort(T[] input, int low, int high) where T : IComparable { + if (low < high) { + int q = Partition(input, low, high); + if (q > 1) { QuickSort(input, low, q - 1); } + if (q + 1 < high) { QuickSort(input, q + 1, high); } + } + } + private static int Partition(T[] input, int low, int high) where T : IComparable { + T x = input[low]; + int i = low + 1; + int j = high; + do { + while (i <= high && input[i].CompareTo(x) < 0) { i++; } + while (j >= low && input[j].CompareTo(x) > 0) { j--; } + if (i < j) { Swap(ref input[i], ref input[j]); } + } while (i < j); + Swap(ref input[low], ref input[j]); + return j; + } + private static void Swap(ref T v1, ref T v2) { + T temp = v1; + v1 = v2; + v2 = temp; + } }