help on selection sort

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mfshake
    New Member
    • Nov 2007
    • 16

    help on selection sort

    i am using only a driver and not a server. I know how to do selection sort for ints but can't figure out how to do it for Strings. I want to sort it my first letter only Here is my code that i started:

    private static void selectionSort(S tring [] a)
    {
    String [] array = a;
    int min, temp;

    for (int index = 0; index < a.length - 1; index++)
    {
    min = index;
    for (int scan = index + 1; scan < a.length; scan++)
    if (a[scan].substring(0,1) < a[min].substring(0,1) )
    min = scan;

    temp = a[min].substring(0,1) ;
    a[min].substring(0,1) = a[index].substring(0,1) ;
    a[index].substring(0,1) = temp;
    }//close for
    }//close selectionSort


    help is appreciated
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Originally posted by mfshake
    i am using only a driver and not a server. I know how to do selection sort for ints but can't figure out how to do it for Strings. I want to sort it my first letter only Here is my code that i started:

    private static void selectionSort(S tring [] a)
    {
    String [] array = a;
    int min, temp;

    for (int index = 0; index < a.length - 1; index++)
    {
    min = index;
    for (int scan = index + 1; scan < a.length; scan++)
    if (a[scan].substring(0,1) < a[min].substring(0,1) )
    min = scan;

    temp = a[min].substring(0,1) ;
    a[min].substring(0,1) = a[index].substring(0,1) ;
    a[index].substring(0,1) = temp;
    }//close for
    }//close selectionSort


    help is appreciated
    You may read the Collator class.

    there is a compare method there that has two parameters.
    First string and second string. starts from left (when we read). just like we did on comparing numerical data types.

    It returns negative if first string is less than second string.
    returns zero if they are equal
    and returns positive number if first string is greater than second string.

    regards,
    sukatoa

    Comment

    Working...