Peter
Peter

Reputation:

Why do I get this compile error in Java?

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?

Upvotes: 2

Views: 518

Answers (6)

Cuga
Cuga

Reputation: 17904

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).

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533660

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)

Upvotes: 0

MBCook
MBCook

Reputation: 14504

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);
}

Upvotes: 17

Milhous
Milhous

Reputation: 14653

your code should call

quicksort (tab,0,10);

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

Upvotes: 2

DaClown
DaClown

Reputation: 4569

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

Edit: second :(

Upvotes: 6

clamp
clamp

Reputation: 34036

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

Upvotes: 11

Related Questions