compareTo() problems
From: TookieClothespin (krista2882_at_gmail.com)
Date: 02/24/05
- Next message: Eric Sosman: "Re: compareTo() problems"
- Previous message: javaSwing: "can paint panel scrollable ?"
- Next in thread: Eric Sosman: "Re: compareTo() problems"
- Reply: Eric Sosman: "Re: compareTo() problems"
- Reply: proGex: "Re: compareTo() problems"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 24 Feb 2005 10:21:34 -0500
I'm trying to sort an array of strings using Selection Sort. Im using
CompareTo to compare the string elements in the array to each other, but
the loop where I use this method is being skipped over.
Does anyone know an alternate way to do this so that the program actually
enters the loop?
Thaks for your help!
here's my code:
public void selectionSort(String[] array)
{
int compcount = 0;
int excount = 0;
String temp;
for (int i=0; i<array.length-1; i++)
{
for (int j= i+1; j < array.length; j++)
{
if (array[i].compareTo(array[j]) > 0)
{
// exchange elements
temp = array[i];
array[i] = array[j];
array[j] = temp;
excount++;
}
compcount++;
}
}
System.out.println("Selection Sort: " + excount + " exchanges,
and " + compcount + " comparisons.");
}
- Next message: Eric Sosman: "Re: compareTo() problems"
- Previous message: javaSwing: "can paint panel scrollable ?"
- Next in thread: Eric Sosman: "Re: compareTo() problems"
- Reply: Eric Sosman: "Re: compareTo() problems"
- Reply: proGex: "Re: compareTo() problems"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|