user2980566
user2980566

Reputation: 289

Having trouble with this method, compilation error

I'm getting an error in line 21 "cannot find symbol symbol method quickSort(int[], int, int), and I'm not sure why it's happening.

import java.util.Random;

  public class timeQuickSort {

  public static void main(String[] args) {
      int size = 16;
      int max = 10;
      int[] array = new int[size];

      Random random = new Random();

      random.nextInt(max);

      for (int i = 0; i < size; i++) {
          array[i] = random.nextInt(max);
      }  

      long result;

      long startTime = System.currentTimeMillis();
      quickSort(array,  100,  array.length-1);

      long endTime = System.currentTimeMillis();
      result = endTime-startTime; 

      System.out.println("The quick sort runtime is " + result + " miliseconds");
  }
}

This is my quickSort method:

  public static void quickSort(int[] a, int p, int r)
  {
    if (p < r)
    {
      int q = partition(a,p,r);
      quickSort(a,p,q);
      quickSort(a,q+1,r);
    }
  }

Upvotes: 1

Views: 84

Answers (2)

Akhilesh Dhar Dubey
Akhilesh Dhar Dubey

Reputation: 2148

Specify class name to call static method if it resides in other class than current class. like:

class Sort{
  public static void quickSort(int[] a, int p, int r)
  {
    if (p < r)
    {
    int q = partition(a,p,r);
    quickSort(a,p,q);
    quickSort(a,q+1,r);
    }
  }
}

and call it in your class like this:

    Sort.quickSort(array,  100,  array.length-1);

Upvotes: 2

PNS
PNS

Reputation: 19895

If the method is declared in another class, then you need to invoke it by explicitly using the other classes name.

So, if the other class is called, say, Sorter, you just need to call

Sorter.quicksort(a, p, q);
Sorter.quicksort(a, q+1, r);

Upvotes: 1

Related Questions