I am trying to sort an array of integers so that the evens print out in descending order FIRST and then the odd numbers in the array print out in ascending order.
So the output would look like:
8 6 4 2 1 3 5 7 9
How would I go about doing this?
#include <stdio.h>
#include <stdlib.h>
int compare(const void *p, const void *q);
void printArr(int arr[], int n);
//Driver program to test sort
int main()
{
int nums[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int size = sizeof(nums) / sizeof(nums[0]);
qsort((void*) nums, size, sizeof(nums[0]), compare);
printf("Sorted array is\n");
printArr(nums, size);
return 0;
}
//This function is used in qsort to decide the relative order of elements at addresses p and q
int compare(const void *p, const void *q)
{
return ( *(int*)p < *(int*)q);
}
//A utility function to print an array
void printArr(int arr[], int n)
{
int i;
for (i = 0; i < n; ++i)
printf("%d ", arr[i]);
}