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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:25:59+00:00 2026-06-02T06:25:59+00:00

So i had to make a quicksort algorithm using pivot as the middle element

  • 0

So i had to make a quicksort algorithm using pivot as the middle element of the array. I did that all fine. But now its asking me to modify the quickSort algorithm so that when any of the sublists reduces to less than 20, then i sort the sublist using an insertionSort.

I seemed to have got it working. It compiles and sorts the array perfectly, However I’m not sure if i did it right cuz the difference in CPU time between the modified quicksort and the normal quicksort arent that different. My uncertainty is in the recursive method recQuickSortC where i have the “>= 20” statements. I’m not sure if that’s the right way to implement the modifcation, it could be completely wrong, all i know is that it sorts it correctly. Any help would be nice, thanks.

Here’s my modified quickSort algorithm:

public void quickSortC(T[] list, int length)
{
    recQuickSortC(list, 0, length - 1);
}//end quickSort

private void recQuickSortC(T[] list, int first, int last)
{
  if (first < last)
  {
      int pivotLocation = partitionA(list, first, last);
      if ((pivotLocation - 1) >= 20)
          recQuickSortC(list, first, pivotLocation - 1);
      else
          insertionSort(list,pivotLocation -1);

      if ((pivotLocation - 1) >= 20)
          recQuickSortC(list, pivotLocation + 1, last);
      else
          insertionSort(list, pivotLocation + 1);
  }
}//end recQuickSort

private int partitionA(T[] list, int first, int last)
{
    T pivot;

    int smallIndex;

    swap(list, first, (first + last) / 2);

    pivot = list[first];
    smallIndex = first;

    for (int index = first + 1; index <= last; index++)
    {
        if (list[index].compareTo(pivot) < 0)
        {
            smallIndex++;
            swap(list, smallIndex, index);
        }
    }

    swap(list, first, smallIndex);

    return smallIndex;
}//end partition


    public void insertionSort(T[] list, int length)
{
    for (int unsortedIndex = 1; unsortedIndex < length;
                                unsortedIndex++)
    {
        Comparable<T> compElem =
                  (Comparable<T>) list[unsortedIndex];

        if (compElem.compareTo(list[unsortedIndex - 1]) < 0)
        {
            T temp = list[unsortedIndex];

            int location = unsortedIndex;

            do
            {
                list[location] = list[location - 1];
                location--;
            }
            while (location > 0 &&
                   temp.compareTo(list[location - 1]) < 0);

            list[location] = (T) temp;
        }
    }
}//end insertionSort

If you noticed theres a bunch of A’s,B’s, and C’s next to the methods becuase i have to do alot of different quicksort algorithms. I put in all the code that is used within the algorithm. Let me know if u need more of it thanks.

  • 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-02T06:26:03+00:00Added an answer on June 2, 2026 at 6:26 am

    This looks perfectly fine to me, although instead of testing whether the pivot distance is at most 20, I would just rewrite the quicksort method to say if (last - first <= 20) { do insertion sort} else { do normal quicksort}. That way you only have to write the check once, as opposed to once for each “side” of the recursion.

    That said, it’s likely that your benchmark isn’t actually giving you good time estimates — that is, your code is probably actually faster than you think it is — just because getting accurate benchmarks in Java is not trivial or obvious.

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

Sidebar

Related Questions

Had a problem with the recursive conflictCheck() method. That seems fine now. I have
I have had to make changes to a trigger and assumed that running an
I had more database and I tried to make backup for them but they
I recently had an idea to create my own String class to make using
Had a page that was working fine. Only change I made was to add
Had a good search here but can't see anything that gets my mind in
I had to make an Application that displays a form to search data from
I had to make a really small and simple script that would fill a
I have been using an open source Java project and had to make some
this question might sound silly to some but i just had to make sure.

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.