I am trying to sort my input array pAddrSourceArray in and ascending way, but the input array is defined as const as it is shown in below header, that cannot be changed since it is imposed.
So far I have the idea of how to sort the array, however I am struggling to overcome the fact that the input array is defined as constant. I cannot (or I don't know) how to create an auxiliary array with the content of the input one, so that I can sort this auxiliary one and then assign to the output array pAddrResultArray
Any hints of how to solve this?
void sortArray(uint32_t samplesCount, const int16_t *pAddrSourceArray, int16_t *pAddrResultArray)
{
int16_t *pInputArray = pAddrSourceArray; // This is wrong, I know it should be const int16_t *pInputArray = pAddrSourceArray;
int16_t min_idx;
// One by one move boundary of unsorted subarray
for (uint32_t indxUnsort = 0; indxUnsort < samplesCount - 1; indxUnsort++ )
{
// Find the minimum element in unsorted array
min_idx = indxUnsort;
for (uint32_t k = indxUnsort + 1; k < samplesCount; k++)
{
if (pInputArray[k] < pInputArray[min_idx])
{
min_idx = k;
}
}
// Store actual local min value
int16_t localMin = pInputArray[min_idx];
// Update new min at current indxUsort position
pInputArray[min_idx] = pInputArray[indxUnsort];
// Append the new local min found
pInputArray[indxUnsort] = localMin;
}
// Assign sorted array to output array
pAddrResultArray = pInputArray;
}