Jurgen
Jurgen

Reputation: 347

Java Quicksort help understand

I'm trying to understand the Quicksort algorithm in java.

I have commented my question below beside the part i'm trying to comprehend.

public class Quciksort {

  static void qsort( char items[]) {
    qs(items,0,items.length-1); /*how can this method reference its parameters before the method is defined (below)?*/
  }



  private static void qs(char items[], int left, int right) //
  {
    int i, j;
    char x, y;

    i=left; j=right;
    x=items[(left+right)/2];

    do{
        while((items[i] < x) && (i<right)) i++;
        while((x < items[j]) && (j>left)) j--;

        if(i<=j){
            y=items[i];
            items[i]=items[j];
            items[j]=y;
            i++;j--;
            }
    }while (i<=j);

    if(left<j) qs(items, left, j);
    if(i<right) qs(items, i, right);
  }

}

Upvotes: 0

Views: 108

Answers (1)

vefthym
vefthym

Reputation: 7462

You might be confusing Java with C, in which, if I remember well after 10+ years since last using it, it is a good practice, or maybe required, to declare a method before calling it.

In Java, there is no such requirement. Thus, you can call a method that is implemented at a later point in your code.

About the parameters, I am not sure I got your question correctly, but in your example, you are calling qs with the array items as the first argument (as it was given to the qsort method), with 0 for the value of left and the length of the items array -1, for right.

Upvotes: 1

Related Questions