Here I am implementing a simple sort (insertion), but I'm not getting the correct size of the array. Namely, my length variable is getting set equal to 1. Also, what ways could I optimize my code for this insertion sort algorithm? Thanks
void insertionSort(int a[])
{
int sorted = 1;
int length = sizeof(a) / sizeof(int);
int i = 0;
int tmp;
printf("%d ", length);
if (length < 2)
return;
for (i = 1; i < length - 1; i++)
{
sorted = i;
while (a[sorted] < a[sorted - 1] && sorted > 0)
{
tmp = a[sorted];
a[sorted] = a[sorted - 1];
a[sorted - 1] = tmp;
sorted--;
}
}
}
This returns the size of a pointer a, divided by the size of an int. Always equal to one.
You will have to pass length to the function,
void insertionSort(int a[], size_t length){
The function that creates the array has to keep track of the number of elements that are active in the array in order to do this.