按字典顺序排序字符串数组

时间:2012-03-15 07:11:33

标签: arrays string sorting lexicographic

我正在尝试修改合并排序算法以按字典顺序对字符串数组进行排序,我不是很先进,所以我可能会犯一些新手错误。这是主要的,测试者和mergesorter。

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));
    }
}

0 个答案:

没有答案