From f4bc9c9ae0f20d792f70ba6ab4903800c9f56544 Mon Sep 17 00:00:00 2001 From: Tanuj Yadav Date: Sun, 7 Oct 2018 01:09:21 +0530 Subject: [PATCH] Created QuickSort.cs C# program for QuickSort --- QuickSort.cs | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 QuickSort.cs diff --git a/QuickSort.cs b/QuickSort.cs new file mode 100644 index 0000000..aec98e5 --- /dev/null +++ b/QuickSort.cs @@ -0,0 +1,57 @@ +// C# program for implementation of QuickSort +using System; + +class GFG { + + static int partition(int []arr, int low, + int high) + { + int pivot = arr[high]; + + int i = (low - 1); + for (int j = low; j < high; j++) + { + if (arr[j] <= pivot) + { + i++; + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + } + + int temp1 = arr[i+1]; + arr[i+1] = arr[high]; + arr[high] = temp1; + + return i+1; + } + + static void quickSort(int []arr, int low, int high) + { + if (low < high) + { + int pi = partition(arr, low, high); + + quickSort(arr, low, pi-1); + quickSort(arr, pi+1, high); + } + } + + static void printArray(int []arr, int n) + { + for (int i = 0; i < n; ++i) + Console.Write(arr[i] + " "); + + Console.WriteLine(); + } + + public static void Main() + { + int []arr = {10, 7, 8, 9, 1, 5}; + int n = arr.Length; + quickSort(arr, 0, n-1); + Console.WriteLine("sorted array "); + printArray(arr, n); + } +}