排序(名称)使用合并排序

编程入门 行业动态 更新时间:2024-10-26 10:30:11
本文介绍了排序(名称)使用合并排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

排序重复字符串有问题,

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

更多推荐

排序(名称)使用合并排序

本文发布于:2023-11-30 17:09:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650826.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:名称

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!