1

I am trying to run a JUnit test for my program, however I am getting error message like

*incompatible types required: int[]; found: int * 

Here is the code that shows the error

myQSArray = QS.quickSort(sortedArray2,0, sortedArray2.length - 1); 

and here is my call for quickSort method

public static int quickSort( int A[], int p, int r){ int q; if (p<r) { q = partition(A,p,r); quickSort(A, p, q-1); quickSort(A,q+1,r); } return QS1.quickSort(A, p, r); } 

Help Please, thanks in advance

6
  • Please show the method signature for the partition method - and the declaration of sortedArray2 from the method call where the error occurs - plus the declaration of msQSArray (THAT should be int) Commented Mar 9, 2012 at 7:20
  • And please double check the class name, is it QS or QS1 or are you really using to different classes that implement static quicksort methods. Commented Mar 9, 2012 at 7:24
  • @Andreas_D: public static int partition(int A[],int p, int r){ Commented Mar 9, 2012 at 7:27
  • @Andreas_D: Here are the Declaration for sortedArray2 and msQSArray Commented Mar 9, 2012 at 7:29
  • int[] sortedArray1 = new int[SIZE]; int[] sortedArray2 = new int[SIZE]; int[] myQSArray = new int[SIZE]; Commented Mar 9, 2012 at 7:29

2 Answers 2

1

I see a couple of problems with your code:

The method is declared to return an int value but you try to assign this int to an int[]. That causes the actual compiletime error. Change the method signature to

public static int[] quickSort( int A[], int p, int r) 

for a quick fix.

Then, your recursive function quicksort misses the exit criteria. It will run indefinitely (or at least until the virtual machine gives up and throws a StackOverflowException after a couple of milliseconds). You need to add a criteria to check, if the array is sorted and return the sorted array (see the "new" method signature!).

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

2 Comments

Thank you @Andreas, that definitely helped. Now when I run the JUnit Test, I got 0% pass, so basically, everything failed, so is that because of the "exit criteria", and what is this "new" method signature again?
ah sorry, the "new" part of the signature is that the method returns int[] now instead of int. We want to get the sorted array rather then some single integer value.
0

Java Error incompatible types occurred when a compiler found a variable and expression whose data type is not compatible to perform an operation on them.And this is the one of the way.

better once refer you declared the "sortedArray2" as int[] array type or not

1 Comment

Sorry @mohan, but I didn't get what you mean. Do you want me to check the "sortedArray2" declaring statement or is that a suggestion? Please clearify.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.