简体   繁体   English

Java:将列表分为已排序的“块”,以完全对列表进行排序

[英]Java: Breaking a list into “Chunks” that are sorted, to sort list completely

Okay, first post here, so please bear with me. 好吧,先在这里发帖,所以请多多包涵。

So I'm trying to work this out on my own as much as possible, but I'm at a bit of a loss here. 因此,我正在尝试尽可能自己解决这个问题,但是我有点茫然。 I have been given a list ( and will be given many more to test, but I'm working with this one) of 12 integers. 我得到了一个由12个整数组成的列表(并将提供更多测试对象,但是我正在使用它)。 The list is as follows: 列表如下:

18 21 9 12 99 4 101 8 14 7 112 98 18 21 9 12 99 4 101 8 14 7 112 98

and a chunk is defined as an already sorted grouping of integers. 块定义为已经排序的整数分组。 This means there are 6 chunks. 这意味着有6个块。 Since this is a recursive chunk merge sort, I need to be able to break it up based on the chunks. 由于这是递归的块合并排序,因此我需要能够基于块将其分解。

The first split would look like: 第一次拆分看起来像:

18 21 9 12 99 4 101 | 18 21 9 12 99 4 101 | 8 14 7 112 98 8 14 7 112 98

and the second would look like: 第二个看起来像:

18 21 | 18 21 | 9 12 99 | 9 12 99 | 4 101 ||| 4 101 ||| 8 14 | 8 14 | 7 112 | 7112 | 98 98

As you can see, this means six chunks. 如您所见,这意味着六个块。 Now, I have managed to get to the first step, but it is not done via recursion, or through these chunks. 现在,我设法到达了第一步,但是并没有通过递归或这些块来完成。 I cheated and took the size of the list and cut it in half. 我作弊,把名单的大小切成两半。 That's a no go. 那是不行的。

I am open to trying out any suggestion, I am just stumped at how to go about breaking this down into these chunks, especially if I cannot dictate the number of "chunks" going in. 我乐于尝试任何建议,我只是为如何将其分解为这些部分而感到困惑,特别是如果我无法确定要输入的“块”的数量。

Here's a chunk of my code, if it helps: 这是我的代码的一部分,如果有帮助的话:

    public List<Integer> chunkMergesort(List<Integer> S) {
    int c = 0;

    if(S.size() > 1){
        c = 1;
        for(int i = 0; i<S.size()-1 ; i++){
            if(S.get(i+1)< S.get(i)){
                c++;
                System.out.println("C is: " + c);
            }
        }
        System.out.println("Final C is: " + c);
        chunkDivide(S, c);
    }
        return S;
}

public Chunks chunkDivide(List<Integer> S, int c) {

    int Ssize = S.size()/2;
    System.out.println("List:"+ S);

    List<Integer> S1 = new ArrayList<Integer>();
    List<Integer> S2 = new ArrayList<Integer>();

    for(int i = 0; i < Ssize; i++){
        //System.out.println("i is: "+ i);
        S1.add(i, S.get(i));    
        System.out.println("S1 is:"+ S1);
    }

    for(int i = Ssize; i < S.size(); i++){
        S2.add(i-Ssize, S.get(i));
        System.out.println("S2 is:"+ S2);

    }

    System.out.println("S1 is: "+ S1);
    System.out.println("S2 is: "+ S2);      

    Chunks p = new Chunks(S1, S2);

    //chunkMergesort(S1);
    //chunkMergesort(S2);
    //chunkDivide(p.right, mid);
    //chunkDivide(p.left, mid);

    merge(S1,S2);

    return p;

Thanks in advance! 提前致谢!

EDIT: 编辑:

I ended up using something similar to what was suggested by @Eritrean but a whole new issue arose. 我最终使用了类似于@Eritrean所建议的内容,但是出现了一个新问题。 My list is moved into a List of chunks, but the last item in the list (98) which should be the last chunk as well, gets dropped. 我的列表移到了一个块列表中,但是列表中的最后一个项目(98)也应该是最后一个块,但该项目被删除了。 I tried to ask how to fix this issue in another question, and was immediately dismissed as it being a duplicate. 我试图在另一个问题中询问如何解决此问题,但由于重复而被立即驳回。 Those "answers" don't actually answer my question. 这些“答案”实际上并未回答我的问题。 Any more insight into this would be wonderful. 对此有任何更多的见解将是美妙的。

Below will be my image showing the output. 下面是显示输出的我的图像。 CMD Snip CMD片段

You can iterate through the list and look for sorted parts of the list by comparing current element by its successors. 您可以遍历列表,并通过比较当前元素的后继元素来查找列表的排序部分。 Then you can add this parts to a list of lists using the method list.subList(int fromIndex, int toIndex). 然后,您可以使用方法list.subList(int fromIndex,int toIndex)将这部分添加到列表列表中。 Example: 例:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class NewClass {

    public static void main(String[] args) {
        List<Integer> intList = Arrays.asList(18, 21, 9, 12, 99, 4, 101, 8, 14, 7, 112, 98);

        List<List<Integer>> chunks = breakIntoChunks(intList);

        for(List<Integer> chunk : chunks){
            System.out.println(chunk);
        }
    }

    private static List<List<Integer>> breakIntoChunks(List<Integer> intList) {
        List<List<Integer>> chunks = new ArrayList<>();
        int curr =0;
        for(int i = 0; i<intList.size()-1; i++){
            if(intList.get(i).compareTo(intList.get(i+1))>0 || i==intList.size()-1){
                chunks.add(intList.subList(curr, i+1));
                curr = i+1;
            }
         }
         if(curr<=intList.size()){
              chunks.add(intList.subList(curr,intList.size()));
         }
         return chunks;       
      }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM