























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An in-depth exploration of abstract data types (adts), with a focus on the stack adt and its applications in computer systems. It delves into the working mechanism of the stack adt, including its push and pop operations, and how it is utilized in the context of function calls. Additionally, the document compares the performance of two popular sorting algorithms, bubble sort and quick sort, highlighting their time complexities and advantages/disadvantages. The document also discusses the benefits of encapsulation and data hiding when using adts, emphasizing the importance of accessors and mutators in controlling public access to the adt's internal data. Overall, this document serves as a comprehensive resource for understanding the fundamental concepts of adts, their practical applications, and the comparison of sorting algorithms, making it a valuable asset for students and professionals in the field of computer science and data structures.
Typology: Assignments
1 / 31
This page cannot be seen from the preview
Don't miss anything!
























Qualification BTEC Level 5 HND Diploma in Computing Unit number and title Unit 19 : Data Structures and Algorithms Submission date 26/02/2022 Date Received 1st submission Re-submission Date Date Received 2nd submission Student Name Student ID Class Assessor name Student declaration I certify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism. I understand that making a false declaration is a form of malpractice. Student’s signature Grading grid P1 P2 P3 M1 M2 M3 D1 D
Grade: Assessor Signature: Date: Internal Verifier’s Comments: IV Signature:
Figure 23: Quick Sort Code implementation .............................................................................................................. 23 Figure 24: Quick sort example .................................................................................................................................... 24 Figure 25: Encapsulation Illustration .......................................................................................................................... 28 A. INTRODUCTION In this assignment, there are some tasks I need to complete such as creating a design specification for a data structure that consists the valid operations. Following that, I will demonstrate memory stack operations and how they are applied in computers to handle function calls. Additionally, I will describe the abstract data type for a software stack using an imperative description. To more specific, taking an example to introduce the concept of data structure for a First In First Out (FIFO) queue. The performance of two sorting algorithms will next be compared and discussed: bubble sort and quick sort. In the end, discussing about the benefits of encapsulating and concealing data with ADT.
b) Stack operations and working mechanism: Some particular operation terms are given to the two changes that can be made to a stack. For simplicity, assume the data parameter is the integer data type: Push(int): When an element is added to the top of a stack, this operation is called “push”. The “push” operation inserts and stores the element parameter onto the top of the stack. Each stack also has a fixed capacity; hence, the operation “push” could be performed if the stack is not full. In case the stack is full and the push operation is still called to insert an element, this causes stack overflow and the mechanism of push operation will throw a “stack overflow error” exception. When pushing an element successfully, the push mechanism returns the value of the inserted element. Int Pop(): When an element on the top is deleted from the stack, this operation is called “pop”. Figure 2 : Push operation illustration Figure 3 : stack overflow Figure 4 : Pop operation illustration
The “pop” operation removes and returns the current last(top) element(or the element which is most recently added) in the stack. This working mechanism implicitly describes the order of the element in/out(the last inserted element is also the first removed element). If the stack is empty but the “pop” method is still called, this will cause stack underflow. When this case happens, the underflow condition of the “pop” method will immediately throw “nullpointerexception” or other “RuntimeException”. “pop” operation returns the integer value of the removed element. Int PeekTop(): The “PeekTop” operation retrieves and returns the top(last) integer element in the stack without deleting this one like the “pop” operation. As the “Pop” operation, If the stack is empty but the “PeekTop” method is still called, this will cause stack underflow. When this case happens, the underflow condition of the “PeekTop” method will immediately throw “nullpointerexception” or other “RuntimeException”. Boolean isEmpty(): When other operations (“pop”, “peek top”,…) always need to check the number of existing elements in the stack(empty or not) to decide to perform algorithms or throw an error. “isEmpty” will be in charge of this task. Figure 5 : PeekTop Opeartion Illustration Figure 6 : isEmpty operation illustration
1. Definition of Stack Memory The execution of a thread using Stack memory. Stack memory mainly stores the function calls, primitive local variables and references to other objects in the heap that the method is referring to as well as temporary method-specific variables. Stack is one of the significant stack ADT applications, it always refers to storing data in LIFO(last in first out) order (Kamil, 2019). To store local primitive values and references to other objects(such as an array, String,…) in the function calls, a new stack frame is always created for each method call in the stack memory. When the function call is complete, the stack frame is popped out and terminated. The size of each stack is limited based on OS. 2. Operations of Stack Memory Because the data is inserted and deleted in a last-in-first-out manner based on function call, It has two principal operations: Push: Storing the new data onto the top of stack memory. Pop: Restoring the last data on the top of stack memory. Peek: Access and retrieve the last data on the top of stack memory. 3. How it is used to implement function calls in a computer Before talking about the implementation of function calls, there is a concept needing to be defined and this is the activation record. An activation record, which contains the space for all of the function's parameters and local variables, temporary objects, the return address, and other things the function needs, is where most implementations keep the data for a function call as a whole (Kamil, 2019). When a function is called, the activation record is created immediately and the stack memory mechanism will push it to the top of the stack in this thread(Pushing). In this case, the activation record now could be called the stack frame(or block; in other words, the stack frame is an implementation of the activation record). The first activation record(stack frame) to be terminated is the most recent one that has been created. For more specific, there is a particular example of the function call:
I have a code screen like this one: Here are the illustration of stack memory and their explanations. Illustration Explain When running the program, the main() function is called first, hence an activation record(stack frame) is created and pushed to the top of the stack. The variable reqCapital(primitive type) is stored in this stack frame space with the value 1000 In the main() function, it calls the getCapital() function; so, the new unique stack frame is created and pushed on top of the previous stack frame. The parameter c is stored temporarily in this stack frame space. Figure 11 : Code Example
Summary of the working process of how the stack memory implements function calls: **D. Queue ADT (FIFO)
DeQueue : When the element is removed from the queue, this operation is called “DeQueue” DeQueue removes the first element(which has been first inserted into the queue. In other words, the located at the first place in the queue) sequently with each inserted element. This operation only is used when the Queue has at least one element; In other words, when the queue is not empty, DeQueue could be executed. If the queue does not have any element (empty queue) and the dequeue operation is still called to delete the first element, this causes queue underflow and the program of the dequeue operation will throw a “queue underflow error” exception. When the Dequeue completes deleting the first element, it will return this value of this element. b) Other operations: PeekHead : This operation also returns the first element without deleting it. As the “DeQUeue” operation, If the queue is empty but the “PeekHead” method is still called, this will cause queue underflow. When this case happens, the underflow condition of the “PeekHead” method will immediately throw “nullpointerexception” or other “RuntimeException”. QueueSize : QueueSize returns the existed number in queue. Figure 14 : DeQueue Illustration Figure 15 : PeekHead illustration Figure 16 : QueueSize operation
E. Compare the performance of two sorting algorithms.
1. Introduction a) Definition: A sorting algorithm performs the sorting on all components of a list(array,..) in a certain order, either in ascending or descending order. In other words, the actual output after applying the sorting algorithm is the permutations or reorderings of the input in a certain order (Karumanchi, 2017). The necessary of the sorting algorithms: Sorting is viewed as one of the essential types of algorithms in computer science. The sorting algorithm can appreciably lower the complexity of a problem(such as sorting before performing a searching algorithm will reduce the time complexity) and is often used for database algorithms and searches. b) Some common sorting algorithms: Here are some sorting algorithms that can be introduced such as Selection Sort, Bubble Sort, Insertion Sort, Quick Sort, Merge Sort, and Heap Sort,…. However, this report just dives into two sorting algorithms and they are Bubble Sort and Quick Sort. c) Chosen Sorting Algorithms: The reason why they are chosen: Bubble Sort and Quick Sort are chosen for diving into the concepts of sorting algorithms because they are viewed as the representation of the simple and complex levels of the sorting algorithm. Bubble sort is the simplest sorting algorithm, its mechanism(including compare, swap, and iteration) indicates the basic foundation and the base logic of all other sorting algorithms. This one also is the best practice for everyone who wants to start learning sorting algorithms. Figure 18 : Sorting algorithm
Quick sort is the representative sorting algorithm that applies the divide-and-conquer algorithmic technique. It also is the sorting algorithm that gets most widely used in many programs. Its partitioning mechanism brings about parallelizable logic. Compare the chosen sorting algorithms: Bubble sort: The list(or array,…) is traversed repeatedly until this one is sorted. The name of the comparison sort method refers to pushing the "bubble" of smaller or bigger entries to the end of the list (Toscano, 2015). The algorithm repeats comparing each couple of elements and swapping them if they are not in a certain order until it can traverse the complete collection without discovering any components that need switching places. Performance: In case the list is already sorted in a certain order(ascending or descending) with a small number of elements, bubble sort will examine that in the initial loop that no couple of elements need to be swapped and will then terminate immediately. Because it just needs to perform n-1 comparisons; therefore, the complexity now is O(n) and this also is the best case. Except for the above case, other cases such as the list is not already sorted or the huge amount of elements in the list(or array) also bring about Bubble sort the bad case than the average case with time complexity is O(n^2 ). Here is the summary of Bubble sort complexity: Worst case complexity: O(n^2 ) Best case complexity: O(n) Average case complexity: O(n^2 ) Worst case space complexity: O(1) auxiliary Advantages of Bubble sort: This is the basic and simplest algorithm, which is very suitable for newbies with the sorting algorithm. Easy and straightforward to implement with a short structure. Disadvantages of Bubble sort: Worst performance when compared with other sorting algorithms. Really bad handling with a large amount of data in a collection. Quick sort By dividing the input into two parts by choosing pivot, sorting them, and then recombining them, Quick sort is implied that each iteration operates in this manner (Patel, 2019).
Pseudocode: Public void bubleSort(list / array of items ) //this algorithm is declared as a function n = array.length; for i = 0 to loop-1 do: flag = false //flag is understood as a checker to somehow get the best case of bubble sort as well as enhance its performance. for j = 0 to loop-1 do: if array[j] > array[j+1] then // compare the contiguous elements swap( list[j], list[j+1] ) // swap them flag = true // flag is true informing that this loop time has performed the swap end if end for if(flag is false) then break // if no number was swapped that means the array is sorted now, break the loop. If just after one loop and flag is still false, it provides the best case of bubble sort end if end for end void
Flowchart of Bubble sort: Code implementation(Java): To implement this algorithm as a function, the argument of this function now is integer data type: Figure 19 : Bubble sort flow chart Figure 20 : Bubble sort Source code