Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java插入排序字符串数组_Java_Arrays_String_Sorting_Insertion Sort - Fatal编程技术网

Java插入排序字符串数组

Java插入排序字符串数组,java,arrays,string,sorting,insertion-sort,Java,Arrays,String,Sorting,Insertion Sort,我试图通过比较字符串的首字母,按字母顺序对其排序。我可以使用整数进行插入排序,但当我将整数更改为字符串并引用第一个字符的整数值进行比较时,插入排序将停止工作。这是我的密码,有人能帮我知道我做错了什么吗 public static boolean cSL(String a, String b) { int aN = (int)(a.charAt(0)); int bN = (int)(b.charAt(0)); if(aN < 97) aN += 32;//make

我试图通过比较字符串的首字母,按字母顺序对其排序。我可以使用整数进行插入排序,但当我将整数更改为字符串并引用第一个字符的整数值进行比较时,插入排序将停止工作。这是我的密码,有人能帮我知道我做错了什么吗

public static boolean cSL(String a, String b)
{
    int aN = (int)(a.charAt(0));
    int bN = (int)(b.charAt(0));
    if(aN < 97) aN += 32;//make case insensitive
    if(bN < 97) bN += 32;
    return(aN < bN);
}

public static void main(String[] args)
{
    String[] sort = {"ai", "ff", "gl", "bw", "dd", "ca"};
    for( int c = 1; c < sort.length; c++ )
    {
        String key = sort[c];
        int count = c - 1;
        while (count >= 0 && cSL(key, sort[count]))
        {
            sort[count + 1] = sort[count];
            count--;
        }
        sort[count + 1] = sort[c];
    }
    //print out the array
    for(int n = 0; n < sort.length; n++)
        System.out.print(sort[n] + " ");

}
公共静态布尔cSL(字符串a、字符串b)
{
intan=(int)(a.charAt(0));
int bN=(int)(b.charAt(0));
if(aN<97)aN+=32;//使不区分大小写
如果(bN<97)bN+=32;
回报率(aN=0&&cSL(键,排序[count]))
{
排序[计数+1]=排序[计数];
计数--;
}
排序[计数+1]=排序[c];
}
//打印出数组
for(int n=0;n

这应该输出“ai bw ca dd ff gl”,但它会打印“ai gl ff gl”

已解决!!!我所做的就是编辑
,同时
循环,并在它下面的下一行添加注释

public static boolean cSL(String a, String b)
{
    int aN = (int)(a.charAt(0));
    int bN = (int)(b.charAt(0));
    if(aN < 97) aN += 32;//make case insensitive
    if(bN < 97) bN += 32;
    return aN < bN;
}

public static void main(String[] args)
{
    String[] sort = {"ai", "ff", "gl", "bw", "dd", "ca"};
    for( int c = 1; c < sort.length; c++ )
    {
        String key = sort[c];
        int count = c - 1;
        while (count >= 0 && cSL(key, sort[count]))
        {
            String temp = sort[count+1];
            sort[count + 1] = sort[count];
            sort[count] = temp;
            count--;
        }
        //sort[count + 1] = sort[c]; This Line is in comment because it is not needed
    }
        //print out the array
        for(int n = 0; n < sort.length; n++)
            System.out.print(sort[n] + " ");

}
公共静态布尔cSL(字符串a、字符串b)
{
intan=(int)(a.charAt(0));
int bN=(int)(b.charAt(0));
if(aN<97)aN+=32;//使不区分大小写
如果(bN<97)bN+=32;
返回一个=0&&cSL(键,排序[count]))
{
字符串温度=排序[计数+1];
排序[计数+1]=排序[计数];
排序[计数]=温度;
计数--;
}
//sort[count+1]=sort[c];此行处于注释中,因为它不需要
}
//打印出数组
for(int n=0;n
已解决!!!我所做的就是编辑
,同时
循环,并在它下面的下一行添加注释

public static boolean cSL(String a, String b)
{
    int aN = (int)(a.charAt(0));
    int bN = (int)(b.charAt(0));
    if(aN < 97) aN += 32;//make case insensitive
    if(bN < 97) bN += 32;
    return aN < bN;
}

public static void main(String[] args)
{
    String[] sort = {"ai", "ff", "gl", "bw", "dd", "ca"};
    for( int c = 1; c < sort.length; c++ )
    {
        String key = sort[c];
        int count = c - 1;
        while (count >= 0 && cSL(key, sort[count]))
        {
            String temp = sort[count+1];
            sort[count + 1] = sort[count];
            sort[count] = temp;
            count--;
        }
        //sort[count + 1] = sort[c]; This Line is in comment because it is not needed
    }
        //print out the array
        for(int n = 0; n < sort.length; n++)
            System.out.print(sort[n] + " ");

}
公共静态布尔cSL(字符串a、字符串b)
{
intan=(int)(a.charAt(0));
int bN=(int)(b.charAt(0));
if(aN<97)aN+=32;//使不区分大小写
如果(bN<97)bN+=32;
返回一个=0&&cSL(键,排序[count]))
{
字符串温度=排序[计数+1];
排序[计数+1]=排序[计数];
排序[计数]=温度;
计数--;
}
//sort[count+1]=sort[c];此行处于注释中,因为它不需要
}
//打印出数组
for(int n=0;n
在while循环之后,此行存在逻辑错误

 sort[count + 1] = sort[c];
您使用的是sort[c],其中数组由上面的操作,而循环和索引被洗牌。相反,您应该使用key变量,它用于存储当前值,以便在循环重写时进行比较

 sort[count + 1] = key;

这使得代码工作得非常完美。希望这有帮助

在while循环之后,这行中有逻辑错误

 sort[count + 1] = sort[c];
您使用的是sort[c],其中数组由上面的操作,而循环和索引被洗牌。相反,您应该使用key变量,它用于存储当前值,以便在循环重写时进行比较

 sort[count + 1] = key;

这使得代码工作得非常完美。希望这有帮助

您可能需要替换
sort[count+1]=sort[c]带有
排序[count+1]=键没有看到。但是,我认为除了使代码看起来更好之外,它不会改变任何东西,因为key和sort[c]在循环的每个迭代中引用相同的值。仅供参考,您正在使整个
c
count
东西更难阅读。为什么要计数
c-1
,然后使用
count+1
<代码>计数+1
c
;不?SaschaKolberg的建议解决了你的问题issue@Ben当我运行它时,它会打印出你期望的确切输出。因为您在第一次通过while循环时用一个新值覆盖了
sort[c]
。您可能需要替换
sort[count+1]=sort[c]带有
排序[count+1]=键没有看到。但是,我认为除了使代码看起来更好之外,它不会改变任何东西,因为key和sort[c]在循环的每个迭代中引用相同的值。仅供参考,您正在使整个
c
count
东西更难阅读。为什么要计数
c-1
,然后使用
count+1
<代码>计数+1
c
;不?SaschaKolberg的建议解决了你的问题issue@Ben当我运行它时,它会打印出你期望的确切输出。因为您在第一次通过while循环时会用一个新值覆盖排序[c]