简体   繁体   English

Java-我的字母顺序排序算法无法始终按预期工作

[英]Java - my alphabetical order sorting algorithm not always working as expected

Here's my java code. 这是我的Java代码。 (Explanation at the end) (末尾说明)

import java.util.Scanner;
import java.util.Arrays;
/**
 *
 * @author Laksh
 */
public class Sort4 {
    public static void Swap(int[] array,int Swap1,int Swap2){
        int temporarySwapper;
        temporarySwapper=array[Swap1];
        array[Swap1]=array[Swap2];
        array[Swap2]=temporarySwapper;
    }
    public static void Swap(String[] array,int Swap1,int Swap2){
        String temporarySwapper;
        temporarySwapper=array[Swap1];
        array[Swap1]=array[Swap2];
        array[Swap2]=temporarySwapper;
    }
    public static String[] alphasort(String[] original, int level, int start, int end ){
        int swaps;
        int[] array=new int[original.length];
        for(int i=0;i<original.length;i++){
            array[i]=(int)original[i].charAt(level);
        }
        do{
            swaps=0;
            for(int i=start;i<end;i++){
                if(array[i]>array[i+1]){
                    Swap(original,i,i+1);
                    Swap(array,i,i+1);
                    swaps++;
                }
            }
        }while(swaps != 0);
        return Arrays.copyOfRange(original, start, end+1);
    }
    public static String repeat(String s,int times){
        String returnString="";
        for(int i=0;i<times;i++){
            returnString+=s;
        }
        return returnString;
    }

        public static void main(String[] args) {
        Scanner input=new Scanner(System.in);        
        System.out.println("Enter 5 Names:");
        int max=Integer.MIN_VALUE;
        String[] name=new String[5];
        for(int i=0;i<5;i++){
            name[i]=input.next();
            if(name[i].length()>max){
                max=name[i].length();
            }
        }
        for(int i=0;i<5;i++){
            if(name[i].length()<max){
                name[i]=name[i]+repeat(" ",max-name[i].length());
            }
        }

        String[] sorted=alphasort(name,0,0,name.length-1);
        for(String c:sorted){
            System.out.println(c);
        }

    }

}

To Clarify the issues I am facing, my code relies on finding ascii integer value of a char, and then sorting an integer array. 为了弄清我面临的问题,我的代码依赖于找到char的ascii整数值,然后对整数数组进行排序。 If the first letter of the string I am trying to is equal to the first of another, the code moves to the second letter, and so on. 如果我尝试输入的字符串的第一个字母等于另一个字母,则代码移至第二个字母,依此类推。 To avoid string index out of bounds, I used the "repeat" method to add enough spaces to the end of each string so that they are all the same length- as Strings of the same length would prevent this. 为了避免字符串索引超出范围,我使用“重复”方法在每个字符串的末尾添加了足够的空格,以使它们的长度都相同,因为相同长度的字符串可以防止这种情况的发生。 The issue I am facing is that it sort correctly, but in some places, it fails. 我面临的问题是排序正确,但是在某些地方失败了。

for example, if I pass in "butter" and "butterfly", the two are outputed int he order they were entered, in relation to the other values, which are sorted properly! 例如,如果我输入“ butter”和“ butterfly”,则将它们按输入的顺序输出(相对于其他值,它们已正确排序)。 For example: 例如:

butter
cookie
butterfly
cookiemonster
ninja

sorts to 排序到

butter       
butterfly    
cookie       
cookiemonster
ninja  

whereas

butterfly
cookie
butter
cookiemonster
ninja

sorts to 排序到

butterfly       
butter    
cookie       
cookiemonster
ninja

Help me, as I am unable to find my error. 帮助我,因为我找不到我的错误。

You compared only first letters of each word and the order of words, butterfly and butter, didn't change. 您只比较了每个单词的第一个字母,并且单词顺序(蝴蝶和黄油)没有变化。

You must compare each letter of each word with a function below: 您必须将每个单词的每个字母与以下功能进行比较:

  int size = myArray.length;

  for(int i = 0; i<size-1; i++) {
     for (int j = i+1; j<myArray.length; j++) {
        if(myArray[i].compareTo(myArray[j])>0) {
           String temp = myArray[i];
           myArray[i] = myArray[j];
           myArray[j] = temp;
        }
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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