0

I mainly do python because its in my school syllabus, but I enjoy java(I am still fairly new to it). I was trying to make a quick sort program(recursive) in java but a typical stackoverflow occurs.I've made recursove program in java but this time i just don't know what is wrong

This is the function for setting pivot:

static void part(int[]a){
    int n = a.length;
    j=-1;
    for(int i=0;i<n;i++){
        if(a[n-1]<a[i]){continue;}
    else if(a[n-1]>=a[i]){
        j++;
        if(a[j]>a[i]){
        int t=a[j];
        a[j]=a[i];
        a[i]=t;
        }
    }
}

This is the function that sorts:

static int[] sort(int[]a){
    if(a.length<=1){
        return(a);
        }
    part(a);
    int temp=j; //Variable 'j' is public
    sort(slice(a,0,temp));
    sort(slice(a,temp+1,a.length+1));
    return(a);
}

This is the slice function

static int[] slice(int []a, int b, int c){
     int[]temp=new int[c-b+2];
     int ind=0;
     for(int i=b;i<c;i++,ind++){
         temp[ind]=a[i];
        }
      return(temp);
}

I've tried changing the value for bounds but always the error occurs.

6
  • slice is another that does what oython slicing does. static int[] slice(int []a, int b, int c){ ` int[]temp=new int[c-b+2];` int ind=0; `for(int i=b;i<c;i++,ind++){ temp[ind]=a[i] Commented Apr 25, 2024 at 18:12
  • no idea what oython slicing is (and to tired to search) - but as posted in previous comment - should be in question - it is doing nothing, or better, we cannot see what it is returning (looks like Arrays.copyOfRange()) Commented Apr 25, 2024 at 18:50
  • Presumably oython is a typo for python. Why on earth did you make j an instance variable instead of just having part return the pivot? Commented Apr 25, 2024 at 19:41
  • 1
    Have you made any attempt to debug and see how the pivot and the slices evolve over the course of the recursive calls? See How to Debug Small Programs. Commented Apr 25, 2024 at 19:43
  • It looks like slice creates a copy of the array. But sort returns the original array. If you're recursively sorting copies of the original array, how do you expect to sort the original array? Commented Apr 26, 2024 at 8:35

1 Answer 1

0

I think I found the error reason.

In case, int a[] = {2, 5, 1}. I think your expected output is {5, 2, 1}

But your part() function modifies variable j and in this case, it's always 0. Because part() function is comparing all elements of array with the last one element. Then, 2 and 5 is bigger than 1, so, the first if is never executed.

The else if is only executed with a[2] which is a[n-1], and the variable j is increased, so it's 0.

I used Arrays.copyOfRange() function for slice(). So the first recursive call of sort() function is sort({}) and doesn't do anything.

The second one, does slice(a, 0+1, 3+1), so it's sort({5, 1, 0}). You should change a.length+1 to a.length.

Sign up to request clarification or add additional context in comments.

7 Comments

In case of int a[]={2,5,1} setting the last variable(1 in this case) as pivot, the expected output is {1,5,2}, so the elements to the right of pivot are greater and to the left are the smaller(none in this case) so fiirst recursive call will return 1
As far as I am aware, "U" and "ur" are not words in the English language and may confuse readers of your answer whose command of English is poor. Further, translation software may not know how to correctly translate "U" and "ur".
@Abra confuse? No. We've all been on the internet long enough. Irritate? Yeah a little.
@Gimby looks like ur right (and I'm wrong :-)
@Arnavjoharwal Ok, I see you uploaded the slice() function. But it's kind weird. The part() of the first sort() does ` {1, 5, 2}, and j=0` but the execution of program got stuck in slice(), because slice({1, 5, 2}, 0, 0) returns {0, 0}. And In the second part() returns j=1, because the two iteration executes the else if, so j is increased twice. And slice({0, 0}, 0, 1) returns an array of size 1-0+2 = 3, which is {0,0,0}. So the second recursive call of sort() is never executed.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.