Last active
November 29, 2017 07:02
-
-
Save ritwickdey/df21ed627b4a2397ccb9ff1c9a19bb8c to your computer and use it in GitHub Desktop.
Few Sorts in one place : Bubble Sort, Selection Sort, Insertion Sort, Marge Sort, Quick Sort, Heap Sort
This file contains hidden or 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
/* | |
--------------------- | |
Bubble Sort, | |
Selection Sort, | |
Insertion Sort, | |
Marge Sort, | |
Quick Sort, | |
Heap Sort | |
---------------------- | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
void SWAP(int *num1, int *num2) | |
{ | |
int temp1 = *num1; | |
*num1 = *num2; | |
*num2 = temp1; | |
} | |
// Bubble Sort | |
void BubbleSort(int *arr, int n) | |
{ | |
int i, j; | |
for (i = 0; i < n - 1; i++) | |
{ | |
for (j = 0; j < n - i - 1; j++) | |
{ | |
if (arr[j] > arr[j + 1]) | |
SWAP(&arr[j], &arr[j + 1]); | |
} | |
} | |
} | |
// Selection Sort | |
void SelectionSort(int *arr, int n) | |
{ | |
int i, j; | |
for (i = 0; i < n; i++) | |
{ | |
int minIndex = i; | |
for (j = i + 1; j < n; j++) | |
if (arr[j] < arr[minIndex]) | |
minIndex = j; | |
SWAP(&arr[i], &arr[minIndex]); | |
} | |
} | |
//Insertion Sort | |
void InsertionSort(int *arr, int n) | |
{ | |
int i = 0; | |
for (i = 1; i < n; i++) | |
{ | |
int key = arr[i]; | |
int j = i - 1; | |
while (j >= 0 && arr[j] > key) | |
arr[j + 1] = arr[j--]; | |
arr[j + 1] = key; | |
} | |
} | |
//Marge Sort | |
void Marge(int *arr, int low, int mid, int high) | |
{ | |
int i = low; | |
int j = mid + 1; | |
int c = 0; | |
int temp_Arr_size = high - low + 1; | |
int *tempArr = (int *)malloc(sizeof(arr[0]) * temp_Arr_size); | |
while (i <= mid && j <= high) | |
if (arr[i] < arr[j]) | |
tempArr[c++] = arr[i++]; | |
else | |
tempArr[c++] = arr[j++]; | |
while (i <= mid) | |
tempArr[c++] = arr[i++]; | |
while (j <= high) | |
tempArr[c++] = arr[j++]; | |
for (i = low, c = 0; i <= high; i++) | |
arr[i] = tempArr[c++]; | |
} | |
void MargeSort(int *arr, int low, int high) | |
{ | |
if (low < high) | |
{ | |
int mid = (high + low) / 2; | |
MargeSort(arr, low, mid); | |
MargeSort(arr, mid + 1, high); | |
Marge(arr, low, mid, high); | |
} | |
} | |
// Quick Sort | |
int Partition(int *arr, int low, int high) | |
{ | |
int pivotIndex = low; | |
int p = low; | |
int q = high; | |
while (p < q) | |
{ | |
while (arr[p] <= arr[pivotIndex]) | |
p++; | |
while (arr[q] > arr[pivotIndex]) | |
q--; | |
if (p < q) | |
SWAP(&arr[p], &arr[q]); | |
} | |
SWAP(&arr[q], &arr[pivotIndex]); | |
return q; | |
} | |
void QuickSort(int *arr, int low, int high) | |
{ | |
if (low < high) | |
{ | |
int p = Partition(arr, low, high); | |
QuickSort(arr, low, p - 1); | |
QuickSort(arr, p + 1, high); | |
} | |
} | |
//Heap Sort | |
void MaxHeapify(int *arr, int n, int index) | |
{ | |
int left = index * 2 + 1; | |
int right = index * 2 + 2; | |
int maxIndex = index; | |
if (left < n && arr[left] > arr[maxIndex]) | |
maxIndex = left; | |
if (right < n && arr[right] > arr[maxIndex]) | |
maxIndex = right; | |
if (maxIndex != index) | |
{ | |
SWAP(&arr[maxIndex], &arr[index]); | |
MaxHeapify(arr, n, maxIndex); | |
} | |
} | |
void BuildMaxHeap(int *arr, int n) | |
{ | |
int i = 0; | |
for (i = n / 2; i >= 0; i--) | |
MaxHeapify(arr, n, i); | |
} | |
void HeapSort(int *arr, int n) | |
{ | |
BuildMaxHeap(arr, n); | |
int i; | |
for (i = n - 1; i >= 1; i--) | |
{ | |
SWAP(&arr[0], &arr[i]); | |
BuildMaxHeap(arr, i); | |
} | |
} | |
void BlackBox(int *arr, int n) | |
{ | |
/* | |
Uncomment any one! | |
*/ | |
// BubbleSort(arr, n); | |
// SelectionSort(arr, n); | |
// InsertionSort(arr, n); | |
// MargeSort(arr, 0, n - 1); | |
// QuickSort(arr, 0, n - 1); | |
HeapSort(arr, n); | |
} | |
int main() | |
{ | |
int nums[] = {25, 45, 0, -85, 95, -9, 45, 23, 69}; | |
int n = sizeof(nums) / sizeof(nums[0]); | |
BlackBox(nums, n); | |
int i; | |
for (i = 0; i < n; i++) | |
{ | |
printf("%d ", nums[i]); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment