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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:01:18+00:00 2026-06-14T04:01:18+00:00

The purpose of this program is to find the kth smallest element in an

  • 0

The purpose of this program is to find the kth smallest element in an array without sorting the array using a recursive and nonrecursive decrease and conquer type method.

I was hoping someone could look over my code and try to help me with my array out of bounds error(s).

The method that is throwing these errors is the recursive selection the non recursive selection works fine.

My driver is also attached and everything should compile if you want to test my code.

public class KthSmallest
{
private int counter;
private int term;
private int[] A;

int SelectionNonRecursive(int A[], int kthSmallest, int sizeOfA)
{   
    this.A = A;
    if(kthSmallest == 1 || kthSmallest == sizeOfA)
    {
        return (LinearSearch(kthSmallest, sizeOfA));
    }
    else
    {
        for(int i = 0; i<sizeOfA; i++)
        {
            counter = 0;
            for(int j = 0; j<sizeOfA; j++)
            {
                if(A[i] < A[j])
                {
                    counter++;
                }
            }
            if((sizeOfA - counter) == kthSmallest)
            {
                return A[i];
            }
        }
    }

    return 0;
}

int SelectionRecursive(int A[], int kthSmallest, int sizeOfA)
{
    this.A = A;
    return Selection_R(0, sizeOfA - 1, kthSmallest);
}

int Selection_R(int l, int r, int kthSmallest)
{
    if(l<r)
    {
    if(kthSmallest == 1 || kthSmallest == A.length)
    {
        return (LinearSearch(kthSmallest, A.length));
    }
    else
    {
        int s = LomutoPartition(l, r);
        if(s == kthSmallest - 1)
        {
            return A[s];
        }
        else if(s > (A[0] + kthSmallest - 1))
        {
            Selection_R(l, s-1, kthSmallest);
        }
        else
        {
            Selection_R(s+1, r, kthSmallest); 
        }
    }
    }
    return 0;
}

int LomutoPartition(int l, int r)
{
    int pivot = A[l];
    int s = l;
    for(int i = l+1; i<r; i++)
    {
        if(A[i] < pivot)
        {
            s += 1;
            swap(A[s], A[i]);
        }
    }
    swap(A[l], A[s]);
    return s;
}

public void swap(int i, int j)
{
    int holder = A[i];
    A[i] = A[j];
    A[j] = holder;
}

int LinearSearch(int kthSmallest, int sizeOfA)
{
    term = A[0];
    for(int i=1; i<sizeOfA; i++)
    {
        if(kthSmallest == 1)
        {
            if(term > A[i])
            {
                term = A[i];
            }
        }
        else
        {
            if(term < A[i])
            {
                term = A[i];
            }
        }
    }
    return term;
}
}

public class KthDriver
{
public static void main(String[] args)
{
    KthSmallest k1 = new KthSmallest();
    int[] array = {7,1,5,9,3};
    System.out.print(k1.SelectionRecursive(array, 3, array.length));


}
}
  • 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-14T04:01:19+00:00Added an answer on June 14, 2026 at 4:01 am

    Inside your LomutoPartition method, you are passing the array elements in your swap method: –

    swap(A[s], A[i]);  // Inside for loop
    

    and

    swap(A[l], A[s]);  // Outside for loop
    

    And your swap method considers them as indices: –

    public void swap(int i, int j)  <-- // `i` and `j` are elements A[s] and A[i]
    {
        int holder = A[i];  <-- // You are accessing them as indices(A[i] -> A[A[s]])
        A[i] = A[j];
        A[j] = holder;
    }
    

    That is why you are getting that exception. Because, if any element in array is greater than size, it will blast out.

    You should change your invocation to: –

    swap(s, i);   // Inside for loop
    

    and

    swap(l, s);   // Outside for loop
    

    respectively. And leave your method as it is.

    Note that, you should pass array indices, and not array elements. If you pass array elements, then the swapping in the method will not be reflected in your array. Because, your method will have its own copy of your elements.

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

Sidebar

Related Questions

The purpose of my program is to find the prime indicated by user input.
My program that I am writing's purpose arose with this issue: There are two
I am writing a simple keylogger program (for non-malicious purposes). Note: This is with
[purpose] This simple command sequence runs expected in the Windows' CMD shell: dir &
What is the purpose this and why this be added when we add new
The purpose of this code is to pull upgrade.zip from a central server, extract
EDIT: Purpose of this Website: Its called Utopiapimp.com. It is a third party utility
My whole purpose of this is to get something looking like this: I want
What kind of tools or techniques exist for this purpose?
What kind of tools and techniques exist for this purpose?

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.