From fe50da039db5435d3b340b364fe09e749faf33e0 Mon Sep 17 00:00:00 2001 From: Suyog Kulkarni <98457685+Suyog-Kulkarni@users.noreply.github.com> Date: Tue, 10 Oct 2023 23:51:19 +0530 Subject: [PATCH] Added Sorting Algorithms in C# Implemented Quick Sort, Insertion Sort, and Merge Sort using C#. --- InsertionSort.cs | 43 +++++++++++++++++++++ MergeSort.cs | 99 ++++++++++++++++++++++++++++++++++++++++++++++++ QuickSort.cs | 66 ++++++++++++++++++++++++++++++++ 3 files changed, 208 insertions(+) create mode 100644 InsertionSort.cs create mode 100644 MergeSort.cs create mode 100644 QuickSort.cs diff --git a/InsertionSort.cs b/InsertionSort.cs new file mode 100644 index 000000000..54e361c13 --- /dev/null +++ b/InsertionSort.cs @@ -0,0 +1,43 @@ +using System; + +class InsertionSort +{ + public static void Sort(int[] arr) + { + for (int i = 1; i < arr.Length; i++) + { + int key = arr[i]; + int j = i - 1; + + while (j >= 0 && arr[j] > key) + { + arr[j + 1] = arr[j]; + j--; + } + + arr[j + 1] = key; + } + } + + public static void Main(string[] args) + { + int[] arr = { 12, 4, 5, 6, 7, 3, 1, 15 }; + + Console.Write("Original Array: "); + PrintArray(arr); + + Sort(arr); + + Console.Write("Sorted Array: "); + PrintArray(arr); + } + + private static void PrintArray(int[] arr) + { + foreach (var item in arr) + { + Console.Write(item + " "); + } + Console.WriteLine(); + } +} diff --git a/MergeSort.cs b/MergeSort.cs new file mode 100644 index 000000000..5fc9e5da1 --- /dev/null +++ b/MergeSort.cs @@ -0,0 +1,99 @@ +using System; + +class MergeSort +{ + public static void Sort(int[] arr) + { + if (arr == null || arr.Length <= 1) + return; + + MergeSortRecursive(arr, 0, arr.Length - 1); + } + + private static void MergeSortRecursive(int[] arr, int left, int right) + { + if (left < right) + { + int middle = (left + right) / 2; + + MergeSortRecursive(arr, left, middle); + MergeSortRecursive(arr, middle + 1, right); + + Merge(arr, left, middle, right); + } + } + + private static void Merge(int[] arr, int left, int middle, int right) + { + int n1 = middle - left + 1; + int n2 = right - middle; + + int[] leftArray = new int[n1]; + int[] rightArray = new int[n2]; + + for (int i = 0; i < n1; i++) + { + leftArray[i] = arr[left + i]; + } + + for (int j = 0; j < n2; j++) + { + rightArray[j] = arr[middle + 1 + j]; + } + + int k = left; + int iIndex = 0; + int jIndex = 0; + + while (iIndex < n1 && jIndex < n2) + { + if (leftArray[iIndex] <= rightArray[jIndex]) + { + arr[k] = leftArray[iIndex]; + iIndex++; + } + else + { + arr[k] = rightArray[jIndex]; + jIndex++; + } + k++; + } + + while (iIndex < n1) + { + arr[k] = leftArray[iIndex]; + iIndex++; + k++; + } + + while (jIndex < n2) + { + arr[k] = rightArray[jIndex]; + jIndex++; + k++; + } + } + + public static void Main(string[] args) + { + int[] arr = { 12, 4, 5, 6, 7, 3, 1, 15 }; + + Console.Write("Original Array: "); + PrintArray(arr); + + Sort(arr); + + Console.Write("Sorted Array: "); + PrintArray(arr); + } + + private static void PrintArray(int[] arr) + { + foreach (var item in arr) + { + Console.Write(item + " "); + } + Console.WriteLine(); + } +} diff --git a/QuickSort.cs b/QuickSort.cs new file mode 100644 index 000000000..0bfc74dd4 --- /dev/null +++ b/QuickSort.cs @@ -0,0 +1,66 @@ +using System; + +class QuickSort +{ + public static void Sort(int[] arr) + { + QuickSortRecursive(arr, 0, arr.Length - 1); + } + + private static void QuickSortRecursive(int[] arr, int left, int right) + { + if (left < right) + { + int pivotIndex = Partition(arr, left, right); + QuickSortRecursive(arr, left, pivotIndex - 1); + QuickSortRecursive(arr, pivotIndex + 1, right); + } + } + + private static int Partition(int[] arr, int left, int right) + { + int pivot = arr[right]; + int i = left - 1; + + for (int j = left; j < right; j++) + { + if (arr[j] <= pivot) + { + i++; + Swap(arr, i, j); + } + } + + Swap(arr, i + 1, right); + return i + 1; + } + + private static void Swap(int[] arr, int i, int j) + { + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + + public static void Main(string[] args) + { + int[] arr = { 12, 4, 5, 6, 7, 3, 1, 15 }; + + Console.Write("Original Array: "); + PrintArray(arr); + + Sort(arr); + + Console.Write("Sorted Array: "); + PrintArray(arr); + } + + private static void PrintArray(int[] arr) + { + foreach (var item in arr) + { + Console.Write(item + " "); + } + Console.WriteLine(); + } +}