Merge Sort / Insertion Sort/ bubble sort/ selection sort
#include <iostream>
using namespace std;
int CUTOFF = 7;
int arrAux[7];
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
// A function to implement bubble sort
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}
void insertionSort(int arr[], int n)
{
for (int i = 0; i < n-1; i++)
{
for (int j = i+1; j > 0; j--)
{
if (arr[j] < arr[j-1])
swap(&arr[j], &arr[j-1]);
}
}
}
void selectionSort(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
int min_index = i;
for (int j = i+1; j < n; j++)
{
if (arr[min_index] > arr[j])
min_index = j;
}
swap(&arr[i], &arr[min_index]);
}
}
void merge(int* arr, int low, int mid, int high)
{
int p = low;
int q = mid+1;
for (int i = low; i <= high; i++)
{
arrAux[i] = arr[i];
}
for (int i = low; i <= high; i++)
{
if (p > mid)
arr[i] = arr[q++];
else if (q > high)
arr[i] = arrAux[p++];
else if (arrAux[p] > arrAux[q])
{
arr[i] = arrAux[q++];
}
else
{
arr[i] = arrAux[p++];
}
}
}
void Mergesort(int arr[], int low, int high)
{
// Imporvement #1 : using insertion sort for smaller array
if (high < low + CUTOFF - 1)
{
insertionSort(arr, high-1);
}
if (low >= high)
return;
int mid = (low + high)/2;
Mergesort(arr, low, mid);
Mergesort(arr, mid+1, high);
// Improvement #2 : Stop if it is alrady sorted
if (arr[mid+1] > arr[mid])
return;
merge(arr, low, mid, high);
}
/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
//printf("n");
}
int main()
{
//int arr[] = {64, 34, 25, 12, 22, 11, 90};
int arr[] = {64, 34, 25, 12, 22, 11, 90, 2, 23, 39, 89, 88, 123, 10};
int n = sizeof(arr)/sizeof(arr[0]);
//selectionSort(arr, n);
Mergesort(arr, 0 , n-1);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
#include <iostream>
using namespace std;
#define ARRARY_SIZE 10
int aux[ARRARY_SIZE] = {};
int count_inversion_merge(int* a, int first, int mid, int last)
{
int p1 = first;
int p2 = mid+1;
int count = 0;
for (int i = first; i <= last; i++)
{
aux[i] = a[i];
}
for (int i = first; i <= last; i++)
{
if (p1 > mid)
{
a[i] = aux[p2++];
}
else if (p2 > last)
{
a[i] = aux[p1++];
}
else if( aux[p1] > aux[p2])
{
a[i] = aux[p2++];
count += (mid - p1 + 1);
}
else
{
a[i] = aux[p1++];
}
}
return count;
}
int count_inversion(int* a, int first, int last)
{
int counter = 0;
if (first >= last)
return 0;
int mid = (first + last) / 2;
int c1 = count_inversion(a, first, mid);
int c2 = count_inversion(a, mid+1, last);
int c3 = count_inversion_merge(a, first, mid, last);
return c1+c2+c3;
}
int main()
{
int a[] = {9, 8 , 7, 6, 5, 4, 3, 2, 1, 10};
static_assert(sizeof(a)/sizeof(int) == ARRARY_SIZE, "The arrary size doesn't match");
int len = ARRARY_SIZE;
int inversion = count_inversion(a, 0, len-1);
int i;
for (i=0 ; i<len; i++)
printf("%d ", a[i]);
printf("\n");
printf("Total inversions : %d \n", inversion);
return 0;
}