📜  使用堆栈对数组进行排序

📅  最后修改于: 2021-04-29 18:48:56             🧑  作者: Mango

给定一个元素数组,任务是使用堆栈对这些元素进行排序。

先决条件:堆栈

例子 :

Input :  8 5 7 1 9 12 10
Output : 1 5 7 8 9 10 12 
Explanation :
Output is sorted element set

Input :  7 4 10 20 2 5 9 1
Output : 1 2 4 5 7 9 10 20 

我们基本上使用临时堆栈对堆栈进行排序。然后,我们将排序后的堆栈元素放回数组中。

C++
// C++ program to sort an array using stack
#include 
using namespace std;
  
// This function return the sorted stack
stack sortStack(stack input)
{
    stack tmpStack;
  
    while (!input.empty())
    {
        // pop out the first element
        int tmp = input.top();
        input.pop();
  
        // while temporary stack is not empty
        // and top of stack is smaller than temp
        while (!tmpStack.empty() &&
                tmpStack.top() < tmp)
        {
            // pop from temporary stack and
            // push it to the input stack
            input.push(tmpStack.top());
            tmpStack.pop();
        }
  
        // push temp in tempory of stack
        tmpStack.push(tmp);
    }
  
    return tmpStack;
}
  
void sortArrayUsingStacks(int arr[], int n)
{
    // Push array elements to stack
    stack input;
    for (int i=0; i tmpStack = sortStack(input);
  
    // Put stack elements in arrp[]
    for (int i=0; i


Java
// Java program to sort an 
// array using stack
import java.io.*;
import java.util.*;
  
class GFG
{
    // This function return
    // the sorted stack
    static Stack sortStack(Stack input)
    {
        Stack tmpStack = 
                       new Stack();
      
        while (!input.empty())
        {
            // pop out the
            // first element
            int tmp = input.peek();
            input.pop();
      
            // while temporary stack is 
            // not empty and top of stack
            // is smaller than temp
            while (!tmpStack.empty() &&
                    tmpStack.peek() < tmp)
            {
                // pop from temporary 
                // stack and push it 
                // to the input stack
                input.push(tmpStack.peek());
                tmpStack.pop();
            }
      
            // push temp in
            // tempory of stack
            tmpStack.push(tmp);
        }
      
        return tmpStack;
    }
      
    static void sortArrayUsingStacks(int []arr, 
                                     int n)
    {
        // push array elements
        // to stack
        Stack input = 
                       new Stack();
        for (int i = 0; i < n; i++)
            input.push(arr[i]);
      
        // Sort the temporary stack
        Stack tmpStack = 
                       sortStack(input);
      
        // Put stack elements
        // in arrp[]
        for (int i = 0; i < n; i++)
        {
            arr[i] = tmpStack.peek();
            tmpStack.pop();
        }
    }
      
    // Driver Code
    public static void main(String args[])
    {
        int []arr = {10, 5, 15, 45};
        int n = arr.length;
      
        sortArrayUsingStacks(arr, n);
      
        for (int i = 0; i < n; i++)
            System.out.print(arr[i] + " ");
    }
}
  
// This code is contributed by 
// Manish Shaw(manishshaw1)


Python3
# Python3 program to sort an array using stack
  
# This function return the sorted stack
def sortStack(input):
    tmpStack = []
    while (len(input) > 0):
      
        # pop out the first element
        tmp = input[-1]
        input.pop()
  
        # while temporary stack is not empty
        # and top of stack is smaller than temp
        while (len(tmpStack) > 0 and tmpStack[-1] < tmp):
          
            # pop from temporary stack and
            # append it to the input stack
            input.append(tmpStack[-1])
            tmpStack.pop()
          
        # append temp in tempory of stack
        tmpStack.append(tmp)
      
    return tmpStack
  
def sortArrayUsingStacks(arr, n):
  
    # append array elements to stack
    input = []
    i = 0
    while ( i < n ):
        input.append(arr[i])
        i = i + 1
          
    # Sort the temporary stack
    tmpStack = sortStack(input)
    i = 0
      
    # Put stack elements in arrp[]
    while (i < n):
        arr[i] = tmpStack[-1]
        tmpStack.pop()
        i = i + 1
          
    return arr
  
# Driver code
arr = [10, 5, 15, 45]
n = len(arr)
  
arr = sortArrayUsingStacks(arr, n)
i = 0
  
while (i < n):
    print(arr[i] ,end= " ")
    i = i + 1
  
# This code is contributed by Arnab Kundu


C#
// C# program to sort an 
// array using stack
using System;
using System.Collections.Generic;
  
class GFG
{
    // This function return
    // the sorted stack
    static Stack sortStack(Stack input)
    {
        Stack tmpStack = new Stack();
      
        while (input.Count != 0)
        {
            // pop out the
            // first element
            int tmp = input.Peek();
            input.Pop();
      
            // while temporary stack is 
            // not empty and top of stack
            // is smaller than temp
            while (tmpStack.Count != 0 &&
                    tmpStack.Peek() < tmp)
            {
                // pop from temporary 
                // stack and push it 
                // to the input stack
                input.Push(tmpStack.Peek());
                tmpStack.Pop();
            }
      
            // push temp in
            // tempory of stack
            tmpStack.Push(tmp);
        }
      
        return tmpStack;
    }
      
    static void sortArrayUsingStacks(int []arr, 
                                     int n)
    {
        // Push array elements
        // to stack
        Stack input = new Stack();
        for (int i = 0; i tmpStack = sortStack(input);
      
        // Put stack elements in arrp[]
        for (int i = 0; i < n; i++)
        {
            arr[i] = tmpStack.Peek();
            tmpStack.Pop();
        }
    }
      
    // Driver Code
    static void Main()
    {
        int []arr = new int[] {10, 5, 
                               15, 45};
        int n = arr.Length;
      
        sortArrayUsingStacks(arr, n);
      
        for (int i = 0; i < n; i++)
        Console.Write(arr[i] + " ");
    }
}
  
// This code is contributed by 
// Manish Shaw(manishshaw1)


输出:
5 10 15 45

时间复杂度: O(n * n)