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

  • Home
  • SEARCH
  • 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 3243238
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:24:33+00:00 2026-05-17T18:24:33+00:00

I have a randomly ordered C array of integer numbers. Each of these numbers

  • 0

I have a randomly ordered C array of integer numbers.

Each of these numbers represents a color, and has its relationship to every other number in the array defined elsewhere (they are non-linear, and are based on both the brightness of the color and the hue).

I need a quick, efficient algorithm to sort these numbers based on their resemblance to each other. The sorting is either so that the the numbers in the array cluster based on resemblance, or the opposite, i.e. in such a way that numbers that resemble each other are as far from each other as possible.

What’s the best way to do this?

  • 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-05-17T18:24:33+00:00Added an answer on May 17, 2026 at 6:24 pm

    First, I’m assuming that you are going to use the qsort or similar function to do your sorting, and need a comparison function to pass to that. These comparison functions behave like memcnp and strcmp — returning an integer to signify less than, equal to, or greater than.

    One way to do this is by treating the entire color value as one big number as far as the comparison goes:

    int bright_hue_compare(const void * a, const void * b) {
        int rc = bright_compare(a, b);
        if (!rc) {
            rc = hue_compare(a, b);
        }
        return rc;
    }
    

    This would group colors first by their brightness (assuming that you write a brightness compare function) and second by their hue. You may want to swap the order of these, and internally they can be more complicated.

    Sorting such that similar colors are further from each other is more complicated, since you really need to compare it to several values possible neighbor values at a time to space them out further. I doubt that you could get this reliably with quicksort (the stdlib qsort function may not be quicksort, but assuming that it is), though:

    int bright_hue_inverse_compare(const void * a, const void * b) {
        int rc = bright_hue_compare(a, b);
        if (rc) {
           return 0;
        }
        return random(); // so that they are the same so randomize greater/lesser
    }
    

    Might be good enough for you, but yields far from optimal results.

    Really you would probably have to write your own sorting function for this, and it will probably be have a very high run time because each color needs to be compared against many of its neighbors. The more optimal you want this distribution the more this starts to look like an AI problem where each color wants to move as far away from like colors as it can.

    Oh, something that I just thought of that might yield good results would be if averaged all of the hues and all of the brightnesses and then divided the array in half and tried to make each sub-array’s averages be as close as you could to the averages of the entire array as you could by swapping some colors between the arrays to balance things out. Then you divide these arrays in half and repeat. I don’t think you will (and probably can’t) get optimal results with this, but I think it’s may be pretty good. Finding what to swap would be the biggest problem here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While running a batch file in Windows XP I have found randomly occurring error
Since upgrading to 2008 I and many people here have noticed that randomly VS
I have a 2D image randomly and sparsely scattered with pixels. given a point
I have 1 red polygon say and 50 randomly placed blue polygons - they
I have a list of objects and I want to reorder them randomly on
Have just started using Google Chrome , and noticed in parts of our site,
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you guys had any experiences (positive or negative) by placing your source code/solution
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a

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.