Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: sorting name using Selectionsort

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default sorting name using Selectionsort

    hi!
    is there anyone else who can correct my codes?
    i know this is wrong

    here's the problem: Write a class called slectionSort
    this would allow the user to input ten string names (e.g adal, bryan, pius,apple, mango,strawberry, zion, mary, thalea, bon.. . ) and will sort to ascending order using the SelectionSort
    the output will be:
    adal
    apple
    bon
    bryan
    mary
    pius
    strawberry
    thalea
    zion

    and here is my disusting codes
    i can't get it.. maybe its because i use the String.. please help me
    public class selectSort{
      public static void main(String a[]){
        int i;
        String array[] = {. . .};
     
        System.out.println("Data items in original order ");    
        for(i = 0; i < array.length; i++)
          System.out.print( array[i]+"  ");
        System.out.println();
        selection_srt(array, array.length);       
     
        System.out.println("Data items in ascending order");    
        for(i = 0; i <array.length; i++)
          System.out.print(array[i]+"  ");
     
      }
     
      public static void selection_srt(String array[], int n){
        for(int x=0; x<n; x++){
          int min = x;
          for(int y=x; y<n; y++){
            if(array[min]> array[y]){
              min = y;
            }
          }
         String temp = array[x];
          array[x] = array[min];
          array[min] = temp;
        }
      }
    }
    Last edited by helloworld922; May 19th, 2010 at 07:51 PM. Reason: please use [code] tags for code


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: sorting name using Selectionsort

    So how do you know it is wrong? If you get any errors, post the full text here.

    Incidentally, you can't compare Strings with relational operators. Use the String.compareTo(..) method.

  3. #3
    Member
    Join Date
    May 2010
    Posts
    36
    Thanks
    0
    Thanked 13 Times in 12 Posts

    Default Re: sorting name using Selectionsort

    Quote Originally Posted by asdfg View Post
    can't get it.. maybe its because i use the String.. please help me
    try Arrays.sort(yourArray, Collections.reverseOrder());

    import java.util.Arrays;
    import java.util.Collections;
     
    public class javaguitest {
    	public static void main(String[] args) {
    		String[] arr = { "adal", "apple", "bon", "bryan", "mary", "pius",
    				"strawberry", "thalea", "zion" };
     
    		System.out.println(Arrays.toString(arr));
    		Arrays.sort(arr, Collections.reverseOrder());
    		System.out.println("After desc sort: " + Arrays.toString(arr));
    	}
    }

    the output

    [adal, apple, bon, bryan, mary, pius, strawberry, thalea, zion]
    After desc sort: [zion, thalea, strawberry, pius, mary, bryan, bon, apple, adal]
    Last edited by j2me64; May 19th, 2010 at 07:35 AM.

  4. #4
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: sorting name using Selectionsort

    Quote Originally Posted by j2me64 View Post
    try Arrays.sort(yourArray, Collections.reverseOrder());
    Really? Is that a Selection Sort?

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: sorting name using Selectionsort

    I suspect it's either a quick-sort, or a hybrid sort.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: sorting name using Selectionsort

    Arrays.sort uses a modified quicksort (~ n*(log(n)) )...even it it used selection sort it seems that wouldn't solve your problem of needing to write it yourself. The code you have is on its way, just needs a bit more work in the comparison line (see dlorde's response for a clue)

  7. #7
    Junior Member
    Join Date
    May 2010
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: sorting name using Selectionsort

    See the modified code.It will work

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Vector;
     
    public class selectSort{
     
     
    public static void main(String a[]){
    int i;
    String array[] = {"sdasdasd","asdas","abcd","gyhjhgj"};
     
    System.out.println("Data items in original order "); 
    for(i = 0; i < array.length; i++)
    System.out.print( array[i]+" ");
    System.out.println();
    selection_srt(array, array.length); 
     
    System.out.println("Data items in ascending order"); 
    for(i = 0; i <array.length; i++)
    System.out.print(array[i]+" ");
     
    }
     
     
     
    	public static void selection_srt(String array[], int n) {
    		for (int x = 0; x < n; x++) {
    			int min = x;
    			for (int y = x; y < n; y++) {
    				if (array[min].compareTo(array[y]) > 0) {
    					min = y;
    				}
    			}
    			String temp = array[x];
    			array[x] = array[min];
    			array[min] = temp;
    		}
    }
    }
    We can use compareTo() method of string.

    Result
    Data items in original order
    sdasdasd asdas abcd gyhjhgj
    Data items in ascending order
    abcd asdas gyhjhgj sdasdasd
    Last edited by jnthakar; May 20th, 2010 at 12:21 AM. Reason: please use [code] tags

  8. #8
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: sorting name using Selectionsort

    Quote Originally Posted by copeg View Post
    The code you have is on its way, just needs a bit more work in the comparison line (see dlorde's response for a clue)
    No effort needed - someone has fixed the code for him...

Similar Threads

  1. Sorting/Lexicographic =)
    By jcs990 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 12th, 2010, 11:19 PM
  2. thread sorting
    By thanos_ in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 12th, 2010, 06:23 PM
  3. [SOLVED] sorting
    By kite98765 in forum Algorithms & Recursion
    Replies: 8
    Last Post: February 4th, 2010, 08:34 AM
  4. Sorting Algorithms
    By Dalisra in forum Java Programming Tutorials
    Replies: 1
    Last Post: November 10th, 2009, 09:24 PM
  5. [SOLVED] help with sorting...(comparator)
    By mdstrauss in forum Collections and Generics
    Replies: 2
    Last Post: July 26th, 2009, 06:25 AM