Re: Sort Method Conversion



On Jun 28, 9:07 am, kaltizer <kevinalti...@xxxxxxxxx> wrote:
I need to modify this method to sort an array of strings rather than
chars:

// selectionSort(): sorts the elements of a
public static void selectionSort(char[] v) {
for (int i = 0; i < v.length-1; ++i) {
// guess the location of the ith smallest element
int guess = i;
for (int j = i+1; j < v.length; ++j) {
if (v[j] < v[guess]) { // is guess ok?
// update guess to index of smaller element
guess = j;
}
}

// guess is now correct, so swap elements
char rmbr = v[i];
v[i] = v[guess];
v[guess] = rmbr;
}

}
}

Any ideas?

You could use String.compareTo() method for string comparison, instead
of < operatot.

.



Relevant Pages

  • Re: Multithreading / Scalability
    ... int thread_number; ... catch (InterruptedException e) ... public static void main{ ... every increase in threads reduced the number of calculations that could be performed although again not as dramatically as I expected with the increase in number of threads. ...
    (comp.lang.java.programmer)
  • Re: Using java.util.map
    ... public static void main{ ... The problem is not with the Map implemetations but with the handling of the ... In Java you used an imutable one. ... Is is partially the fault of the Java language in not making int a true ...
    (comp.lang.java.advocacy)
  • Re: uninitialized variable
    ... public static void main(String args) { ... int y = 10; ... But as far as the compiler ...
    (comp.lang.java.programmer)
  • Re: last version
    ... public static void main ... JSlider slider = a.getSource; ... int fontSize = source.getValue; ... Sometimes I'm in a good mood. ...
    (comp.lang.java.help)
  • RE: New JDBC 1.2 driver runs slower than JDBC 1.1 driver-- expecte
    ... public static void main ... Driver JDBCDriver = DriverManager.getDriver; ... int iSeqIx = 1; ... SignatureCaptureDT, PersonGID, LastModPersonGID, IsCCmUSer, HasBeenCCMUser" + ...
    (microsoft.public.sqlserver.jdbcdriver)