Sorting an array of numbers by descending frequency Somebody asked me to create a code that orders unique numbers of an array according to their frequency, ie:

{1,3,3,4,4,4,4,4,2,2,5,5,5,5}


to

{4,5,3,2,1}


I'm a beginner and only started learning C last week so my code probably isn't optimal. I managed to get it in just over 100 lines without worrying too much about space. I haven't messed with memory allocation yet but I should start looking into it.

Any tips or feedback?

#include &lt;stdio.h&gt;
#include &lt;string.h&gt;


void CalculateFrequency(int numbers[], int frequency[]) //Populates an array with the frequency of numbers in another
{
    int hasSorted = 0;
    do
    {
        hasSorted = 0;
        for(int currentInt=0; currentInt&lt;10; currentInt++)
        {
            for(int i=0; i&lt;10; i++)
            {
                if(numbers[i] == currentInt)
                {
                    frequency[currentInt]++;
                    hasSorted = 1;
                }
            }
        }
        if(hasSorted == 0)
        {
            break;
        }
    }while(hasSorted == 0);
}
void SortByFrequency(int numbers[], int frequency[])    //Sorts an array according to the frequency of the numbers
{
    int hasSorted = 0;
    int temp = 0;
    do
    {
        hasSorted = 0;
        for(int i=0; i&lt;10; i++)
        {
            for(int i=0; i&lt;10; i++)
            {
                if(frequency[numbers[i]] &lt; frequency[numbers[i+1]])
                {
                    temp = numbers[i+1];
                    numbers[i+1] = numbers[i];
                    numbers[i] = temp;
                    hasSorted = 1;
                }
            }
        }
        if(hasSorted == 0)
        {
            break;
        }
    }while(hasSorted == 0);
}
int CountUniqueNumbers(int array[], int arrayLength)    //Counts unique numbers in an array
{
    int count = 1; //At least 1 number
    for(int i=0; i&lt;arrayLength-1; i++)
    {
        if(array[i] != array[i+1])
        {
            count++;
        }
    }
    return count;
}
int PopulateArrayByFrequencyAndReturnLength(int numbers[], int sortedByFrequency[])
{
    int k = 0;
    sortedByFrequency[0] = numbers[0];
    for(int i=0; i&lt;10; i++)
    {
        if(numbers[i] != numbers[i+1])
        {
            sortedByFrequency[k] = numbers[i];
            k++;
        }
    }
    return k;
}
int counter;

int main(void)
{
    int numbers[10] = {1,2,2,2,5,7,8,8,8,8};
    int frequency[10] = {0,0,0,0,0,0,0,0,0,0};
    int sortedByFrequency[10] = {0,0,0,0,0,0,0,0,0,0};
    int sortedByFrequencyTrueLength = 0;
    int differentNumbers = 1;   /*The array must have at least 1 number*/
    int sizeofNumbersArray = 10;
    int uniqueNumbersInArray = 0;
    int i = 0;
    /*print the numbers*/
    printf("Numbers:\t");
    for(int i=0; i&lt;10; i++)
    {
        printf("%d ", numbers[i]);
    }
    puts("");
    /*Perform functions*/
    CalculateFrequency(numbers, frequency);
    SortByFrequency(numbers, frequency);
    /*Get amount of unique numbers in the array*/
    uniqueNumbersInArray = CountUniqueNumbers(numbers, sizeofNumbersArray);
    /*Poupulate the unique number frequency array and get the true length*/
    sortedByFrequencyTrueLength = PopulateArrayByFrequencyAndReturnLength(numbers, sortedByFrequency);
    //Print unique number frequency array
    printf("Numbers sorted by frequency:\t");
    for(i=0; i&lt;sortedByFrequencyTrueLength; i++)
    {
        printf("%d ", sortedByFrequency[i]);
    }
    puts("");

    return 0;
}
I see a couple occurrences of:

do
{
    hasSorted = 0;
    ...other things...
    if(hasSorted == 0)
    {
        break;
    }
}while(hasSorted == 0);


Did you mean if(hasSorted == 1) in those? If not, then you might want to just get rid of the whole if statement and replace the while condition with while(hasSorted != 0).



Also, comments that describe the whole function could look better on a separate line right before the function, so that the line doesn't get too long.