Skip to content

Instantly share code, notes, and snippets.

@hsynkrcf
Last active June 21, 2024 01:01
Show Gist options
  • Save hsynkrcf/f2e247672e3f5b03daa0275793eb0ee8 to your computer and use it in GitHub Desktop.
Save hsynkrcf/f2e247672e3f5b03daa0275793eb0ee8 to your computer and use it in GitHub Desktop.
Sorting Algorithms Examples
namespace Sorting;
internal class SortingAlgorithms
{
public static int[] BubbleSort(int[] arr)
{
int n = arr.Length;
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr;
}
public static int[] SelectionSort(int[] arr)
{
int n = arr.Length;
for (int i = 0; i < n - 1; i++)
{
int minIdx = i;
for (int j = i + 1; j < n; j++)
{
if (arr[j] < arr[minIdx])
{
minIdx = j;
}
}
int temp = arr[minIdx];
arr[minIdx] = arr[i];
arr[i] = temp;
}
return arr;
}
public static int[] 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);
}
return arr;
}
public static int[] InsertionSort(int[] arr)
{
int n = arr.Length;
for (int i = 1; i < n; ++i)
{
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
return arr;
}
public static int[] HeapSort(int[] arr)
{
int n = arr.Length;
for (int i = n / 2 - 1; i >= 0; i--)
Heapify(arr, n, i);
for (int i = n - 1; i >= 0; i--)
{
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
Heapify(arr, i, 0);
}
return arr;
}
public static int[] MergeSort(int[] arr, int left, int right)
{
if (left < right)
{
int mid = (left + right) / 2;
MergeSort(arr, left, mid);
MergeSort(arr, mid + 1, right);
Merge(arr, left, mid, right);
}
return arr;
}
#region Private Methods
private 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;
}
private static void Heapify(int[] arr, int n, int i)
{
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest])
largest = left;
if (right < n && arr[right] > arr[largest])
largest = right;
if (largest != i)
{
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
Heapify(arr, n, largest);
}
}
private static void Merge(int[] arr, int left, int mid, int right)
{
int n1 = mid - left + 1;
int n2 = right - mid;
int[] L = new int[n1];
int[] R = new int[n2];
for (int x = 0; x < n1; ++x)
L[x] = arr[left + x];
for (int y = 0; y < n2; ++y)
R[y] = arr[mid + 1 + y];
int k = left;
int i = 0, j = 0;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
#endregion
}
using Sorting;
int[] willBeSorted = { 5, 3, 8, 4, 1, 2, 7, 6 };
var bubbleSorted = SortingAlgorithms.BubbleSort(willBeSorted);
var selectionSorted = SortingAlgorithms.SelectionSort(willBeSorted);
var quickSorted = SortingAlgorithms.QuickSort(willBeSorted, 0, 7);
var insertionSorted = SortingAlgorithms.InsertionSort(willBeSorted);
var heapSorted = SortingAlgorithms.HeapSort(willBeSorted);
var mergeSorted = SortingAlgorithms.MergeSort(willBeSorted, 0, 7);
foreach (var item in mergeSorted)
{
Console.Write(item + " ");
}
Console.ReadLine();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment