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 7683597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:49:49+00:00 2026-05-31T18:49:49+00:00

I’m working on an algorithm that manipulates pictures. Basically I will implement a diffusion

  • 0

I’m working on an algorithm that manipulates pictures.
Basically I will implement a diffusion (each pixel will get the median value of the 8 surrounding pixels + its own value).

what I will do is to create a array of 9 integers with the value, sort the array and get the median value at array[4].

I still don’t know what to use for the problem, what is the best sorting function to use for relatively small arrays? The sorting function will roughly be called x times, x being the number of pixels.

Heapsort seems a bit overkill. Quicksort will not perform that well. And I don’t want to implement really complex things.

What do you guys think?

  • 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-31T18:49:50+00:00Added an answer on May 31, 2026 at 6:49 pm

    If all you need is the median, there’s no need to do any sorting at all! (For long arrays, see http://en.wikipedia.org/wiki/Selection_algorithm for an O(n) algorithm; of course we’re talking only about short arrays here).

    For median of 9 numbers, a little googling reveals the article Fast median search: an ANSI C implementation by N. Devillard, which points to the article Implementing median filters in XC4000E FPGAs by J. L. Smith, which provides this self-explanatory “sorting network” to get the median using 19 comparisons:

    enter image description here

    In terms of C:

    typedef int T;
    
    void sort2(T* a, T* b);
    void sort3(T* a, T* b, T* c);
    T min3(T a, T b, T c);
    T max3(T a, T b, T c);
    
    T median9(T p1, T p2, T p3, T p4, T p5, T p6, T p7, T p8, T p9)
    {
        sort3(&p1, &p2, &p3);
        sort3(&p4, &p5, &p6);
        sort3(&p7, &p8, &p9);
    
        p7 = max3(p1, p4, p7);
        p3 = min3(p3, p6, p9);
    
        sort3(&p2, &p5, &p8);
        sort3(&p3, &p5, &p7);
    
        return p5;
    }
    
    void sort2(T* a, T* b)
    {
        if (*a > *b)
        {
            T tmp = *b;
            *b = *a;
            *a = tmp;
        }
    }
    
    void sort3(T* a, T* b, T* c)
    {
        sort2(b, c);
        sort2(a, b);
        sort2(b, c);
    }
    
    T min3(T a, T b, T c)
    {
        if (a < b)
            return a < c ? a : c;
        else
            return b < c ? b : c;
    }
    
    T max3(T a, T b, T c)
    {
        if (a > b)
            return a > c ? a : c;
        else
            return b > c ? b : c;
    }
    

    Edit: this file also contains the code for getting the median of 3, 5, 6, 7, 9 and 25 numbers.

    #define PIX_SORT(a,b) { if ((a)>(b)) PIX_SWAP((a),(b)); }
    #define PIX_SWAP(a,b) { pixelvalue temp=(a);(a)=(b);(b)=temp; }
    
    /*----------------------------------------------------------------------------
       Function :   opt_med9()
       In       :   pointer to an array of 9 pixelvalues
       Out      :   a pixelvalue
       Job      :   optimized search of the median of 9 pixelvalues
       Notice   :   in theory, cannot go faster without assumptions on the
                    signal.
                    Formula from:
                    XILINX XCELL magazine, vol. 23 by John L. Smith
    
                    The input array is modified in the process
                    The result array is guaranteed to contain the median
                    value
                    in middle position, but other elements are NOT sorted.
     ---------------------------------------------------------------------------*/
    
    pixelvalue opt_med9(pixelvalue * p)
    {
        PIX_SORT(p[1], p[2]) ; PIX_SORT(p[4], p[5]) ; PIX_SORT(p[7], p[8]) ;
        PIX_SORT(p[0], p[1]) ; PIX_SORT(p[3], p[4]) ; PIX_SORT(p[6], p[7]) ;
        PIX_SORT(p[1], p[2]) ; PIX_SORT(p[4], p[5]) ; PIX_SORT(p[7], p[8]) ;
        PIX_SORT(p[0], p[3]) ; PIX_SORT(p[5], p[8]) ; PIX_SORT(p[4], p[7]) ;
        PIX_SORT(p[3], p[6]) ; PIX_SORT(p[1], p[4]) ; PIX_SORT(p[2], p[5]) ;
        PIX_SORT(p[4], p[7]) ; PIX_SORT(p[4], p[2]) ; PIX_SORT(p[6], p[4]) ;
        PIX_SORT(p[4], p[2]) ; return(p[4]) ;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping 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.