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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:41:02+00:00 2026-06-03T18:41:02+00:00

For a homework problem, I need to write a Java method to find the

  • 0

For a homework problem, I need to write a Java method to find the kth smallest number in an array, using quicksort-style partitioning. I was given the partition() method, and I’m supposed to write the method to get the kth smallest number.

The problem asks that the pivot always be the rightmost element in the array range.

I was given the following:

public int partition(int left, int right, long pivot) {
    int leftPtr = left - 1;
    int rightPtr = right + 1;
    while (true) {
        while (leftPtr < right && array[++leftPtr] < pivot); 
        while (rightPtr > left && array[--rightPtr] > pivot);

        if (leftPtr >= rightPtr)
            break;
        else
            swap(leftPtr, rightPtr);
    }
    return leftPtr;
}

And I’ve written this method based on Wikipedia’s pseudocode:

public int kthSmallest(int left, int right, int k){
    if(left >= right){
        return array[left];
    }

    int pivotNewIndex = partition(left, right, array[right]);

    int pivotDist = pivotNewIndex - left - 1;

    if(pivotDist == k){
        return array[pivotNewIndex];
    } else if(k < pivotNewIndex){
        return kthSmallest(k, left, pivotNewIndex - 1);
    } else{
        return kthSmallest(k - pivotDist, pivotNewIndex + 1, right);
    }
}

But when I call kthSmallest() with randomly generated arrays of integers, about half the time it returns the wrong value. For example:

array: [45, 60, 24, 82, 87, 79, 16, 32, 59, 83, 20, 2, 1, 
         50, 11, 79, 72, 32, 0, 48, 69, 74, 22, 6, 96]
expected result when k=4: 11
actual result when k=4: 87

What am I doing wrong?

  • 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-03T18:41:04+00:00Added an answer on June 3, 2026 at 6:41 pm

    Your recursive calls have the argument list in the wrong order. The position in the subarray that you’re looking at should be the third argument, not the first.

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

Sidebar

Related Questions

The problem: Write a method that takes in an array of salary values and
I have a problem: i need to find an average of the list using
I am having a problem with my array homework, I need to enter a
I have a homework question that says: Problem 1: Given the array [ 22
I have this homework problem where I need to use regex to remove every
I use latex to write up homework assignments. I need to include both charts
I am working on a school homework problem. I need to create 2 int[]
My homework has a problem for example there is a category array $cat=array('1','4','5','7'); now
Im doing a homework problem where I need to run the bellman-ford algorithm starting
This is not a homework problem. This questions was asked to one of my

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.