Write an efficient function in C language that receives an array
Aof natural numbers and their amoun asn. The function must rearrange the arrayAlike this:In the left part all the even numbers will be sorted in ascending order and in the right part all the odd numbers will be descending down.
The function must run on the order of n·log(n).
It is forbidden to use an auxiliary array but we can use functions like :
quicksort , binarysearch , partition , merge_sort..
For example:
A={6, 8, 8, 10, 20, 5,1}: the functionA={ 1, 20, 5, 6, 8, 8, 10 }will rearrange the array
I tried to order the whole array using quick sort and then running all over the elements, putting the even numbers first and then counting how many even numbers there is. After that, using the evencounter starting one step after it tried to put the odd numbers and tried to sort them in descending order.
The complexity should be n·log(n) so I don't know whether running all over the elements is the right thing to do.
I think it is enough to use standard C function
qsort.Here is a demonstration program.
The program output is
Otherwise you could for example write a function that split an array in two partitions and sort the partitions separately.
Pay attention to this lone
without casting to
intthe result of expression will beunsigned int(the type of x and y) and never can be negative.If you want to have an array of signed integers then apart from other changes from unsigned int to int in this statement use standard C function
abs