Implement a Stack in Python Using Merge Sort

by Kal Bartal

Problem: Implement a stack data structure in Python using merge sort. A stack is a collection of elements, with two principal operations: push, which adds an element to the collection, and pop, which removes the most recently added element from the collection.

Your stack should be able to handle the following operations:

  • push(data): adds an element to the collection
  • pop(): removes the most recently added element
  • peek(): returns the most recently added element, but does not remove it
  • isEmpty(): returns True if the stack is empty and False otherwise

You must also implement a merge sort algorithm in order to sort the elements in the stack in ascending order.

Merge sort takes an unsorted list as an input and returns a sorted list.

Understanding the problem

This problem is about implementing a stack data structure in Python using the merge sort algorithm. A stack is a type of collection where elements can be added or removed in a particular order.

The two operations we perform on a stack are push and pop. Push adds an element to the collection, and pop removes the most recently added element. To implement these operations, you should create functions for push, pop, peek and isEmpty, allowing the user to use the stack in the expected way. 

The second part of this problem is to use the merge sort algorithm to sort the stack in ascending order. Merge sort is an efficient sorting algorithm which takes an unsorted list of elements and returns a sorted list. To implement the merge sort algorithm, you have to break the list down into smaller, sorted lists and then combine them to form a new sorted list. 

Ultimately, this problem aims to write an algorithm allowing a user to use a stack data structure and use merge sort to sort the stack in ascending order correctly.

Stack Data Structures & Merge Sort

  • Implementing a stack data structure in Python.
  • Understanding merge sort algorithm.
  • Accessing resources to learn the concepts.

To tackle this problem, you will need to understand the basics of implementing a stack data structure in Python and how the operations push, pop, peek and isEmpty should be implemented in the context of a stack. 

Also, you’ll need to understand the merge sort algorithm and how it works. Merge sort is an efficient sorting algorithm which recursively breaks down a list of elements into smaller and smaller sorted sub-lists before merging them together to form a new sorted list. 

To make this easier, there are a lot of resources available to help you better understand the stack data structure and the merge sort algorithm. So don’t worry if some of it seems hard to understand at first – just take your time and you will eventually get it.

Working with a Stack in Python

  • Last-in-first-out (LIFO) rule for a stack
  • Operations to manipulate the stack: Push, Pop, Peek and isEmpty
  • Using these functions to keep elements in the right order

When implementing a stack in Python, there are a few key things to note. Firstly, a stack is a collection of elements which are manipulated in a particular order – the last element added is the first element removed (this is known as last-in-first-out, or LIFO). To manipulate the stack, we have four key operations which can be used – push, pop, peek and isEmpty. 

Push adds an element to the top of the stack, which is known as ‘pushing’ the element onto the stack. Pop removes the element at the top of the stack, which is known as ‘popping’ it off the stack. Peek returns the element at the top of the stack, without removing it. Finally, isEmpty will return True if the stack is empty and False if it is not. 

By using these functions, we can manipulate the stack in the expected way and make sure that the elements are always in the correct order.

Merge Sort Algorithm 

  • Break list into smaller, sorted lists
  • Merge halves by comparing & adding smaller element to result list
  • Repeat until fully sorted, resulting in ascending order

Merge sort is an efficient sorting algorithm which is used to sort a list of elements into ascending order. It works by first breaking the list of elements down into smaller, sorted lists and then merging them together to create a new sorted list. 

The first step is to divide the list into two halves, and then recursively divide the two halves until each half is composed of only one element each. At this point, each half is sorted by definition, so the two halves can simply be merged together. To merge the two halves together, elements from each half are compared and the smaller element is added to the result list. This is repeated until there are no elements left in either of the original halves.

By repeating this process until the list is completely sorted, the elements will be in ascending order.

Stack Manipulation with Merge Sort

  • Defined Stack class with four functions
  • Implemented merge sort algorithm
  • Called sort() and isEmpty() functions

So first, I’ll explain the logic then I’ll go through the code step by step.

First of all, I have defined a Stack class with four functions—push, pop, peek and isEmpty. These functions allow the user to use the stack in the expected way. The push() function adds an element to the top of the stack and the pop() function removes the most recently added element from the top of the stack. The peek() function returns the most recently added element without removing it, and the isEmpty() function returns True if the stack is empty and False otherwise. 

Next, I have implemented a merge sort algorithm to sort the elements in the stack in ascending order. The merge sort algorithm works by breaking the list of elements down into smaller and smaller sorted sub-lists until each sub-list consists of only one element. Then the sub-lists are merged together in ascending order to form a new sorted list. To do this, the two halves of the list are compared and the smaller element is added to the result list. This process is repeated until there are no elements left in either of the original halves. 

Finally, I have called the sort() and isEmpty() functions. The sort() function uses the mergeSort() function to sort the elements in the stack in ascending order and the isEmpty() function returns True if the stack is empty and False otherwise.

Now let’s take a look at the code.

class Stack:
    def __init__(self):
        self.data = []

The “__init__” function defines the variables that will be used when creating an instance of the Stack class. This function instantiates the Stack class, and sets the data attribute to an empty list. 

    def push(self, value):
        self.data.append(value)

