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 2 of 2

Thread: Selection Sorting of Data type (Char)

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Selection Sorting of Data type (Char)

    public class CharSortTest {
     
        public static void main(String[] args) {
     
            char[] letters = {'j', 'a', 'z', 'y', 'x'};
     
            char min = 0;
     
            char swap = 0;
     
            for (int x = 0; x <= letters.length - 1; x++) {
     
                min = letters[x];
     
                for (int i = x + 1; i <= letters.length - 1; i++) {
     
                    if (letters[i] < min) {
     
                        min = letters[i];
                    }
                }
     
                if (letters[x] != min) {
     
                    swap = letters[x];
                    letters[x] = min;
                    min = swap;
                }
     
            }
     
            for (int p = 0; p <= letters.length - 1; p++) {
     
                System.out.println(letters[p]);
            }
        }
    }

    output:
    a
    a
    x
    x
    x

    i followed the logic of selection sorting.. i even checked the integer values of each character in my array
    but why is my program not sorting....


  2. #2
    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: Selection Sorting of Data type (Char)

    In selection sort, you don't want to keep track of the specific element, but rather it's index. This way you can perform the swap at the end. Also, there's no point in having the second if statement.

Similar Threads

  1. char (toUppercase?)
    By chronoz13 in forum Java Theory & Questions
    Replies: 4
    Last Post: December 12th, 2009, 10:01 AM
  2. Selection Sorting
    By chronoz13 in forum Algorithms & Recursion
    Replies: 5
    Last Post: December 10th, 2009, 11:08 AM
  3. Sorting Algorithms
    By Dalisra in forum Java Programming Tutorials
    Replies: 1
    Last Post: November 10th, 2009, 09:24 PM
  4. [SOLVED] help with sorting...(comparator)
    By mdstrauss in forum Collections and Generics
    Replies: 2
    Last Post: July 26th, 2009, 06:25 AM
  5. Replies: 4
    Last Post: June 10th, 2009, 01:04 AM