I have this bubble sort function:
void bubble_sort(float* array, int length)
{
int c, d;
float temp;
for (c = 0; c < (length - 1); c++) {
for (d = 0; d < length - c - 1; d++) {
if (array[d] > array[d + 1]) {
temp = array[d];
array[d] = array[d + 1];
array[d + 1] = temp;
}
}
}
}
How do I change it so that I can use it for double
as well? I want to be able to pass a float array one time and a double array another time, but it has to be the same function. Something like this:
float farr[SIZE];
double darr[SIZE];
...
bouble_sort(farr, SIZE);
bouble_sort(darr, SIZE);
EDIT: I rewrote the sorting function and it seems to work fine now. What do you think?
void bubble_sort(void* generalArray, int lenght_row, char type)
{
int column_sort;
int sorting_process = 0;
if (type == 'f')
{
float temp;
float* array = (float *) generalArray;
while (sorting_process == 0)
{
sorting_process = 1;
for (column_sort = 0; column_sort < lenght_row - 1; column_sort++)
{
if (array[column_sort] > array[column_sort + 1])
{
temp = array[column_sort + 1];
array[column_sort + 1] = array[column_sort];
array[column_sort] = temp;
sorting_process = 0;
}
}
}
}
else if (type == 'd')
{
double temp; // added
double* array = (double *) generalArray;
while (sorting_process == 0)
{
sorting_process = 1;
for (column_sort = 0; column_sort < lenght_row - 1; column_sort++)
{
if (array[column_sort] > array[column_sort + 1])
{
temp = array[column_sort + 1];
array[column_sort + 1] = array[column_sort];
array[column_sort] = temp;
sorting_process = 0;
}
}
}
}
}