How can i get this function to sort in descending order inst
How can i get this function to sort in descending order instead of ascending in c?
*q is a pointer to an array and 0 is being passed into i.
void insertionSort(int *q, int i)
{
if (i < SIZE)
{
int j;
int temp = q[i];
for (j = i; j > 0 && q[j - 1] > temp; j--)
{
q[j] = q[j - 1];
q[j] = temp;
}
insertionSort(q, ++i);
}
}
Solution
#include <stdio.h>
int main()
{
int arr[100];
int size, i, j, temp;
/*
* Read size of array and elements in array
*/
printf(\"Enter size of array: \");
scanf(\"%d\", &size);
printf(\"Enter elements in array: \");
for(i=0; i<size; i++)
{
scanf(\"%d\", &arr[i]);
}
/*
* Array sorting code
*/
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
/*
* If there is a smaller element towards left of the array then swap it.
*/
if(arr[i] < arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
/*
* Prints the sorted array
*/
printf(\"\ Elements of array in sorted descending order: \");
for(i=0; i<size; i++)
{
printf(\"%d\\t\", arr[i]);
}
return 0;
}

