简体   繁体   中英

Using Bubble Sort to Alphabetically Sort Array of Names in Java

I've been trying to tackle this bug for a while, but I can't get around to it. The purpose of the program below is to use bubble sort to alphabetically order an array of names. For example, if the names are ["Bob Joe", "Bob Frank", and "Bob Johnson"], the correctly sorted array would be ["Bob Frank", "Bob Joe", "Bob Johnson"].

The main challenge I am having is comparing any 2 strings past name.charAt(0). If I only compare the characters of any 2 strings at 1 specific index point, my code works. However, if I try to make the comparison move past index 0 if index 0 of both strings are equal to each other, my program no longer works.

The code is outlined below

public static void sortAlpha (String names[])
    {
        for (int i = 0 ; i < names.length - 1 ; i++)
        {
            for (int a = 0 ; a < names.length - 1 - i ; a++)
            {

                int length1 = names [a].length ();
                int length2 = names [a + 1].length ();

                int min = 1; 

                if (length1 > length2)
                {
                    min = length2;
                }
                else
                {
                    min = length1;
                }

                for (int b = 0 ; b < min ; b++)
                {

                    if ((int) names [a].toLowerCase ().charAt (b) > (int) names [a + 1].toLowerCase ().charAt (b))
                    {
                        String tempName = names [a];

                        // sort:
                        names [a] = names [a + 1];
                        names [a + 1] = tempName;

                        break;
                    }

                }

            }
        }
    }

If I simply default the min value to 1, the code runs and does its intended job. However, if the min value stays dynamic, the program does not work. I'm trying to discern why this is so and what the fix is. Any help would be appreciated!

You can simply use compareTo() and a temp variable to compare and store

Scanner sc = new Scanner(System.in);
String n[]= new String[5];
System.out.println("Enter the String");
for(int k = 0;k<5;k++) {
    n[k] = sc.nextLine();
}
String temp;
System.out.println("sorted order:");
for (int j = 0; j < n.length; j++) {
   for (int i = j + 1; i < n.length; i++) {
     if (n[i].compareTo(n[j]) < 0) {
        temp = n[j];
        n[j] = n[i];
        n[i] = temp;
    }
}
System.out.println(n[j]);

Check this out.

   public static void sortAlpha(String names[]) {
      for (int i = 0; i < names.length - 1; i++) {
         for (int a = 0; a < names.length - 1 - i; a++) {
            int lengthLeft = names[a].length();
            int lengthRight = names[a + 1].length();
            int minLength = lengthLeft > lengthRight ? lengthRight : lengthLeft;
            for (int b = 0; b < minLength; b++) {
               int letterLeft = (int) names[a].toLowerCase().charAt(b);
               int letterRight = (int) names[a + 1].toLowerCase().charAt(b);
               if (letterLeft > letterRight) {
                  String tempName = names[a];
                  // sort:
                  names[a] = names[a + 1];
                  names[a + 1] = tempName;
                  break;
               } else if (letterLeft == letterRight) {
                  // if the letters are the same go for the next letters
                  continue;
               } else {
                  // if it's already in the right position - stop.
                  break;
               }
            }
         }
      }
   }

Use this

for (int i = 0; i < count; i++) 
        {
            for (int j = i + 1; j < count; j++) { 
                if (str[i].compareTo(str[j])>0) 
                {
                    temp = str[i];
                    str[i] = str[j];
                    str[j] = temp;
                }
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM