按字母顺序排列字符串(不使用compareTo方法)

时间:2014-11-06 20:54:26

标签: java string sorting while-loop compareto

我正在尝试在不使用compareTo()的情况下对字符串数组进行排序,但我陷入了while循环。有没有办法按字母顺序排序字符串而不使用compareTo()Arrays.sort()

public class mycode 
{

    public static void main(String[ ] args)
    {
            String[ ] ar = {"hello", "java", "elephant", "array"};
            mycode.display(ar);
            mycode.bubbleSort(ar);
            mycode.display(ar);
    }
    static void display(String[] ar)
    {
        System.out.println("***********************");
        for(int i = 0; i < ar.length; i++)
        {
            System.out.println(ar[i]);
        }
        System.out.println("***********************");

    }
    static void bubbleSort(String[] ar)
    {
        int theFollower;
        for(int currStart = 1; currStart < ar.length; currStart++)
        {
            theFollower = currStart;
            while(theFollower != 0 && ar[theFollower] < ar[theFollower - 1]) //this is where my problem is
            {
                String swap = ar[theFollower];
                ar[theFollower] = ar[theFollower - 1];
                ar[theFollower - 1] = swap;
                theFollower--;
            }
        }
    }

}

Alphabetization是我的目标,所以我的输出将是以下

***********************
hello
java
elephant
array
***********************
***********************
array
elephant
hello
java
***********************

我使用建议的想法添加了这个方法,但我不确定我将通过字符串的索引来实现什么

    int alphabetize(String a, String b)
    {
    String A = a.toLowerCase();
    String B = b.toLowerCase();
    if (A < B)
    {
        return -1;
    }
    else if (A > B)
    {
        return  1;
    }
    else
    {
    }
    }

3 个答案:

答案 0 :(得分:3)

我假设这是家庭作业,因为显而易见的方法就是使用compareTo(),所以我会给你一个关于如何编写自己的方法进行比较的提示。你想要一些带签名的东西

int compareStrings(String s, String t);

返回-101,具体取决于s是否按字母顺序排在字母表之前,等于或之后{/ 1}}。

逐个字符地浏览两个t个字符,并且在每个阶段,如果String中的字符小于s的字符(这里可以使用t })然后返回<;如果它更大,那么返回-1;如果他们相等,继续前进。

如果1中的字符用尽,但s中仍有一些字符,则返回t,如果相反,则返回{{1} }}。如果您同时用完两个,请返回-1

答案 1 :(得分:0)

我已经通过创建另一个函数来连续比较每个字符来完成这个程序。当发现两个字符不相等时它会中断。喜欢,&#34; Tony&#34;和#34; Toby&#34;,在比较&#39; t&#39;然后&#39; o&#39; o它比较了&#39; n&#39;和&#39; b&#39;给予&#34; Toby&#34;小于&#34; Toby&#34;然后打破。对于包含在另一个中的字符串,例如&#34; Max&#34;和&#34;麦克斯韦&#34;它使较短的一个更少。

在我的程序中,我已经要求用户输入名称,然后按字母顺序降序排序。该函数返回第一个字符串是否 lessThan (而不是&#39;&lt;&#;&#;;)第一个字符串,如果它是,则交换两个字符串。

我假设你理解冒泡排序算法,所以我不解释它。如果您不理解,请随时问我。

/**
 * To sort twenty names in descending order using Bubble sort algorithm
 * This program does not use the compareTo method of the String class
 */

import java.util.Scanner;

public class NameBubbleSort {

    /*
     * function to find whether first string comes before second one in
     * alphabetical order
     */
    static boolean lessThan(String s1, String s2) {
        boolean flag = false;
        int i, lim;
        String shorter;
        // converting strings to lower case
        String first = s1.toLowerCase(), second = s2.toLowerCase();
        // finding shorter string setting iteration limit to its length
        if (first.length() <= second.length()) {
            shorter = first;
            lim = first.length();
        } else {
            shorter = second;
            lim = second.length();
        }
        // comparing successively each character one by one if they are same
        for (i = 0; i < lim; i++)
            if (first.charAt(i) != second.charAt(i)) {
                if (first.charAt(i) < second.charAt(i))
                    flag = true;
                // breaks the loop since the characters are unequal
                break;
            }
        // condition if one name is contained in another
        // eg. Peter Steve and Peter Stevens
        if (i == lim && first == shorter)
            flag = true;
        return flag;
    }

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        String t;
        String[] name = new String[20];
        System.out.println("Enter 20 names: ");
        for (int i = 0; i < 20; i++)
            name[i] = in.nextLine();
        for (int i = 0; i < 20; i++)
            for (int j = 0; j < 19 - i; j++)
                // compares two adjacent strings and exchanges them if not in
                // correct order
                if (lessThan(name[j], name[j + 1])) {
                    t = name[j];
                    name[j] = name[j + 1];
                    name[j + 1] = t;
                }
        // printing the names
        for (String i : name) {
            System.out.println(i);
        }
        in.close();

    }

}

答案 2 :(得分:-1)

用这个代替你:

while(theFollower != 0 && ar[theFollower].compareTo(ar[theFollower - 1]) < 0)

您无法将对象与基本运算符进行比较。 compareTo就是来完成这项工作。

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#compareTo(java.lang.String)

相关问题