Java 使用合并排序对(名称)进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20795158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sorting (names) using Merge Sort
提问by jarnthrax
having problem sorting repeated Strings,
对重复的字符串进行排序时遇到问题,
and here's my 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].compareToIgnoreCase(right[b]) < 0)) {
names[i] = left[a];
a++;
} else {
names[i] = right[b];
b++;
}
}
}
}
and heres the output::
和继承人的输出::
Ara
Chan
Cristine
Bubbles
Jollibee.
Inasal
Kring
Madonna
Matang
Miley
Minnie
Kitty
Panda
Rose
Maria
Soliel
Darryl
Zoom-zoom
...
...
采纳答案by Ashish Aggarwal
Change
改变
if (names.length > 2) {
with
和
if (names.length >= 2) {
output
输出
Ara
Bubbles
Chan
Cristine
Darryl
Inasal
Jollibee.
Kitty
Kring
Madonna
Maria
Matang
Miley
Minnie
Panda
Rose
Soliel
Zoom-zoom
回答by Christian
Just change this:
只需改变这个:
if (names.length > 2)
to
到
if (names.length > 1)
mergeSort
runs recursively spliting the array into two halves, then merges them and return back up the call chain. When the length of the array passed to mergeSort
is <= 1 it considers the array sorted, this is called the base case.
mergeSort
运行递归地将数组分成两半,然后合并它们并返回调用链。当传递给数组的长度mergeSort
<= 1 时,它认为数组已排序,这称为base case。
回答by Chris Abbenda
Why not just concatenate the two arrays (String ClassOne and ClassTwo) into one and then call a MergeSort on one array? Your process makes the program more ambiguous in my opinion.
为什么不将两个数组(String ClassOne 和 ClassTwo)连接成一个,然后在一个数组上调用 MergeSort?在我看来,您的过程使程序更加模棱两可。