编写程序以升序对堆栈进行排序

Write a program to sort a stack in ascending order(编写程序以升序对堆栈进行排序)
本文介绍了编写程序以升序对堆栈进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮忙看看我的代码吗?非常感谢你的帮助.输入堆栈是 [5, 2, 1, 9, 0, 10],我的代码给出了输出堆栈 [0, 9, 1, 2, 5, 10],9 不在正确的位置.

Can someone help look at my code, please? Thank you so much for your help. The input stack is [5, 2, 1, 9, 0, 10], my codes gave output stack [0, 9, 1, 2, 5, 10], 9 is not in the right position.

import java.util.*;

public class CC3_6 {
public static void main(String[] args) {
    int[] data = {5, 2, 1, 9, 0, 10};
    Stack<Integer> myStack = new Stack<Integer>();
    for (int i = 0; i < data.length; i++){
        myStack.push(data[i]);
    }
    System.out.println(sortStack(myStack));
}

public static Stack<Integer> sortStack(Stack<Integer> origin) {
    if (origin == null)
        return null;
    if (origin.size() < 2)
        return origin;

    Stack<Integer> result =  new Stack<Integer>();
    while (!origin.isEmpty()) {
        int smallest = origin.pop();
        int remainder = origin.size();
        for (int i = 0; i < remainder; i++) {
            int element = origin.pop();
            if (element < smallest) {
                origin.push(smallest);
                smallest = element;                    
            } else {
                origin.push(element);
            }
        }
        result.push(smallest);
    }
    return result;

}

}

推荐答案

这是我的代码版本,非常简单易懂.

Here's my version of the code which is pretty straightforward to follow.

import java.util.Stack;

public class StackSorting {

    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<Integer>();

        stack.push(12);
        stack.push(100);
        stack.push(13);
        stack.push(50);
        stack.push(4);

        System.out.println("Elements on stack before sorting: "+ stack.toString());

        stack = sort(stack);

        System.out.println("Elements on stack after sorting: "+ stack.toString());

    }

    private static Stack<Integer> sort(Stack<Integer> stack) {

        if (stack.isEmpty()) {
            return null;
        }

        Stack<Integer> sortedStack = new Stack<Integer>();

        int element = 0;
        while(!stack.isEmpty()) {
            if (stack.peek() <= (element = stack.pop())) {
                if (sortedStack.isEmpty()) {
                    sortedStack.push(element);
                } else {
                    while((!sortedStack.isEmpty()) && sortedStack.peek() > element) {
                        stack.push(sortedStack.pop());
                    }
                    sortedStack.push(element);
                }
            }
        }

        return sortedStack;
    }
}

这篇关于编写程序以升序对堆栈进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How can create a producer using Spring Cloud Kafka Stream 3.1(如何使用Spring Cloud Kafka Stream 3.1创建制片人)
Insert a position in a linked list Java(在链接列表中插入位置Java)
Did I write this constructor properly?(我是否正确地编写了这个构造函数?)
Head value set to null but tail value still gets displayed(Head值设置为空,但仍显示Tail值)
printing nodes from a singly-linked list(打印单链接列表中的节点)
Control namespace prefixes in web services?(控制Web服务中的命名空间前缀?)