Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Sorting

Posted on 2003-12-01
12
Medium Priority
?
542 Views
Last Modified: 2008-02-01
Hello,

I'm trying to test the insertionSort, quickSort, and mergeSort but I'm getting compilation errors on parts of my program.  Below are the two methods (I think...) that are giving the errors:
-----------------------------------------------------------

private static void choosePivot(Comparable[] theArray, int first, int last) {

      int pivotIndex;
      int mid = (first + last)/2;
            if (theArray[first] <= theArray[mid]) {
                  if (theArray[mid] <= theArray[last]) {
                       pivotIndex = mid;
                  }
                  else {
                       pivotIndex = (theArray[first] <= theArray[last] ? last:first);
                  }
            }
            else if(theArray[last] <= theArray[mid]) {
                 pivotIndex = mid;
            }
            else {
                 pivotIndex = (theArray[first] <= theArray[last] ? first:last);
                 swap(theArray[first], theArray[pivotIndex]);
            }

}  // end choosePivot

//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//

 public void swap(int left, int right) {

         Comparable temp = theArray[left];
         theArray[left] = theArray[right];
         theArray[right] = temp;

 } // end swap()

-----------------------------------------------------

Can someone also explain what 'Comparable' is and what it does... because I get the following compilation errors:

operator <= cannot be applied to java.lang.Comparable,java.lang.Comparable  
                        - and -
swap(int,int) cannot be applied to (java.lang.Comparable,java.lang.Comparable)

----------------------------------------------------

Also, the compiler's complaing that it can't find 'theArray' in my swap method:

cannot resolve symbol: Comparable temp = theArray[left];
                                                                     ^

----------------------------------------------------

If you need the rest of my program, please let me know.  Thanks :)
0
Comment
Question by:luna621
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 92

Expert Comment

by:objects
ID: 9856193
Comparable is an interface that defines how to compare objects.
It includes one method:
int compareTo(Object o)
See the javadoc for more details.

in your case to use it, instead of:
 if (theArray[first] <= theArray[mid]) {
you would do:
 if (theArray[first].compareTo(theArray[mid])<=0) {
0
 
LVL 92

Expert Comment

by:objects
ID: 9856202
> cannot resolve symbol: Comparable temp = theArray[left];

You need to pass theArray to your swap() method.

public void swap(int[] theArray, int left, int right) {
0
 

Author Comment

by:luna621
ID: 9856767
Yay!  It worked for the swap method, but how do I ".compareTo" for these?  Someone told me to write the ? : form, but I don't understand what that is... can you please explain?  
------------------------------------------------------------------------------------
            else {
            pivotIndex = (theArray[first] <= theArray[last] ? last:first);
            }
      }
      else if(theArray[last].compareTo(theArray[mid])<=0) {
            pivotIndex = mid;
      }
      else {
            pivotIndex = (theArray[first] <= theArray[last] ? first:last);
            swap(theArray[first], theArray[pivotIndex]);
      }

-----------------------------------------------------------------------------------

I also get this error:

swap(java.lang.Comparable[],int,int) cannot be applied to (java.lang.Comparable,java.lang.Comparable)

swap(theArray[first], theArray[pivotIndex]);

What does that mean?  Thank you!
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 92

Expert Comment

by:objects
ID: 9856815
> pivotIndex = (theArray[first] <= theArray[last] ? last:first);

pivotIndex = (theArray[first].compareTo(theArray[last])<=0 ? last : first);

> swap(java.lang.Comparable[],int,int) cannot be applied to (java.lang.Comparable,java.lang.Comparable)

you need to pass the array as you have now added it as a param
you also should be passing the index not the array value

swap(theArray, first pivotIndex);
0
 

Author Comment

by:luna621
ID: 9864738
Yay!  It worked!  Okay, but now I'm testing the program.  Is there a way I can have random integers and Strings sorted through my program?  Is there a way to time how long it takes?

Here's my main method so far...
---------------------------------------

      public static void main(String args[]){

            long start = System.currentTimeMillis();
            Date startTime = new Date(start);

            System.out.println(startTime.toString());

//-------------------------------------------
// start testing... need to input random
// numbers and integers here.
//-------------------------------------------



            long stop = System.currentTimeMillis();
            Date stopTime = new Date(stop);

            System.out.println(stopTime.toString());


//-------------------------------------------
// find out how much time spent...
//-------------------------------------------

            Date totalTime = (stopTime - startTime);

      } // end main()

-------------------------------------------------

I seem to have a problem with this line:  Date totalTime = (stopTime - startTime);

It's giving me this error:  operator - cannot be applied to java.util.Date,java.util.Date

-------------------------------------------------

I believe this is the compacted version of something...

pivotIndex = (theArray[first] <= theArray[last] ? last:first);

Is there another way to write the above (without the question mark, etc.)?

-------------------------------------------------

Sorry for asking so many questions.  This is like a 3-in-1 question!  To be fair, I increased the points :)

-luna621 =^_^=

0
 
LVL 92

Accepted Solution

by:
objects earned 1400 total points
ID: 9864764
> Date totalTime = (stopTime - startTime);

startTime and stopTime are longs, it should be:

long totalTime = (stopTime - startTime);

> pivotIndex = (theArray[first] <= theArray[last] ? last:first);
> Is there another way to write the above (without the question mark, etc.)?

Its called a ternary operator: <condition> ? <value> : <value>
It is equivalent to:

if (theArray[first] <= theArray[last])
{
   pivotIndex = last;
}
else
{
   pivotIndex = first;
}
0
 

Author Comment

by:luna621
ID: 9871890
Is there a way for me to have RANDOM integers read into the different sort methods?  Can I do the same for Strings?  Do I need to import anything (i.e. import java.util.*;)?
0
 
LVL 92

Expert Comment

by:objects
ID: 9871932
java.util.Random can be used for generating Random numbers.
0
 

Author Comment

by:luna621
ID: 9872031
I want to do something like this in my main to compare the execution times of each sorting method:

- Timings of insertionSort, quickSort, & mergeSort -

 Number of (random) elements: #####

insertionSort execution time in millisecs: #####
     quickSort execution time in millisecs: #####
   mergeSort execution time in millisecs: #####

----------------------------------

How would I make it sort the random ints & Strings?  My insertionSort method is:

public static void insertionSort(Comparable[] theArray, int n) {
...
}

Do I write it like this: insertionSort(???, Math.random());

I don't know what to put for the ??? part.  Because the method calls for a Comparable[] theArray... am I suppose to make an array with random ints?  How do I make that Comparable?
0
 
LVL 92

Expert Comment

by:objects
ID: 9872053
You create your array to sort and then fill it with random numbers using Random.

arr = new Comparable[10];
for (int i=0; i<arr.length; i++)
{
   arr[i] = new Integer(myrandom.nextInt());
}
insertionSort(arr, arr.length);
0
 

Author Comment

by:luna621
ID: 9872696
I think I got it!  Thank you very much!

-luna621 =^_^=
0
 
LVL 92

Expert Comment

by:objects
ID: 9872722
0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question