Skip to content

Commit

Permalink
Merge pull request #1 from Suyog-Kulkarni/Suyog-Kulkarni-patch-1
Browse files Browse the repository at this point in the history
Added Sorting Algorithms in C#
  • Loading branch information
Suyog-Kulkarni authored Oct 10, 2023
2 parents a9bdbeb + fe50da0 commit 0a48c12
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 0 deletions.
43 changes: 43 additions & 0 deletions InsertionSort.cs
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();
}
}
99 changes: 99 additions & 0 deletions MergeSort.cs
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();
}
}
66 changes: 66 additions & 0 deletions QuickSort.cs
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();
}
}

0 comments on commit 0a48c12

Please sign in to comment.