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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:39:01+00:00 2026-05-15T01:39:01+00:00

I have a divide and conquer method to find the i th smallest element

  • 0

I have a divide and conquer method to find the i th smallest element from an array. Here is the code:

public class rand_select{
    public static int Rand_partition(int a[], int p, int q, int i) {
        //smallest in a[p..q]
        if ( p==q) return a[p];
        int r=partition (a,p,q);
        int k=r-p+1;
        if (i==k) return a[r];
        if (i<k){
            return Rand_partition(a,p,r-1,i);
        }
        return Rand_partition(a,r-1,q,i-k);
    }

    public static void main(String[] args) {
        int a[]=new int []{6,10,13,15,8,3,2,12};
        System.out.println(Rand_partition(a,0,a.length-1,7));
    }

    public static  int partition(int a[],int p,int q) {
        int  m=a[0];
        while (p < q) {
            while (p < q && a[p++] < m) {
                p++;
            }
            while (q > p && a[q--] > m) {
                q--;
            }
            int t = a[p];
            a[p] = a[q];
            a[q] = t;
        }
        int k=0;
        for (int i=0; i < a.length; i++) {
            if ( a[i]==m){
                k=i;
            }
        }
        return k;
    }
}

However, I get an exception when run: java.lang.ArrayIndexOutOfBoundsException.

  • 1 1 Answer
  • 1 View
  • 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-05-15T01:39:01+00:00Added an answer on May 15, 2026 at 1:39 am

    I was able to fix a few bugs. A minor one is this line:

      return Rand_partition(a,r-1,q,i-k);
                               ^
    

    Instead, you want this:

      return Rand_partition(a,r+1,q,i-k);
                               ^
    

    That’s because you have partitioned a[p..q] into three parts as follows:

      a[p..r-1], a[r], a[r+1..q]
    

    Your original code handles the a[r] and a[p..r-1] case fine, but messes up on the a[r+1..q] by using r-1 instead.


    I was also able to correct and simplify partition:

    public static  int partition(int a[],int p,int q){
        int  m=a[p]; // not m[0], you want to partition m[p..q]!!!
        while ( p<q){
            while (p<q && a[p] <m){ // don't do p++ here!
                p++;
            }
            while (q>p && a[q]>m){ // don't do q-- here!
                q--;
            }
            int t=a[p];
            a[p]=a[q];
            a[q]=t;
        }
        return p; // no need to search!!!
    }
    

    Your original code had extraneous p++ and q--. Also, the search for where the pivot is is unnecessary. It’s where p and q meet.


    On naming conventions

    Please follow Java naming conventions:

    Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.

    Related questions

    • How is this statement making sense? (Sun’s naming convention for Java variables)
      • Unfortunately the naming convention document above has one glaring error

    On array declarations

    Also, do not make a habit of declaring arrays like this:

    int x[];
    

    You should instead put the brackets with the type, rather than with the identifier:

    int[] x;
    

    Related questions

    • Is there any difference between Object[] x and Object x[] ?
    • Difference between int[] myArray and int myArray[] in Java
    • in array declaration int[] k,i and int k[],i
      • These declarations result in different types for i!
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.