forked from DHEERAJHARODE/Hacktoberfest2024-Open-source-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Suyog-Kulkarni/Suyog-Kulkarni-patch-1
Added Sorting Algorithms in C#
- Loading branch information
Showing
3 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |