本文介绍了排序(名称)使用合并排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
排序重复字符串有问题,
having problem sorting repeated Strings,
这是我的code ..
and here's my code..
我成功排序的第一个数组但在第二(反复字符串),似乎不是在有序输出,你可以帮我追查什么是错在我的code ..
i successfully sorted the first array but in the second (with repeated strings) it seems not in orderly output, can you help me to trace whats wrong in my code..
import java.util.*; public class NewClass { public static void main(String[] args) { String[] ClassOne = { "Kring", "Panda", "Soliel", "Darryl", "Chan", "Matang", "Jollibee.", "Inasal" }; String[] ClassTwo = { "Minnie", "Kitty", "Madonna", "Miley", "Zoom-zoom", "Cristine", "Bubbles", "Ara", "Rose", "Maria" }; String[] names = new String[ClassOne.length + ClassTwo.length]; mergeSort(ClassOne); mergeSort(ClassTwo); merge(names, ClassOne, ClassTwo); mergeSort(names); //Arrays.sort(names); for (String ClassThree : names) { System.out.println(ClassThree); } } public static void mergeSort(String[] names) { if (names.length > 2) { String[] left = new String[names.length / 2]; String[] right = new String[names.length - names.length / 2]; for (int i = 0; i < left.length; i++) { left[i] = names[i]; } for (int i = 0; i < right.length; i++) { right[i] = names[i + names.length / 2]; } mergeSort(left); mergeSort(right); merge(names, left, right); } } public static void merge(String[] names, String[] left, String[] right) { int a = 0; int b = 0; for (int i = 0; i < names.length; i++) { if (b >= right.length || (a < left.length && left[a]pareToIgnoreCase(right[b]) < 0)) { names[i] = left[a]; a++; } else { names[i] = right[b]; b++; } } } }和继承人的输出:
Ara Chan Cristine Bubbles Jollibee. Inasal Kring Madonna Matang Miley Minnie Kitty Panda Rose Maria Soliel Darryl Zoom-zoom...
推荐答案修改
if (names.length > 2) {与
if (names.length >= 2) {输出
Ara Bubbles Chan Cristine Darryl Inasal Jollibee. Kitty Kring Madonna Maria Matang Miley Minnie Panda Rose Soliel Zoom-zoom更多推荐
排序(名称)使用合并排序
发布评论