使用合并排序

时间:2015-10-04 20:13:21

标签: java mergesort divide-and-conquer

我试图使用mergesort对数组的索引进行排序。 mergesort工作得很好,最后的答案是完全正确的,但我不确定为什么索引不能在正确的位置工作。我不想对数组进行排序,我想做的就是对作为索引列表的perm []数组进行排序。

为了避免混淆,下面是一个例子: perm数组保存原始数组nums []的初始索引(即0到nums.length - 1)

我想根据nums []数组中的数据移动perm数组中的索引,以便索引代表排序顺序。

For example :
Array -> (-1,9,-5,3,0)
Initial perm -> (0,1,2,3,4)
After sorting perm based on array - > (2,0,4,3,1)

这是我的代码:

import java.util.Arrays;

public class IndexSort {

    public boolean leq(Comparable u, Comparable v) {
        return u.compareTo(v) <= 0;
    }

    public void merge(Comparable a[], int temp[], int perm[], int lo, int mid, int hi) {
        int i = lo, j = mid + 1;

        for (int k = lo; k <= hi; k++) {
            temp[k] = perm[k];
        }

        for (int k = lo; k <= hi; k++) {
            if (i > mid) {
                perm[k] = temp[j++];
            } else if (j > hi) {
                perm[k] = temp[i++];
            } else if (leq(a[perm[i]], a[perm[j]])) {
                perm[k] = temp[i++];
            } else {
                perm[k] = temp[j++];
            }
        }
    }

    public void mergeSort(Comparable a[], int temp[], int perm[], int lo, int hi) {
        if (hi <= lo)
            return;
        int mid = (hi + lo) / 2;
        mergeSort(a, temp, perm, lo, mid);
        mergeSort(a, temp, perm, mid + 1, hi);
        merge(a, temp, perm, lo, mid, hi);
        System.out.println(" lo  = " + lo + "  mid  = " + mid + "  hi  = " + hi);
        System.out.println(Arrays.toString(perm));
    }

    public void indexSort(Comparable nums[], int perm[]) {
        int temp[] = new int[nums.length];
        Comparable temp2[] = new Comparable[nums.length];
        mergeSort(nums, temp, perm, 0, nums.length - 1);
    }

    public static void main(String[] args) {
        IndexSort o1 = new IndexSort();
        Comparable nums[] = { 12, -12, 0, 123, -123, 1, 2, 3, 4, -4, -4, -3, -2, 1 };
        int perm[] = new int[nums.length];
        for (int i = 0; i < perm.length; i++) {
            perm[i] = i;
        }
        System.out.println(Arrays.toString(nums));
        System.out.println(Arrays.toString(perm));
        o1.indexSort(nums, perm);
        System.out.println(Arrays.toString(perm));
    }
}

1 个答案:

答案 0 :(得分:1)

我认为此行需要从 var news =[ {title: 'Eyes without a Face: Horror and Liberation', text: '<p>abc <img src="http://www.hercampus.com/sites/default/files/2014/10/05/Sweet-puppy-sitting.jpg"></p>', type:"review",image:"http://images.popmatters.com/misc_art/d/dvd-eyeswithoutface-650.jpg"}, {title:'The Psychedelic Comics of Grant Morrison',text:"how do you do", type:"article",image:"http://comicsastonish.files.wordpress.com/2013/09/the-filth-panels.jpg"}, {title:"MUGEN: A Customizable Fighter with No Copyright Worries",text:"testing 1 2 3", type:"bump", image:"http://fc00.deviantart.net/fs71/i/2012/150/3/b/shaq_vs_spongebob_by_pershon0-d51oiuf.png"}, {title:"Lifted-1: spaze jazz",text:"live easygoing jazz mixed with drowsy, playful synth sounds that would be familiar to fans of vaporwave",type:"bump",image:"http://p-a-n.org/wp-content/uploads/2015/04/PAN-61-Lifted-_-front_web.jpg" } ]; 更改为perm

temp