Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8843169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:09:11+00:00 2026-06-14T11:09:11+00:00

I would like to sort an array by increasing order of frequency. For example,

  • 0

I would like to sort an array by increasing order of frequency. For example, if I had an array

int arr[] = { 3, 3, 10, 2, 5, 10, 10, 2, 2, 2 };

or another array would have the following sequence in it:

int arr[] = {5, 3, 3, 10, 10, 10, 2, 2, 2, 2};

However, I cannot use hashing or maps – I can only use arrays. What I have thought of is sorting the array using a quick sort algorithm, scanning the sorted array and performing the count in a 2d array so that for each element, there is a count associated with it, and then sorting by count. If two counts are same then I would merely print out the one with the lower value first. I’m having trouble implementing the last two steps. I’m not sure how to “map” a count to an index in the 2d array, nor am I sure on how to sort the 2d array by a count. Could anyone help me out? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:09:12+00:00Added an answer on June 14, 2026 at 11:09 am

    That’s how I’d code it without STL (requires additional O(n) memory):

    // Represents a bunch of equal numbers in an array
    struct Bunch
    {
      int x;  // value of numbers
      int n;  // count of numbers
    };
    
    int cmp_int(const void *x, const void *y)
    {
      return *static_cast<const int*>(x) - *static_cast<const int*>(y);
    }
    
    int cmp_bunch(const void *x, const void *y)
    {
      const Bunch* bx = static_cast<const Bunch*>(x);
      const Bunch* by = static_cast<const Bunch*>(y);
      return (bx->n != by->n) ? bx->n - by->n : bx->x - by->x;
    }
    
    void sort_by_freq(int arr[], int arr_size)
    {
      // Buffer array to store counted bunches of numbers
      Bunch* buf = new Bunch [arr_size];
      int buf_size = 0;
    
      // Sort input array
      qsort(arr, arr_size, sizeof(int), cmp_int);
    
      // Compute bunches
      Bunch bunch;
      bunch.x = arr[0];
      bunch.n = 1;
      for (int i = 1; i < arr_size; ++i)
      {
        if (arr[i] > bunch.x)
        {
          buf[buf_size++] = bunch;
          bunch.x = arr[i];
          bunch.n = 1;
        }
        else
        {
          ++bunch.n;
        }
      }
      buf[buf_size++] = bunch;  // Don't forget the last one!
    
      // Sort bunches
      qsort(buf, buf_size, sizeof(Bunch), cmp_bunch);
    
      // Populate bunches to the input array
      int i = 0;
      for (int k = 0; k < buf_size; ++k)
        for (int j = 0; j < buf[k].n; ++j) arr[i++] = buf[k].x;
    
      // Don't forget to deallocate buffer, since we cannot rely on std::vector...
      delete [] buf;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a one-column array that I would like to sort in descending order.
I would like to sort my int array in ascending order. first I make
I have an array which i would like to sort it based on values
I have an array which i would like to sort it based on values
I have an array that I would like to sort using a date field
I have a very large multidimensional array and I would like to sort it
I have an array of appointment objects, and I would like to sort by
I have defined the following Array Dim myArray(10,5) as Long and would like to
I would like to sort the following names Array ( [Jessie] => 2 [Sarah]
I have an array of strings (names) and i would like to sort them

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.