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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:56:18+00:00 2026-06-13T00:56:18+00:00

I’m trying to count the number of comparisons my quicksort algorithm makes for an

  • 0

I’m trying to count the number of comparisons my quicksort algorithm makes for an array size of 500. I know that the best case for quicksort with partition is nlogn-n+1. So for an array size of 500, the best case number of component wise comparisons would be about 3983. However, when I run my code, I’m getting 2400 comparisons or so, depending on the array the random function generates. Am I counting the number of component wise comparisons wrong? Please help.

#include <iostream>
    #include <string>
    #include <stdlib.h>
    using namespace std;


    int count_500 = 0;

    int partition(int *S,int l, int u);
    void swap(int &val1, int &val2);
    void Quicksort(int S[],int low, int hi);
    void exchange(int list[], int p, int q);
    int median_of_3(int list[], int p, int r);
    void Quicksort_M3(int S[], int low, int hi);





    int main()
    {
            int S1_500[500];
            int S2_500[500];
            int S3_500[500];

            int S1_200[200];
            int S2_200[200];
            int S3_200[200];

            int S1_8[8];
            int S2_8[8];
            int S3_8[8];

            srand ( time(NULL) );



            for(int i=0; i<500; i++)
            {
                    S1_500[i] = rand()%1000;
                    S2_500[i] = rand()%1000;
                    S3_500[i] = rand()%1000;
            }

            for(int i=0; i<200; i++)
            {
                    S1_200[i] = rand()%500;
                    S2_200[i] = rand()%500;
                    S3_200[i] = rand()%500;
            }

            for(int i=0; i<8; i++)
            {
                    S1_8[i] = rand()%100;
                    S2_8[i] = rand()%100;
                    S3_8[i] = rand()%100;
            }

            Quicksort(S1_500,0,499);
            for(int i=0; i<500; i++)
            {
                    cout << S1_500[i] << endl;
            }

            cout << "Number of component wise comparisons is: " << count_500 << endl;





    }


    int partition(int *S,int l, int u)
    {
            int x = S[l];
            int j = l;
            for(int i=l+1; i<=u; i++)
            {
                    if(S[i] < x)
                    {       
                            count_500++; // Count the component wise comparison
                            j++;
                            swap(S[i],S[j]);
                    }
            }
            int p = j;
            swap(S[l],S[p]);
            return p;
    }

    void swap(int &val1, int &val2)
    {
            int temp = val1;
            val1 = val2;
            val2 = temp;
    }

    void Quicksort(int S[],int low, int hi)
    {
            if (low < hi)
            {
                    int p = partition(S,low,hi);
                    Quicksort(S,low,p-1);
                    Quicksort(S,p+1,hi);
            }
    }
  • 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-13T00:56:19+00:00Added an answer on June 13, 2026 at 12:56 am

    You want the count_500++; outside the if statement. You’re only counting the comparisons, where the result is true.

    Change

                    if(S[i] < x) 
                    {        
                            count_500++; // Count the component wise comparison 
                    ...
                    }
    

    to

                    count_500++; // Count the component wise comparison 
                    if(S[i] < x) 
                    {        
                    ...
                    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
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 want to count how many characters a certain string has in PHP, but

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.