有一个字符串插入排序问题 [英] Got a string insertion sort problem

查看:71
本文介绍了有一个字符串插入排序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是输出 aa cc dd ee Koko Yoko Yolo ii



我很困惑。 Koko Yoko Yolo 应该在开头,对吗?

我已经检查了整个代码3次。无法发现任何错误。

请指出我错误的地方。

是的,我知道这可能不是最理想的解决方案。



我尝试过:



Here is the output aa cc dd ee Koko Yoko Yolo ii

I'm confused. Koko Yoko and Yolo should be at the start, right?
I've checked the whole code for 3 times. Couldn't've found any mistakes.
Please, point out where I had mistaken.
Yes, I know that it's probably not the most optimal solution.

What I have tried:

class InsertionSort {	
       public static String[] sort (String a[]) {
		String key="0";
		int k=0;
		boolean If;
		String temp1,temp2;
		for (int i = 1; i <= a.length-1; i++) {
			If = false;
			key = a[i];
			for (int o = i-1; o > -1; o--) {
				boolean u;
				if (o==0) u = StrComp(a[o] , key);
				else u = (StrComp(a[o], key)) && (StrComp(key, a[o-1]));
				if (u) 
					{k = o; If = true; break; }
		
			}
			if (If==true){
				temp1 = temp2 = "-1";
			for (int o = k; o < i ; o++) {
				temp2 = a[o+1];
				if (temp1 == "-1")
				    a[o+1] = a[o];
			    else
					a[o+1] = temp1;
				temp1 = temp2;
			}
			a[k]=key;
		    }
		}
		return a;
    }
	public static Boolean StrComp (String a, String b) {
		if (a==b) return true;
		int alength=a.length()-1;
		int blength=b.length()-1;
		
		for (int i=0; i <= alength; i++){
					
			if (i <= blength){
				char cha = a.charAt(i);
				char chb = b.charAt(i);	
				if (cha != chb)
				if (cha > chb) 
			return true;}
		else return true;
		}
		return false;
	}	
}
		
		


class Insertion1 {
    public static void main(String args[]){
		 String array2[] = {"aa","ii","cc", "dd", "ee", "Yoko", "Koko", "Yolo"};
		 
		 array2 = InsertionSort.sort(array2);
		 for (String i: array2) System.out.print(i + " ");
	}
}

推荐答案

问题出在你的 StrComp()函数中,它必须是:

The problem is in your StrComp() function, it must be:
public static Boolean StrComp (String a, String b) {
		if (a==b) return true;
		
		int alength=a.length()-1;
		int blength=b.length()-1;
		
		for (int i=0; i <= alength; i++){
					
			if (i <= blength){
				char cha = a.charAt(i);
				char chb = b.charAt(i);	
				// if (cha != chb)
				if (cha > chb) 
			        return true;
			    else
			        return false;
			}
		    else return true;
		}
		return false;
	}


Quote:

这是输出aa cc dd ee Koko Yoko Yolo ii

Here is the output aa cc dd ee Koko Yoko Yolo ii



我们可以看到长度为2的所有字符串都是有序的,每个长度为4的字符串也是有序的。

这导致怀疑字符串比较。



要理解它是如何错误的,最好是使用调试器,看看当函数比较时会发生什么长度为2的字符串,另一个长度为4.



您的代码行为不符合您的预期,您不明白为什么!



有一个几乎通用的解决方案:逐步在调试器上运行代码,检查变量。

调试器在这里显示你的代码在做什么并且你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它不知道你应该做什么,它没有找到bug,它只是帮助你来笑告诉你发生了什么事。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows /jdb.html [ ^ ]

https: //www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]

调试器仅显示您的代码正在执行的操作,您的任务是与什么进行比较应该这样做。


What one can see is that all strings of length 2 are in order, and each strings of length 4 are in order too.
This lead to suspect the string comparison.

To understand how it is wrong, the best is to use the debugger and see what happen when the function compares a string of length 2 with another one of length 4.

Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


请参阅插入排序 - Google搜索 [ ^ ]。


这篇关于有一个字符串插入排序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