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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:09:48+00:00 2026-06-19T03:09:48+00:00

I am trying to implement the RandomizedQuickSort algorithm. I think that I did the

  • 0

I am trying to implement the RandomizedQuickSort algorithm. I think that I did the randomizedPartition method correctly but I do not know what is wrong with the sort method?!

Here is my try:

public class RandomizedQuickSort {
    public static void sort(int[] A, int start, int end) {
        if(start < end) {
            int pivot = randomizedPartition(A, start, end);

            sort(A, start, pivot - 1);
            sort(A, pivot + 1, end);
        }
    }

    private static int randomizedPartition(int[] A, int start, int end) {
        int pivot = A[start];
        int pivotIndex = start;

        for(int i = start + 1; i < end; i++) {
            if(A[i] <= pivot) {
                pivotIndex += 1;
                swap(A, pivotIndex, i);
            }
        }
        swap(A, start, pivotIndex);

        return pivotIndex;
    }

    private static void swap(int[] A, int x, int y) {
        int temp = A[x];

        A[x] = A[y];
        A[y] = temp;
    }

    public static void main(String[] args) {
        int A[] = {11, 0, 2, 8, 15, 12, 20, 17, 5, 11, 1, 16, 23, 10, 21, 7, 22, 9};

        sort(A, 0, A.length);

        for(int i = 0; i < A.length; i++) {
            System.out.print(A[i] + ", ");
        }
        System.out.println();
    }
}

Here is the output I got:

0, 1, 2, 8, 5, 9, 10, 11, 7, 11, 12, 16, 17, 15, 20, 21, 22, 23
  • 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-19T03:09:49+00:00Added an answer on June 19, 2026 at 3:09 am

    Well, besides the fact that you are not choosing a pivot randomly, you have an off-by one error when you sort the sub-arrays. Note that your first sort call is sort(A, 0, A.length), so the end index is A.length - 1. Hence the first sub-array should go up to pivot, not pivot - 1. It’s fixed as follows:

    public static void sort(int[] A, int start, int end) {
        if(start < end) {
            int pivot = randomizedPartition(A, start, end);
    
            sort(A, start, pivot); // This line needed to be changed!
            sort(A, pivot + 1, end);
        }
    }
    

    Output:

    0, 1, 2, 5, 7, 8, 9, 10, 11, 11, 12, 15, 16, 17, 20, 21, 22, 23, 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

trying to implement the following but its not working <script> function loadmap(lat,lng) { //alert(lat,lang);
I'm trying implement my project in Apache Struts 2 but I'm not very familiar
Trying to implement sticky footer but its not working as planned. It throws it
Trying to implement something similar to qtip, but using a table that compares the
Trying to implement LoaderManager + CursorLoader. In onFinish method adapter should swap its cursor
Trying to implement 3-layer (not: tier, I just want to separate my project logically,
I'm trying implement server code of Server-Sent Events in a generic way that any
Trying to implement AVAudioplayer and get some metering data of the played music, but
Trying to implement some nested loops that are spitting out good old nested html
I trying to implement synchronization algorithm in C#, without success. Why isn't the following

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.