【问题标题】:Merge Sorting by using Queue in Java在 Java 中使用队列合并排序
【发布时间】:2017-05-03 05:44:17
【问题描述】:
public class NumberQueue {
    private int firstLoc=0;
    private int lastLoc=0;
    private int[] numArray = new int[MAXSIZE]; 
    public static final int MAXSIZE=100;

    public int getQsize() { 
        return (MAXSIZE+lastLoc-firstLoc) % MAXSIZE;
    }

    public boolean fullCheck() { 
        return (getQsize() == MAXSIZE-1);
    }

    public boolean emptyCheck() { 
        return (firstLoc == lastLoc); 
    }

    public int getValue(int loc) { 
        return numArray[loc];
    }   

    public int front() {
        return numArray[firstLoc]; 
    }

    public void remove() { 
        if (!emptyCheck()) 
            firstLoc = (firstLoc + 1) % MAXSIZE;
    }

    public int insert(int val) {
        if (fullCheck()) {
            return -1;
        } else {
          numArray[lastLoc] = val;
          lastLoc = (lastLoc +1) % MAXSIZE;
          return 0;
        }
    }

    public void display() {
        int i = firstLoc;
        while(i < lastLoc) {
            System.out.print(numArray[i]+" ");
            i++;
        }
        System.out.println();
    }   
}


public class NumberQueueMain {
  public static void main(String[] args) {

    //Create three queues
    NumberQueue S1 = new NumberQueue();
    NumberQueue S2 = new NumberQueue();
    NumberQueue S3 = new NumberQueue();

    //Get the array of queues       
    Scanner S = new Scanner(System.in);
    System.out.println("Enter a series of number with a space between each value: ");       
    String str = S.nextLine();
    String[] arr = str.split(" ");
    int array[] = new int[arr.length];
    for(int i = 0; i < arr.length; i++) {
        array[i] = Integer.parseInt(arr[i]);
    }

    //Put the array into the NumberQueues-S1.
    for(int i = 0; i < arr.length; i++) {
        S1.insert(array[i]);
    }

    //Merge Sort S1 
    int loc = -1;
    while(loc < (S1.getQsize()-1)) {
        do {
            S2.insert(S1.front());       
            S1.remove();                
            loc++;
        } while(S1.getValue(loc) < S1.getValue(loc+1) && loc < S1.getQsize()-1);
        S2.display(); 

        while(loc < S1.getQsize()-1) {
            do{
                S3.insert(S1.front());
                S1.remove();
                loc++;
        } while(S1.getValue(loc) < S1.getValue(loc+1) && loc < S1.getQsize()-1);
            S3.display();
        }

        while(!S2.emptyCheck() && !S3.emptyCheck()) {
            if(S2.front() > S3.front()) {
                S1.insert(S3.front());
                S3.remove();        
            } else if(S2.front()==S3.front()) {
                S1.insert(S2.front());
                S1.insert(S3.front());
                S2.remove();
                S3.remove();
            } else {
                S1.insert(S2.front());
                S2.remove();
            }
        }

        if (S2.emptyCheck()){
            int sizeS3 = S3.getQsize();
            for(int i = 0;i < sizeS3; i++){
                S1.insert(S3.front());
                S3.remove();
            }
        } else if(S3.emptyCheck()){
            int sizeS2 = S2.getQsize();
            for(int i = 0;i < sizeS2; i++) {
                S1.insert(S2.front());
                S2.remove();
            }
        }
    } 
    System.out.println();
    S1.display();
  }

}

输出:

输入一系列数字,每个值之间有一个空格:3 5 7 2 3 5 11 34 12 10 15 18 3 12 17 22 12 18 25 22 30 3 5 7 2 3 5 11 34 2 3 5 11 34 12 2 3 5 11 34 12 10 15 18 3 12 17 22 12 18 25 22 30

2 3 3 5 5 7 11 34 12 10 15 3 12 17 18 22 12 18 25 22 30

问题:
为什么它只对彼此相邻的每两个队列进行排序,并且只做了一次。我找不到修复它的地方。任何人都可以帮助我,请!!!

【问题讨论】:

  • 我认为不需要添加“android”标签。不是吗?
  • 我可能只是用谷歌搜索算法。看起来你付出了很大的努力,但是这段代码太混乱了,无法理解。
  • 我的教授希望我们通过使用队列和给定的类(即“类 NumberQueue”)来编写合并排序。在 Google 上找不到。
  • 您好,欢迎来到 SO。我想帮助您改写您的问题“为什么它只对彼此相邻的每两个队列进行排序,并且只做了一次。” 很不清楚您在问什么。如果您可以通过“将两个队列彼此相邻排序”或“只做了一次”来解释您的意思,那么我们可能会提供更多帮助。
  • 输出结果应该是有序的。但是,它只对前两次运行进行排序;对第三次和第四次运行进行排序;第五和第六;第七和第八。最后在合并两次运行后合并这些运行。所以结果表明它只对堆栈的一半进行了排序。我找不到修复它的地方。请大家帮帮我!!!

标签: java arrays queue mergesort


【解决方案1】:

您的代码中有很多问题。但是首先考虑第一个问题,您没有处理所有数据的原因是以下行中的错误:

while(loc < (S1.getQsize()-1))

以下代码块中的代码会从S1 中删除项目,因此每次迭代都会减小大小。

您可以通过以下方式解决此问题:

int size = S1.getQsize();
while (loc < size - 1)

或者(更好)

while (!S1.emptyCheck)

但是,即使您处理了所有输入数据,排序算法仍然存在错误。也许您可以解决此问题,进一步调查,然后在遇到困难时提出具体问题。

【讨论】:

  • 非常感谢。我按照你说的做了,而且越来越好了。运行分离更准确。但最后的合并还是一样。
  • @KaixuanChen 正如我的回答中提到的,您的代码中还有许多其他错误,所以我对合并不起作用并不感到惊讶。您需要针对您遇到的问题提出具体问题。
  • 现在的问题是代码只是对第一个循环进行排序并将它们放在一起。 { [3 5 7]+[2 3 5 11 34] 将排序; [12] + [10 15 18] 将排序; [3 12 17 22] + [12 18 25] 将排序; [22 30] } 它只排序一次并将它们合并在一起。队列还没有完全排序。我想知道是否可以添加另一个函数来检查队列是否已排序。但是,还是不行。公共布尔 OrderCheck(){ int i = firstLoc;布尔检查 = 真; while(check == true && i numArray[i+1]) check = false;否则我++; } 返回检查;}
猜你喜欢
  • 1970-01-01
  • 2013-10-28
  • 2012-08-17
  • 1970-01-01
  • 2019-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多