Assignment 1 Data Structure and Algorithms (1649) (điểm chuẩn Merit) năm 2022 đại học GW, có references. Content: ADT, Stack ADT, Stack memory, Queue ADT, Sorting algorithms, Specific Language, Introduction to formal specification, types, VDM for Stack, Advantages of encapsulation and information hiding when using an ADT, Introduction encapsulation, Advantages of encapsulation and information hiding when using an ADT. P1 Create a design specification for data structures explaining the valid operations that can be carried out on the structures. P2 Determine the operations of a memory stack and how it is used to implement function calls in a computer. P3 Using an imperative definition, specify the abstract data type for a software stack. M1 Illustrate, with an example, a concrete data structure for a First In First out (FIFO) queue. M2 Compare the performance of two sorting algorithms. M3 Examine the advantages of encapsulation and information hiding when using an ADT.
ASSIGNMENT FRONT SHEET Qualification BTEC Level HND Diploma in Computing Unit number and title Unit 19: Data Structures and Algorithms Submission date 24/08/2022 Date Received 1st submission Re-submission Date 24/08/2022 Date Received 2nd submission Student Name Nguyen Manh Tung Student ID GCH200064 Class GCH0907 Assessor name Do Hong Quan 1.1 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 D2 Summative Feedback: Grade: Resubmission Feedback: Assessor Signature: Internal Verifier’s Comments: IV Signature: Date: Contents Introduction I II ADT Stack ADT Stack memory Queue ADT Sorting algorithms 10 III Specific Language 20 Introduction to formal specification, types 20 VDM for Stack 21 IV V ADT Advantages of encapsulation and information hiding when using an ADT 23 Introduction encapsulation 23 Advantages of encapsulation and information hiding when using an ADT 24 Conclusion 25 References list 25 List of figures Figure 1: Abstract data type model (GeeksforGeeks, 2022) Figure 2: Practical example for Stack Figure 3: push to stack Figure 4: pop() of stack Figure 5: peek() of stack Figure 6: empty() of stack Figure 7: search operation of stack Figure 8: Example to explain stack memory Figure 9: Example of stack memory (1) Figure 10: Example of stack memory (2) Figure 11:Real-world example for Queue Figure 12: enQueue of Queue Figure 13: deQueue of Queue Figure 14:isEmpty of Queue Figure 15: getFront of Queue Figure 16: search of Queue 10 Figure 17: Flowchart of selection sort 12 Figure 18: Example of selection sort (1) 12 Figure 19: Example of selection sort (2) 13 Figure 20: Example of selection sort (3) 13 Figure 21: Example of selection sort (4) 13 Figure 22: Source code of selection sort 14 Figure 23: Flowchart main of quick sorting 16 Figure 24: Flowchart quick function 16 Figure 25: Quick sort example (1) 17 Figure 26: Quick sort example (2) 17 Figure 27: Quick sort example (3) 18 Figure 28: : Quick sort example (4) 18 Figure 29: Quick sort example (5) 19 Figure 30: Quick sort example (6) 19 Figure 31: Input array 20 Figure 32: quick function 20 Figure 33: Basic types of VDM (Wikipedia, 2021) 21 Figure 34: Example of encapsulation for ADT (Bank system) 24 I Introduction Abstract data type is an important data type for programming, especially for beginners ADTs such as Stack, Queue will be introduced with the application of Stack, memory stack Algorithms are an important part of programming, it has many applications in system and software development Sorting is a type of algorithm that helps to sort lists in a certain order Selection sort and quick sort will be done in this report Then a specification language will be used for the stack, along with the reasons why encapsulation should be used for the ADT II ADT ADT According to Datta, an abstract data type (ADT) is a data type that helps to hide their complexity, users not need to care about its implementation, it also provides functions for users to easily interact with data types like add, delete, search (Datta, 2021) An ADT consists of a list of data (integers, strings, etc.), with functions to perform operations on that data or its own subset of data The purpose of ADT is to simplify and hide the complexity of the data type as well as make the functionality available to the user, bringing the complexity into the classes Figure 1: Abstract data type model (GeeksforGeeks, 2022) Simply put, it provides users with public functions to use and hides the complexity of handling those functions, users not need to re-program from scratch but just use the built-in functions This saves time, ADTs are always updated with corrections and optimizations over time Some popular ADT like List, Stack, Queue Stack ADT Before learning about Stack, the concept of Linear structure will be introduced A set of data has a linear structure, the elements will be arranged in a specified order, it has ends, bottom and top or left and right, can only access elements sequentially Elements are usually linked in a sequential manner and consume linear memory space Stack and Queue are linear data structures Stack is an ADT with top-down data structure, things like adding elements and removing elements are done on top of the Stack structure It is a linear data structure because the data set is ordered in ascending order, the first in first, the last in second, can only access the elements in sequence (it cannot be go directly from bottom to top) Stack's sorting principle is called LIFO (last in, first out) ie the element added to the end will be accessed first, then the element below it The practical example for Stack is a stack of disks, if you want to remove the bottom disk, you have to take it from the top, otherwise if you want to add a new one, you have to leave it on the top Figure 2: Practical example for Stack a Overview stack operations push (Element item): Add an item of data type Element to the top of the stack, return true (successful), false (failed) pop (): Remove an item from the top of the stack and return its value peek (): Get the value on the top of the stack empty (): Checks if the stack has an item, returns true if true, returns false if false search (Object s): Checks and returns the item's position in the stack b push(Element item) function How it works: The function will take the input value of an item of data type Element, and then check if the stack is full Finally, perform a push operation, the value will be added to the top position of the stack and push the previous element down, ie the new element will be the top of the stack Success returns true, failure (full stack limit or full memory, ) returns false Figure 3: push to stack After pushing onto the stack, becomes the top instead of 5, return true c pop() function How it works: The pop() function checks whether the stack is empty or not, if empty, returns null, if there is an element, deletes that element from the stack, the top element after deletion will be the element below Returns the deleted value Figure 4: pop() of stack Do pop() with stack S, the top element (5) will be removed, the return value will be The purpose of returning the deleted value is so that the user knows what the deleted value is or uses it for something else d peek() function How it works: The peek() function checks whether the stack is empty or not, if empty, returns null, if not, returns the top value of the stack This function does not change anything of the stack, it only has the purpose of returning the top value of the stack to the user Figure 5: peek() of stack Do peek() with stack S, the top element (1) will be return, stack S does not change e empty() function How it works: The empty() function will check if the stack is empty, if the stack has an item, return false, if the stack is null, return true This function does not change the stack For example: Stack S has an item of 9, returns false, ie the stack is not empty Figure 6: empty() of stack f search(Object s) How it works: The function will check if the stack is null or not, if null, return null, if there is an item, use equals to check the items of the stack against the input object If the condition is true, return the position of that item relative to the top (top = 1), if the condition is not satisfied with the whole stack, return -1 If there are multiple matching elements, take the element closest to the vertex Figure 7: search operation of stack Do search(9) with stack S, check the condition from the top down, at position = the condition is satisfied, stop and return position = This function does not change the stack g Application Stack ADT is commonly used in the system, its applications can be divided into types: hardware and software In which the application for hardware is built-in to the hardware, has a dedicated memory and is managed entirely by the system's hardware Software applications are controlled by software, often used to store variables while running the program Some practical applications: converting expressions to low-level languages, machine languages for computers to perform; store variables to use, hold information, program functions, active subfunctions, reverse the string (heap, stack) Stack memory Memory stack is a memory area of the system, it is used to store variables and functions when running the program, after the function ends, it will delete and free the memory cell that stores that function The main memory stack operations are push (push a variable, function into stack memory) and pop (delete position to release memory) Applications in storing memory cells for recursive functions, for example, calculating the results of the recursive multi function below, the variable result will be stored on the stack, the multi function (4) will also be stored on the stack to wait for the results after calculation Each recursive call will have an extra memory cell to store the multi function, when multi (1) will have a return value of 1, then calculate the value of Figure 8: Example to explain stack memory multi (2), multi (3) ,multi (4) and free the stack memory Finally return the value for the variable result at Main Figure 9: Example of stack memory (1) Figure 10: Example of stack memory (2) Queue ADT Like Stack, Queue is a linear data structure because items are stored in a specified order The difference of Queue compared to Stack is that the sorting principle, Queue's sorting principle is FIFO (first in, first out), ie the first items (front) will be processed first, the last items (rear) will be processed in the end Flowchart Figure 17: Flowchart of selection sort Example Array has element {9,5,11,2} Figure 18: Example of selection sort (1) At i = 0, = 0: j = 1: > => = j = j = 2: < 11 12 j = 3: > => = j = != i => swap => {2,5,11,9} Figure 19: Example of selection sort (2) At i = 1, = 1: j = 2: < 11 j = 3: < Figure 20: Example of selection sort (3) At i = 2, = 2: j = 3: 11 > => = j = != i => swap => {2,5,11,9} Figure 21: Example of selection sort (4) Implementation Data structure is used: Integer array has n elements, loop for Time complexity: The first loop always runs through (n-1) elements, the second loop always runs through (n-i-1) elements Hence, time complexity is O(n2) Space complexity: The generated memory consists of int variables like min, i, j, temp, occupying constant memory so the space complexity is O(1) Source code: 13 Figure 22: Source code of selection sort c Quick sort Step: Given array of type Integer with n elements, n > Rules for choosing pivot variables: The pivot variable is the axis of the array, it is the value in the middle of the array The pivot index is calculated using the formula: first index + (last index - first index)/2 Quick sort algorithm: The principle of quick sort is to sort elements that are smaller than the pivot before the pivot and larger than the pivot after the pivot In most cases, quick sort will split the list into small lists to sort until the sublists have only element left, then quick sort is complete Pass in parameters, the first index (0) and the last index (length - 1) of the array Step 1: Assign the first and last index to two variables i and j, respectively Use those two variables to find the pivot Step 2: Compare i = pivot, if false, increase i value by and repeat step 3, if true go to step Step 4: Compare the array at position j => = j = j = 2: < 11 12 j =... {2,5 ,11 ,9} Figure 19 : Example of selection sort (2) At i = 1, = 1: j = 2: < 11 j = 3: < Figure 20: Example of selection sort (3) At i = 2, = 2: j = 3: 11 > => = j = != i => swap => {2,5 ,11 ,9}... Figure 18 : Example of selection sort (1) 12 Figure 19 : Example of selection sort (2) 13 Figure 20: Example of selection sort (3) 13 Figure 21: Example