Skip to content
This repository has been archived by the owner on May 10, 2020. It is now read-only.

Commit

Permalink
QuickSort Added.
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsaffari committed Jan 19, 2019
1 parent c5d4ec8 commit 7790165
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
11 changes: 8 additions & 3 deletions Exercises and Samples/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;

namespace Exercises_and_Samples {
class Program {
Expand All @@ -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++) {
Expand Down
31 changes: 30 additions & 1 deletion Exercises and Samples/SortMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public static T[] InsertionSort<T>(T[] input) where T : IComparable {
return result;
}


public static T[] MergeSort<T>(T[] input) where T : IComparable {
int n = input.Length;

Expand Down Expand Up @@ -47,6 +46,36 @@ public static T[] MergeSort<T>(T[] input) where T : IComparable {

return input;
}

public static T[] QuickSort<T>(T[] input) where T : IComparable {
T[] result = input.Clone() as T[];
QuickSort(result, 0, result.Length - 1);
return result;
}
private static void QuickSort<T>(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>(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<T>(ref T v1, ref T v2) {
T temp = v1;
v1 = v2;
v2 = temp;
}
}


Expand Down

0 comments on commit 7790165

Please sign in to comment.