views:

310

answers:

6

Hello all,

I get the following error:

quicksort(int[],int,int)cannot be applied to(int[])

When I compile this:

import java.util.*;

public class Sort {

public static void main(String[] args){

Random rand = new Random();
int[] tab = new int[10];

for(int i = 0; i < tab.length; i++) {
tab[i] = rand.nextInt(100);

System.out.println("Before: ");
show(tab);

quicksort (tab);
System.out.println("After: ");
show(tab);
  }
}
static void quicksort(int tab[], int x, int y) {

     int i,j,v,temp;

     i=x;
     j=y;
     v=tab[(x+y) / 2];
     do {
      while (tab[i]<v) 
       i++;
      while (v<tab[j]) 
       j--;
      if (i<=j) {
       temp=tab[i];
       tab[i]=tab[j];
       tab[j]=temp;
       i++;
       j--;
      }
     }
     while (i<=j);
     if (x<j) 
      quicksort(tab,x,j);
     if (i<y) 
      quicksort(tab,i,y);
    }


static void show (int tab[]) {
for (int i = 0; i <tab.length; i++) {
System.out.println(tab[i]);

  }
 }
}

What am I doing wrong?

+10  A: 

the function "quicksort" that you define asks for 3 parameters, but you are only providing one.

clamp
+5  A: 

Because your quicksort function has 3 parameters, but your call gives only one.

Edit: second :(

DaClown
+2  A: 

your code should call

quicksort (tab,0,10);

In your outer calll, so you can sort the list.

Milhous
+16  A: 

Just after the line to print out "before", you have:

quicksort (tab);

The function you designed needs three arguments. You can either add the extra arguments:

quicksort (tab, 0, tab.length - 1)

Or add a new function such as:

public quicksort(int[]) {
  quicksort(tab, 0, tab.length - 1);
}
MBCook
A: 

BTW: You could just use Arrays.sort() which is a built in function. You wouldn't write a function like this in real life. (only as homework)

Peter Lawrey
+1  A: 

Without knowing what you're writing code in, I strongly recommend using an IDE if you haven't adopted one already. Particularly Eclipse for Java.

Eclipse would underline the offending line of code and make some suggestions to you, (in addition to offering code completion). A text editor, like JEdit does not.

Note: I've been told IntelliJ is good, but you can't beat Eclipse's price (free).

Cuga