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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:30:30+00:00 2026-06-14T16:30:30+00:00

I have two different implementations of quicksort below. I have verified that oth of

  • 0

I have two different implementations of quicksort below. I have verified that oth of these versions of quicksort work in the sense that they will sort any array I give it correctly. If you notice (at least it appears to me), Version #2 is exactly the same as Version #1 when the array size n is greater than 8. Therefore, I would expect that when I give both of these functions an array of the same size that is greater than 8, they should make around the same number of component wise comparisons on average, but they do not.

For n > 8, both functions use sort3() and partition() functions. I have listed those below as well to show you how I count the number of component wise comparisons.

I know that W(n), the theoretical worst case number of comparisons for these implementations of quicksort is (n(n+2)/4)+8. Therefore, for an array size n = 500, W(n) = 62758. For a test run on an array of size n = 500, Version #1 makes about 5000 comparisons on average which is reasonable. However, Version #2 is making 80000 comparisons on average. Obviously this can’t be right – Version #2 is making more comparisons than the theoretical W(n) and it is exactly (at least appears to me) the same algorithm as Version #1.

Do you see an error that I am making in Version #2?

Version #1:

void Quicksort_M3(int S[], int low, int hi)
{
    if(low < hi)
    {
        if((low+1) == hi)
        {
            comparisons++;
            if(S[low] > S[hi])
                swap(S[low],S[hi]);
        }
        else
        {
            Sort3(S,low,hi);
            if((low+2)<hi)
            {
                swap(S[low+1],S[(low+hi)/2]);
                int q = partition(S, low+1, hi-1);
                Quicksort_M3(S, low, q-1);
                Quicksort_M3(S, q+1, hi);
            }
        }
    }
}

Version #2:

void Quicksort_Insert_M3(int S[], int n, int low, int hi)
{
    if((hi-low)<=8)
        Insertionsort(S,n);
    else 
    {
        if(low < hi)
        {
            if((low+1) == hi)
            {
                comparisons++;
                if(S[low] > S[hi])
                    swap(S[low],S[hi]);
            }
            else
            {
                Sort3(S,low,hi);
                if((low+2)<hi)
                {
                    swap(S[low+1],S[(low+hi)/2]);
                    int q = partition(S, low+1, hi-1);
                    Quicksort_Insert_M3(S, n, low, q-1);
                    Quicksort_Insert_M3(S, n, q+1, hi);
                }
            }
        }
    }
}

Partition:

int partition(int *S,int l, int u)
{
    int x = S[l];
    int j = l;
    for(int i=l+1; i<=u; i++)
    {
        comparisons++;
        if(S[i] < x)
        {   
            j++;
            swap(S[i],S[j]);
        }

    }
    int p = j;
    swap(S[l],S[p]);
    return p;
}

Sort3:

int Sort3(int list[], int p, int r)
{
    int median = (p + r) / 2;
    comparisons++;
    if(list[p] <= list[median])
    {
        comparisons++;
        if(list[median]>list[r])
        {
            comparisons++;
            if(list[p]<list[r])
            {
                int temp = list[p];
                list[p] = list[r];
                list[r] = list[median];
                list[median] = temp;
            }
            else
            {
                exchange(list,median,r);
            }
        }
        else
            ;

    }
    else
    {
        comparisons++;
        if(list[p] > list[r])
        {
            comparisons++;
            if(list[median] < list[r])
            {
                int temp = list[p];
                list[p] = list[median];
                list[median] = list[r];
                list[r] = temp;
            }
            else
            {
                exchange(list,p,r);
            }
        }
        else
        {
            exchange(list,p,median);
        }

    }


    return list[r];
}
  • 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-14T16:30:31+00:00Added an answer on June 14, 2026 at 4:30 pm

    I think your error is that when you do the insertion sort you are still using the original size of the array. Therefore you end up doing an insertion sort on the whole array.

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

Sidebar

Related Questions

Imagine that we have two static libraries built with different implementations of std::vector .
I have two classes with identical implementations but different names. They are generated and
I have two total different implementations of AsyncTask , lets say AT1 and AT2
I've recently discovered that MVC is supposed to have two different flavors, model one
Sometimes I wonder that we do have interfaces and abstract classes for two different
I have tried two different implementations for simulating POSTing a form. One uses fsockopen
I have several different implementations of a trait that I would like to test,
We have two different implementations of the same interface. Think of it as a
I've wrote two different implementations of heapsort today and both have given me the
I have two different libGL libraries on the same Ubuntu 11.04 machine. One library

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.