The “push” method adds a value to the Stack. This method takes a value as an argument and appends it to the data list attribute. 

    def pop(self):
        if len(self.data) == 0:
            return None
        else:
            return self.data.pop()

The “pop” method removes the last item that was added to the Stack. If there are no items in the list, it returns None. Otherwise, it pops off the last item and returns it.

    def peek(self):
        if len(self.data) == 0:
            return None
        else:
            return self.data[-1]

The “peek” method returns the value of the last item added to the Stack. If there are no items in the list, it returns None. Otherwise, it returns the last item added to the Stack.

    def isEmpty(self):
        return self.data == []

The “isEmpty” method checks if the Stack is empty and returns True or False.

    def sort(self):
        self.data = self.mergeSort(self.data)

    def mergeSort(self, data):
        #check for base case
        if len(data) == 1:
            return data
        #split in two
        mid = len(data)//2
        left = self.mergeSort(data[:mid])
        right = self.mergeSort(data[mid:])
        #merge two sorted halves
        return self.merge(left, right)

    def merge(self, left, right):
        result = []
        while len(left) > 0 or len(right) > 0:
            if len(left) > 0 and len(right) > 0:
                if left[0] <= right[0]:
                    result.append(left[0])
                    left = left[1:]
                else:
                    result.append(right[0])
                    right = right[1:]
            elif len(left) > 0:
                result.append(left[0])
                left = left[1:]
            else:
                result.append(right[0])
                right = right[1:]
        return result

The “sort” method sorts the items in the Stack by using the “mergeSort” and “merge” functions. The mergeSort function uses a divide-and-conquer approach to seperate the list into a left and right halves. The merge function then combines the sorted halves.

#test code
stack = Stack()
stack.push(3)
stack.push(2)
stack.push(7)
stack.push(4)
stack.sort()

while not stack.isEmpty():
    print(stack.pop())

Finally, a sample code shows how the Stack class can be used.

This code creates a Stack object, pushes 4 items to the Stack, calls the sort method to sort the items and then prints the items from the Stack.

Here is the code in one block

class Stack:
    def __init__(self):
        self.data = []

    def push(self, value):
        self.data.append(value)

    def pop(self):
        if len(self.data) == 0:
            return None
        else:
            return self.data.pop()

    def peek(self):
        if len(self.data) == 0:
            return None
        else:
            return self.data[-1]

    def isEmpty(self):
        return self.data == []

    def sort(self):
        self.data = self.mergeSort(self.data)

    def mergeSort(self, data):
        #check for base case
        if len(data) == 1:
            return data
        #split in two
        mid = len(data)//2
        left = self.mergeSort(data[:mid])
        right = self.mergeSort(data[mid:])
        #merge two sorted halves
        return self.merge(left, right)

    def merge(self, left, right):
        result = []
        while len(left) > 0 or len(right) > 0:
            if len(left) > 0 and len(right) > 0:
                if left[0] <= right[0]:
                    result.append(left[0])
                    left = left[1:]
                else:
                    result.append(right[0])
                    right = right[1:]
            elif len(left) > 0:
                result.append(left[0])
                left = left[1:]
            else:
                result.append(right[0])
                right = right[1:]
        return result

#test code
stack = Stack()
stack.push(3)
stack.push(2)
stack.push(7)
stack.push(4)
stack.sort()

while not stack.isEmpty():
    print(stack.pop())

Time Complexity

  • Time complexity of the code is O(n*log(n))
  • Other functions in code take O(1) time
  • Total time complexity of code is O(nlog(n)) + O(1) = O(nlog(n))

The time complexity of this code is O(n*log(n)). This is because the merge sort algorithm takes O(n*log(n)) time to complete, due to the fact that it needs to recursively divide the list into smaller and smaller sub-lists until each sub-list consists of only one element. This process requires O(log(n)) time, and since it needs to be done for each element in the list, the total time complexity is O(n*log(n)). 

In addition to this, the other functions in the code (push, pop, peek and isEmpty) all take O(1) time, as they perform constant time operations on the list. Therefore, the total time complexity of the code is O(n*log(n)) + O(1) = O(n*log(n)). 

Space Complexity

  • The code has a space complexity of O(n).
  • O(n) space is needed to create a merged list of the same size.
  • Stack requires O(n) extra space, resulting in O(n) total complexity.

The space complexity of this code is O(n). This is because the merge sort algorithm needs to create a new list by merging the two halves of the list, which has the same size as the original list, so we need extra O(n) space to store it. Additionally, the stack stores the elements, which again requires O(n) extra space. 

Therefore, the total space complexity of this code is O(n) + O(n) = O(n).

Understanding Stacks and Merge Sort in Python

  • Understand stack data structure and its operations in Python.
  • Look at resources and practice difficult parts.
  • Learn concepts in Python, data structures and merge sort.

Remember, the key is to understand the basics of implementing a stack data structure in Python and how the operations push, pop, peek and isEmpty should be implemented in the context of a stack. Also, make sure you understand the merge sort algorithm and how it works. 

Take some time to look at resources and practice any parts you have difficulty understanding. This is an interesting problem which will help you learn concepts in Python and data structures so keep going and don’t give up!

Related Posts

Leave a Comment