#include <stdlib.h>
#include <stdio.h>
#include <time.h>
typedef void (*sortAlgorithm)(int *array, int size);
void insertionSort(int *array, int size) {
int i, j;
int temp;
for (i = 0; i < size; i++) {
temp = array[i];
for (j = i; j >= 1; j--) {
if (array[j-1] <= temp) {
break;
}
array[j] = array[j-1];
}
array[j] = temp;
}
}
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
void tests(int *nums, int size) {
sortAlgorithm sort_method = insertionSort;
clock_t start, end;
double cpu_time_used;
printf("==== Sorted array test results ====\n");
printf("Original:\n");
printArray(nums, size);
start = clock();
sort_method(nums, size);
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("Sorted:\n");
printArray(nums, size);
printf("CPU time used: %f\n\n", cpu_time_used);
}
int main() {
// test 1
int nums[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(nums)/sizeof(nums[0]);
tests(nums, n);
// test 2
int nums2[] = {1, 2, 4, 6, 8, 2, 3, 4, 0, -1, 10, 7, 8, 9, 1, 5};
n = sizeof(nums2)/sizeof(nums2[0]);
tests(nums2, n);
// test 3
int nums3[] = {};
n = sizeof(nums3)/sizeof(nums3[0]);
tests(nums3, n);
// test 4
int nums4[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
n = sizeof(nums4)/sizeof(nums4[0]);
tests(nums4, n);
// test 5
int nums5[] = {1, 2, -4, 6, -8, 2, 3, -4, 0, -1, 10, -1, 5};
n = sizeof(nums5)/sizeof(nums5[0]);
tests(nums5, n);
return 0;
}