Mergesort For ArrayList <string> With Coordinate Points </string>

时间:2015-04-21 07:20:05

标签: java sorting arraylist merge mergesort

我正在尝试合并一个字符串的ArrayList。每个字符串都是一个x,y点,它们需要按x坐标排序。我正试图在我的算法书中的伪代码之后对其进行建模。但是,我很困惑如何为我的目的转换它(因为它处理数组,我需要它为字符串的ArrayList)以及函数是否应该返回任何东西。

我的书提供的伪代码如下:

//"This algorithm sorts the array a[i],...,a[j] in nondecreasing order. 
//It uses the merge algorithm."
//Input Parameters: a,i,j
//Output Parameter: a
mergesort(a,i,j) {
    //if only one element, just return
    if (i == j)
        return
    //divide a into two nearly equal parts
    m = (i + j) / 2
    //sort each half
    mergesort(a, i, m)
    mergesort(a, m + 1, j)
    //merge the two sorted halves
    merge(a, i, m, j)
}

//"This algorithm receives as input indexes i, m, and j, and an array a, where 
//a[i],...,a[m] and a[m+1],...,a[j] are each sorted in nondecreasing order. 
//These two nondecreasing subarrays are merged into a single nondecreasing 
//array."
//Input Parameters: a,i,m,j
//Output Parameter: a

merge(a,i,m,j) {
    p = i //index in a[i],...,a[m]
    q = m+1 //index in a[m+1],...,a[j]
    r = i //index in a local array c
    while(p <= m && q <= j) {
        //copy smaller value to c
        if(a[p] <= a[q]) {
            c[r] = a[p]
            p = p + 1
        }
        else {
            c[r] = a[q]
            q = q + 1
        }
        r = r + 1
    }
    //copy remainder, if any, of first subarray to c
    while(p <= m) {
        c[r] = a[p]
        p = p + 1
        r = r + 1
    }
    //copy remainder, if any, of second subarray to c
    while(q <= j) {
        c[r] = a[q]
        q = q + 1
        r = r + 1
    }
    //copy c back to a
    for r = i to j
        a[r] = c[r]
}

我的尝试如下:

public static void mergesort(ArrayList<String> data, int i, int j)
{
    //if only one element, just return
    if(i == j)
    {
        return;
    }
    //divide data into two nearly equal parts
    int m = (i + j) / 2;
    //sort each half
    mergesort(data, i, m);
    mergesort(data, m + 1, j);
    //merge the two sorted halves
    merge(data, i, m, j);
}

public static void merge(ArrayList<String> data, int i, int m, int j) //1,3,5
{
    int p = i; //index in data.get(i),...,data.get(m)
    int q = m+1; //index in data.get(m + 1),...,data.get(j)
    ArrayList<String> c = new ArrayList<String>(); //initialize local ArrayList<String> c
    while(p <= m && q <= j)
    {
        String[] point = data.get(p-1).split(",");
        String[] otherPoint = data.get(q-1).split(",");
        if(Integer.parseInt(point[0]) <= Integer.parseInt(otherPoint[0]))
        {
            c.add(data.get(p-1));
            p += 1;
        }
        else
        {
            c.add(data.get(q-1));
            q += 1;
        }
    }
    //copy remainder, if any, of first subarraylist to c
    while(p <= m)
    {
        c.add(data.get(p-1));
        p += 1;
    }
    //copy remainder, if any, of second subarrayList to c
    while(q <= j)
    {
        c.add(data.get(q-1));
        q += 1;
    }
    //copy c back to data
    for(int x = 0; x < c.size(); x++)
    {
        data.set(x, c.get(x));
    }
}

我尝试使用ArrayList字符串:

  

data = [“400,400”,“100,100”,“500,500”,“600,600”,“50,50”]

mergesort(data, 1, 5)应该ArrayList排序如下:

  

[“50,50”,“100,100”,“400,400”,“500,500”,“600,600”]

但它给了我:

  

[“50,50”,“600,600”,“500,500”,“600,600”,“50,50”]

我有什么问题吗?我只是不能把手指放在上面。

2 个答案:

答案 0 :(得分:1)

data.set(x, c.get(x));更改data.set(i+x-1, c.get(x));

为了让代码更容易理解,因为java数组被0编入索引,为了简单起见,我肯定会将所有索引建立在0上,以避免拥有所有这些(p-1)和{{ 1}}东西。

答案 1 :(得分:1)

你正在做的一切正确,除了下面提到的合并方法合并部分(ArrayList数据,int i,int m,int j)。  //将c复制回数据     for(int x = 0; x&lt; c.size(); x ++)     {         data.set(x,c.get(x));     }

这里是你在错误的地方将数据从c复制到数据。 由于您已将i中的列表合并到j,因此您应该将数据从c复制到此处。

所以使用

   //copy c back to data
    for (int x = 0; x < c.size(); x++) {
        data.set(x+i-1, c.get(x));
    }