In order not to overstretch I'm going straight to the point, I'm trying to make a program for an exercise that has the following statement:
Build an efficient
int ordenaFreq(int v[], int n)
function, ie runtime in the worst case it must be O (n log n), capable of ordering a vector of elements v of size n by frequency of appearance of each element.For example, given a vector V = [2, 5, 2, 6, -1, 9999999, 5, 8, 8, 8], its function must return a vector W such that W = [8, 8, 8, 2, 5, 5, 6, -1, 9999999].
I have to use this function in my program but I can not think how to solve this problem I already used Insertion Sort to organize the vector, but after that I do not know how to develop this function, especially considering the complexity that has to be used in the function. I currently have the following code done:
#include<stdio.h>
const int max=100;
//Function Prototyping
int InsetionSort(int[],int);
int ordenaFreq(int[],int);
int saida(int);
//Main Function
int main(void)
{
int escolha=10;
do
{
int v[max];
int end;
printf("Entre com o tamanho do arranjo:\n");
scanf("%d",&end);
printf("Entre com os elementos do arranjo:\n");
printf("Numeros com virgula use ponto para separar a parte decimal!\n");
for(int i=0; i<end; i++)
scanf("%d",&v[i]);
InsetionSort(v,end);
printf("Ordenado fica:\n");
printf("[");
for(int i=0; i<end; i++)
printf("%d ",v[i]);
printf("]\n");
}
while(saida(escolha)!=0);
return 0;
}
//Auxiliary Functions
int InsetionSort(int v[],int end)
{
int first,before;
int aux;
for(first=1; first<end; first++)
{
aux=v[first];
for(before=first-1;before>=0 && v[before]<aux;before--)
v[before+1]=v[before];
v[before+1]=aux;
}
}
int ordenaFreq(int v[], int end)
{
int saida(int escolha)
{
printf("Deseja encerrar o programa?\n");
printf("Digite 0 para fechar ou qualquer outro numero para continuar.\n");
scanf("%d",&escolha);
if(escolha==0)
return 0;
else return 10;
}
If possible I would also like to ask you to help me understand the complexity, if it is not too uncomfortable because it is fairly recent for me that part so I do not understand very well how it works right.