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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:08:00+00:00 2026-06-11T12:08:00+00:00

I am trying to sort 100k elements present in a file using quicksort.My algorithm

  • 0

I am trying to sort 100k elements present in a file using quicksort.My algorithm works only if I use first element as pivot.How can I make pivot element generic to the program?What is the best way to choose a pivot element using quicksort?Is pivot element dependent on the data or do we use any specific algorithms in real time?

void quicksort(int *temp,int p,int r)
{
    if(r > p + 1)
    {
        int piv = temp[p];
        int left = p + 1;
        int right = r;
        while(left < right)
        {
            if(temp[left] <= piv)
                left++;
            else
                swap(&temp[left], &temp[--right]);
        }
        swap(&temp[--left], &temp[p]);
        quicksort(temp, p, left);
        quicksort(temp, right, 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-11T12:08:01+00:00Added an answer on June 11, 2026 at 12:08 pm

    The constraint on your pivot selection is that it needs to be fairly fast. Common approaches are choosing the first element, choosing the middle element, or choosing a random element.

    Using a random element is probably best, because it will still tend to have fast sorts even for lists that are already ordered (using the first element will perform very poorly on sorted lists because all of the following elements will fall on one side of the pivot).

    Using the middle element is also fairly bulletproof, unless your list is in some strange nonstandard order such like you would find by concatenating two lists sorted in opposite orders.

    I recommend a random selection.

    My algorithm works only if I use first element as pivot

    When you say this, do you mean it produces a mis-sorted list if your pivot is not the first element? Can you go into greater detail?

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

Sidebar

Related Questions

I'm trying to sort these elements using the jQuery Isotope plugin. I can't get
I am trying to sort a Vector list using the Java Collections frameworks. This
I am trying to sort an array of integers in MIPS using bubble sort
I am trying to sort a list of records that have been created using
Trying to sort an array by writing my own sort method using recursion (Pine's
I'm setting up a new store using 1.4.1 I'm trying sort the products in
I am trying to sort an array using multithreading in java and note its
I am trying to sort char array like this: #include<algorithm> ... char x[] =
Im trying to sort all elements, then attributes, which ive got working, however i
I trying to sort some data to use sortedArrayUsingDescriptors: method in an NSArray class.

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.