#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char s1[]="insertionSort", s2[]="quickSort" , s3[]="mergeSort", s4[]="heapSort";
void print(char s[], int a[]){
puts(s);
for(int i = 1; i <= 10; i++){
printf("%d ", a[i]);
}
printf("\n");
}
void insert(int e, int a[], int i){
a[0] = e;
while(e < a[i]){
a[i+1] = a[i];
i--;
}
a[i+1] = e;
}
void insertionSort(int a[], int n){
int j;
for(j = 1; j <= n; j++){
insert(a[j], a, j-1);
}
}
void swap(int *a, int *b, int temp){
temp = *a;
*a = *b;
*b = temp;
}
void quickSort(int a[], int left, int right){
int pivot, i, j;
int temp;
if(left < right){
i = left;
j = right + 1;
pivot = a[left];
do{
do i++; while(a[i] < pivot);
do j--; while(a[j] > pivot);
if(i < j) swap(&a[i], &a[j], temp);
}while(i < j);
swap(&a[left], &a[j], temp);
quickSort(a, left, j-1);
quickSort(a, j+1, right);
}
}
void merge(int initList[], int mergedList[], int i, int m, int n){
int j, k, t;
j = m + 1;
k = i;
while(i <= m && j <= n){
if(initList[i] <= initList[j]) mergedList[k++] = initList[i++];
else mergedList[k++] = initList[j++];
}
if(i > m){
for(t = j; t <= n; t++){
mergedList[t] = initList[t];
}
}else{
for(t = i; t <= m; t++){
mergedList[k+t-i] = initList[t];
}
}
}
void mergePass(int initList[], int mergedList[], int n, int s){
int i, j;
for(i=1; i <= n-2*s+1; i+=2*s){
merge(initList, mergedList, i, i+s-1, i+2*s-1);
}
if(i+s-1 < n) merge(initList, mergedList, i, i+s-1, n);
else{
for(j = i; j <= n; j++)
mergedList[j] = initList[j];
}
}
void mergeSort(int a[], int n){
int s = 1;
int extra[1000];
while(s < n){
mergePass(a, extra, n, s);
s*=2;
mergePass(extra, a, n, s);
s*=2;
}
}
void adjust(int a[], int root, int n){
int child, rootkey;
int temp = a[root];
rootkey = a[root];
child = 2*root;
while(child <= n){
if((child < n) && (a[child] < a[child+1])) child++;
if(rootkey > a[child]) break;
else {
a[child/2] = a[child];
child *= 2;
}
}
a[child/2] = temp;
}
void heapSort(int a[], int n){
int i, j;
int temp;
for(i=n/2; i>0; i--)
adjust(a, i, n);
for(i=n-1; i>0; i--){
swap(&a[1], &a[i+1], temp);
adjust(a, 1, i);
}
}
int main(){
int a[11] = {0, 26, 5, 37, 1, 61, 11, 59, 15, 48, 19};
insertionSort(a, 10);
print(s1, a);
int b[11] = {0, 26, 5, 37, 1, 61, 11, 59, 15, 48, 19};
quickSort(b, 1, 10);
print(s2, b);
int c[11] = {0, 26, 5, 37, 1, 61, 11, 59, 15, 48, 19};
mergeSort(c, 10);
print(s3, c);
int d[11] = {0, 26, 5, 37, 1, 61, 11, 59, 15, 48, 19};
heapSort(d, 10);
print(s4, d);
}