使用Stacks进行非递归合并排序

时间:2014-10-21 00:44:49

标签: java stack mergesort

我正在进行一项任务,我应该使用堆栈编写mergeSort!我对堆栈和mergesort如何工作有一个很好的想法,但是我不知道如何用堆栈完成我的代码。我已经设置了一个基本案例,在这里我试图使用就地方法,我理论上将我的数组分开,而不是通过更改标记创建一个新数组。我对此很陌生,不确定我该怎么办...... 我认为这就是我想要的东西:

基础案例 - > PC = 1

if (callStack.peek().PC == 1) {
            if (callStack.peek().start == callStack.peek().stop) {   //length <=1 
                callStack.pop(); //either done, or array length was 1
                merge(A, callStack.peek().start, callStack.peek().mid, callStack.peek().stop);

                if (callStack.empty()){
                    break;
                }
                callStack.peek().PC++;
            } else {
                callStack.peek().PC++;
            }
            continue;
        }`

任何左分割数组 - &gt; PC = 2 任何正确的分组数组 - &gt; PC == 3

int mid = (callStack.peek().stop-callStack.peek().start)/2;

        if (callStack.peek().PC == 2) {
            if (callStack.peek().start != callStack.peek().stop) {
            current = new ProgramFrame(callStack.peek().start, callStack.peek().mid, 1);
            callStack.push(current);
            continue;
            }
        }

        if (callStack.peek().PC == 3) {
            if (callStack.peek().start != callStack.peek().stop) {
            current = new ProgramFrame(callStack.peek().mid+1, callStack.peek().stop, 1);
            callStack.push(current);
            continue;
            }
        }

两者的合并 - &gt; PC == 4

if(callStack.peek()。PC == 4){                 merge(A,callStack.peek()。start,callStack.peek()。mid,callStack.peek()。stop);

            callStack.pop();

            if (!callStack.empty()) {
                if (callStack.peek().PC == 2) callStack.peek().start callStack.peek().mid; //help??
                if (callStack.peek().PC == 3) callStack.peek().mid+1, callStack.peek().stop; //help??
                callStack.peek().PC++;
                continue;

对不起,这是一篇很长的帖子:(我真的不确定如何修复它以及如何继续它......

**也合并,我的programeFrame看起来没问题,但是如果你需要看到它们我也可以发送它们!

0 个答案:

没有答案