我正在尝试修改合并排序算法以按字典顺序对字符串数组进行排序,我并不是很先进,所以我可能会犯一些新手错误。这是主要的、测试者和归并分类器。
public class MergeSorter
{
private int[] a;
private int[] left_side;
private int[] right_side;
public MergeSorter(int[] anArray)
{
a = anArray;
}
public void sort()
{
int size = 1;
int from = 0;
int to = a.length - 1;
while (size < to - from)
{
int offset = 0;
while(offset < to)
{
merge(from, to, offset);
offset = offset + (2 * size);
}
}
size = 2 * size;
}
public void merge(int from, int mid, int to)
{
System.out.println("Merging " + from + "..." + mid
+ " and " + (mid + 1) + "..." + to);
}
}
和 ...
import java.util.Arrays;
public class MergeSortDemo
{
public static void main(String[] args)
{
String[] array = new String[]{"Jenny","Rob","Dereck","Mike","Dan"};
System.out.println(Arrays.toString(array));
MergeSorter sorter = new MergeSorter(array);
sorter.sort();
System.out.println(Arrays.toString(array));
}
}