Docsity

1649 Data structure & algorithm ASM1 Merit, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
Contents
CONTENTS______________________________________________________________________________________________
1
FIGURE ________________________________________________________________________________________________
2
TABLE _________________________________________________________________________________________________
3
P1 + M1 CREATE A DESIGN SPECIFICATION FOR DATA STRUCTURES EXPLAINING THE VALID OPERATIONS THAT CAN BE
CARRIED OUT ON THE STRUCTURES. ________________________________________________________________________
4
1. ADT
____________________________________________________________________________________________ 4
2. STACK ADT _______________________________________________________________________________________
4
2.1. What is stack? ______________________________________________________________________________
4
2.2. Operations on Stacks _________________________________________________________________________
5
2.3. Application of Stack Data Structure ______________________________________________________________
9
2.4. Application of Stack in real life (geeksforgeeks, 2023)
__________________________________________________ 10
3. QUEUE ADT _____________________________________________________________________________________
10
3.1. What is Queue? ____________________________________________________________________________
10
3.2. Operations on Queue ________________________________________________________________________
10
3.3. Application of Queue Data Structure (geeksforgeeks, 2023) _________________________________________
14
3.4. Applications of Queue in Networks (geeksforgeeks, 2023) __________________________________________
14
P2 DETERMINE THE OPERATIONS OF A MEMORY STACK AND HOW IT IS USED TO IMPLEMENT FUNCTION CALLS IN A
COMPUTER. ___________________________________________________________________________________________
14
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a

Partial preview of the text

Download 1649 Data structure & algorithm ASM1 Merit and more Exams Data Structures and Algorithms in PDF only on Docsity!

Contents

CONTENTS______________________________________________________________________________________________ 1

FIGURE ________________________________________________________________________________________________ 2

TABLE _________________________________________________________________________________________________ 3

P1 + M1 CREATE A DESIGN SPECIFICATION FOR DATA STRUCTURES EXPLAINING THE VALID OPERATIONS THAT CAN BE CARRIED OUT ON THE STRUCTURES. ________________________________________________________________________ 4

  1. ADT ____________________________________________________________________________________________ 4
  2. STACK ADT _______________________________________________________________________________________ 4 2.1. What is stack? ______________________________________________________________________________ 4 2.2. Operations on Stacks _________________________________________________________________________ 5 2.3. Application of Stack Data Structure ______________________________________________________________ 9 2.4. Application of Stack in real life (geeksforgeeks, 2023) __________________________________________________ 10
  3. QUEUE ADT _____________________________________________________________________________________ 10 3.1. What is Queue? ____________________________________________________________________________ 10 3.2. Operations on Queue ________________________________________________________________________ 10 3.3. Application of Queue Data Structure (geeksforgeeks, 2023) _________________________________________ 14 3.4. Applications of Queue in Networks (geeksforgeeks, 2023) __________________________________________ 14

P2 DETERMINE THE OPERATIONS OF A MEMORY STACK AND HOW IT IS USED TO IMPLEMENT FUNCTION CALLS IN A COMPUTER. ___________________________________________________________________________________________ 14

  1. STACK MEMORY ___________________________________________________________________________________ 14
  2. HOW IT IS USED TO IMPLEMENT FUNCTION CALLS IN A COMPUTER __________________________________________________ 15

M2 SORTING ALGORITHMS. ______________________________________________________________________________ 19

  1. WHAT IS SORTING ALGORITHMS _________________________________________________________________________ 19
  2. SELECTION _______________________________________________________________________________________ 21
  3. QUICK SORT ______________________________________________________________________________________ 25

P3 USING AN IMPERATIVE DEFINITION, SPECIFY THE ABSTRACT DATA TYPE FOR A SOFTWARE STACK. _________________ 31

  1. INTRODUCTION TO FORMAL SPECIFICATION, TYPES OF FORMAL SPECIFICATION LANGUAGES (E.G., AXIOMATIC SPECIFICATIONS, VDM)____ 31
  2. DESCRIBE WHAT ARE PRE-CONDITION, POST-CONDITION, ERROR-CONDITION __________________________________________ 32
  3. SPECIFY STACK’S OPERATIONS USING THIS FORMAL SPECIFICATION LANGUAGE __________________________________________ 32 M3 EXAMINE THE ADVANTAGES OF ENCAPSULATION AND INFORMATION HIDING WHEN USING AN ADT. _____________ 35 REFERENCES ___________________________________________________________________________________________ 39

Figure

FIGURE 1: ABSTRACT DATATYPE (GEEKSFORGEEKS, N.D.) ________________________________________________________________ 4 FIGURE 2: STACK ADT (TUTORIALSPOINT, N.D.) ______________________________________________________________________ 5 FIGURE 3: STACK PUSH METHOD ________________________________________________________________________________ 6 FIGURE 4: STACK POP METHOD _________________________________________________________________________________ 7 FIGURE 5: STACK PEEK METHOD _________________________________________________________________________________ 8 FIGURE 6: STACK EMPTY METHOD _______________________________________________________________________________ 8

FIGURE 26: :EXAMPLE OF QUICK SORT (2) _________________________________________________________________________ 28 FIGURE 27: EXAMPLE OF QUICK SORT (3) _________________________________________________________________________ 29 FIGURE 28:EXAMPLE OF QUICK SORT (4) __________________________________________________________________________ 29 FIGURE 29: SINGLY LINKED LIST ________________________________________________________________________________ 38 FIGURE 30: WRITE-ONLY _____________________________________________________________________________________ 39 FIGURE 31: READ-ONLY _____________________________________________________________________________________ 39

Table

TABLE 1: PUSH(INT DATA) FUNCTION _____________________________________________________________________________ 5 TABLE 2: POP(): INTEGER _____________________________________________________________________________________ 6 TABLE 3: PEEK(): INTEGER _____________________________________________________________________________________ 7 TABLE 4: ISEMPTY(): BOOLEAN _________________________________________________________________________________ 8 TABLE 5: SIZE(): INT _________________________________________________________________________________________ 9 TABLE 6: ENDQUEUE(INT NUMBER) FUNCTION ______________________________________________________________________ 11

TABLE 7: DEQUEUE() _______________________________________________________________________________________ 11 TABLE 8: ISEMPTY _________________________________________________________________________________________ 12 TABLE 9: PEEK()___________________________________________________________________________________________ 13 TABLE 10: COMPARE SELECTION SORT AND QUICK SORT _______________________________________________________________ 21 TABLE 11:PSEUDOCODE OF SELECTION SORT _______________________________________________________________________ 21

P1 + M1 Create a design specification for data structures

explaining the valid operations that can be carried out on the

structures.

1. ADT

An object's behavior is determined by a set of values and a set of actions, and they are referred to as the abstract data type (ADT) or class. The ADT specification merely specifies the tasks that must be completed; it makes no statement of how they will be carried out. It is unclear what algorithms will be applied to carry out the operations and how the data will be arranged in memory. Because it provides an implementationindependent view, it is referred to be "abstract." (Geeksforgeeks, n.d.)

Figure 1: Abstract Datatype (Geeksforgeeks, n.d.)

Return The function will return true if the add is successful and fail if the add is not successful Table 1: Push(int data) function

The push method checks whether the stack is empty or not. If it is empty then it places the new element as the top. If not empty, it creates a new node and assigns the element to be added to that node, then assigns the new node to the top of the stack and updates the top of the stack to point to the new node.node.

Figure 3: Stack push method

Explanation: First, check if the list is empty or not. Given that there are two entries, we must append the parameter value of 91 to the beginning of the list. Consequently, the list's beginning now has the new value of 91.

b. Pop()

Pop(): Integer Purpose Delete the element at the top of the stack Parameters No parameter Return If the list is null return null, if the list is not null method will return the deleted value Table 2: Pop(): Integer

Check if the stack is empty or not. If empty, return null. If not empty, then access the element at the top of the stack and store it in a temporary variable. Then, remove that element from the stack, and update the top of the stack to point to the next element. Returns a temporary variable containing the deleted element.

Figure 4: Stack pop method

Explanation: First, check if the list is empty or not. As you can see, there are now three elements in the list. The first element will be deleted and 3 will be returned

c. Peek

Peek(): Integer Purpose Get the value from the top element of the stack Parameters No parameter Return If the list is null return null, if the list is not null return the value of top Table 3: Peek(): Integer

Check if the stack is empty or not. If empty, then return null. If not empty, then access the element at the top of the stack and return it.

Figure 5: Stack peek method

Return If the list has no elements, return 0. If there are, return the value of the counter variable count. Table 5: size(): int

First initialize a counter variable and assign it to 0. Initialize a pointer and assign it to the top of the stack. Repeat until the pointer is null: Increase the counter variable by 1. Update the pointer to the next element in the stack. Returns the counter variable.

Figure 7: Stack size method

Explanation: The image on the left, because the list has 2 elements, will return the value 3. The remaining image, because it has no elements, will return 0.

2.3. Application of Stack Data Structure

  • Function calls and recursion : When a function is called, the program's current state is pushed into the stack. The state is removed from the stack to allow the prior function to continue running after the function returns. (geeksforgeeks, 2023)
  • Undo/Redo operations : Stacks are used by many programs' undo-redo features to maintain track of prior actions. Every action that is taken is added to the stack. The action can be undone by popping the top element of the stack and carrying out the opposite action. (Geeksforgeeks, n.d.)
  • Expression evaluation : Expressions in infix, postfix, and prefix notations are evaluated using the stack data structure. Pushing operators and operands into the stack allows actions to be carried out based on the elements at the top of the stack. (Geeksforgeeks, n.d.)
  • Browser history : To record the webpages you visit, web browsers employ stacks. The URL is added to the stack each time you visit a new page, and when you use the back button, the previous URL is removed from the stack. (Geeksforgeeks, n.d.)
  • Balanced Parentheses: To determine whether or not parentheses are balanced, utilize the stack data structure. A closing parenthesis is removed from the stack after an opening parenthesis has been pushed onto it. The parentheses are balanced if the stack is empty at the conclusion of the expression. (Geeksforgeeks, n.d.)

2.4 Application of Stack in real life (geeksforgeeks, 2023)

  • CD/DVD stand.
  • Stack of books in a book shop.
  • Call center systems
  • Text editors have an undo and redo capability.
  • An array is used to store a web browser's history.
  • In any gallery, call logs, emails, and Google images are also kept in the form of a stack.
  • Notifications and YouTube downloads are likewise shown in LIFO order, with the most recent content appearing first.
  • RAM that is allocated by an operating system during the course of a process.

3. Queue ADT

3.1. What is Queue?

A queue is a linear ADT that is limited to allowing insertions at one end and deletions at the other. It operates according to the first-in, first-out (FIFO) principle. As a result, the element added first is also the element that is initially taken out of the queue. (Datta, 2023)

3.2. Operations on Queue

Enqueue : Add an element to the back of the queue.

Dequeue : Remove the element at the front of the queue.

Peek : Return the element at the front of the queue without removing it.

isEmpty : Check if the queue is empty.

Return The function will return the deleted value after deletion. Table 7: deQueue() The deQueue function removes the item at the head of the queue and, should the deletion be successful, returns the item. If delete fails for any reason—for example, an empty queue—Null is returned. The item that is in front will be the one that is deleted.

Figure 9: deQueue

Explanation: When they finish buying food, they will leave the cave and delete the id number they just left on the line. By calling the deQueue() function to delete and return the newly deleted value. c. isEmpty()

isEmpty(): boolean Purpose Check if the queue is empty Parameters No parameter Return It returns true if the list is empty and false otherwise. Table 8: isEmpty

The isEmpty method returns true if there are entries in the queue and false otherwise. The isEmpty method looks for items in the queue to test if it is empty.

Figure 10: isEmpty

Explanation: returns true if there are no food buyers in the queue. Returns false in case someone buys food. d. peek()

peek(): Integer Purpose Get the value of the front of the queue without removing it Parameters No parameter Return The function shall return data at the top of the stack after obtaining data. Table 9: peek()

The front value in the queue will be returned by the peek method if it is successful; null will be returned if the queue is empty. The function does not alter the Queue.

Figure 11: peek

Explanation: The getFront() function returns the top id number of the food buyer. If the list is empty, it returns null. In the illustration you can see that when calling the function, it returns 98.

3.3. Application of Queue Data Structure (geeksforgeeks, 2023)

  • Task Scheduling: Tasks can be scheduled using queues according to their priority or the order that they were received.
  • Resource Allocation: Resources, like printers or CPU processing time, can be managed and distributed using queues.
  • Batch Processing: Tasks like data analysis and picture rendering can be completed in batches using queues.
  • Pop operation: Pop operation refers to the removal of an element. Again, since we only have access to the element at the top of the stack, there’s only one element that we can remove. We just remove the top of the stack.

2. How it is used to implement function calls in a computer

A call stack is composed of stack frames (also called activation records or activation frames). These are machine dependent and ABI-dependent data structures containing subroutine state information. Each stack frame corresponds to a call to a subroutine which has not yet terminated with a return (wikipedia, 2023).

Return address : The address in memory of the calling function's instruction that controls should go back to once the called function has finished.

Local variables: Reserving space for variables unique to the function that is invoked.

Parameters : The values or references to the input arguments of the function. Additional details about bookkeeping.

Pushing data for a Function Call : Upon calling a function, all required information is pushed onto the stack, such as the function-specific variables and return address. In order for the computer to know where to start running again after the called function has finished, it is necessary to store the return address from the calling function. Function Execution : When a function is called, its stack frame contains the data that is used to hold local variables and parameters. Additionally, it has the ability to call its own functions, adding new stack frames for those operations.

Data Popping Following Function Call : Following the completion of a function's execution, the data related to that function call is removed off the stack. By removing the stack frame, control is returned to the correct location in the calling code and memory is managed appropriately.

Nested Function Calls : On the memory stack, a stack of stack frames is produced when function calls are nested inside one another. Functions can call other functions in this way, and as each function finishes, its

stack frame is taken down from the top of the stack, enabling the program to go back to the call chain's preceding function.

Return to the Caller Function : The calling function's command directing where control should be taken over is indicated by the return address kept in the stack frame. This guarantees that operations continue within the appropriate framework.

Figure 12:Function calls in a computers For

instance:

Figure 13:Sourse code of recursively calculate the fatorial

Figure 15: Memory Stack

Figure 16: Memory Stack

M2 Sorting Algorithms.

1. What is sorting algorithms

An array or list of elements can be rearranged using a sorting algorithm based on a comparison operator applied to the elements. The new element order in the corresponding data structure is determined by the comparison operator. (geeksforgeeks, 2023)

Some popular methods :

  • Selection sort: is a quick and easy sorting method that functions by repeatedly shifting the lowest (or largest) element from the list's unsorted section to the sorted section. (geeksforgeeks, n.d.)
  • Merge sort: is a sorting method that breaks an array up into smaller subarrays, sorts each subarray separately, and then combines the sorted subarrays back into the original array to create the final sorted array. (geeksforgeeks, n.d.)
  • Quick sort: is a sorting method that breaks an array up into smaller subarrays, sorts each subarray separately, and then combines the sorted subarrays back into the original array to create the final sorted array. (geeksforgeeks, n.d.)

Discuss the purpose of sorting

Sorting is the process of arranging items systematically, usually in ascending or descending order, based on certain criteria such as numerical, alphabetical, or any defined characteristic. Its purpose is multifaceted and serves various practical applications across different domains:

  • Search Efficiency: Sorted data allows for faster search operations. Algorithms like binary search can be used efficiently on sorted data, reducing the time complexity significantly.
  • Data Retrieval : When data is sorted, retrieving specific information becomes easier and quicker. For example, in a phonebook, locating a name becomes faster when entries are sorted alphabetically.
  • Improved Performance : Sorting enhances the performance of various algorithms and operations. Many algorithms perform better on sorted data, leading to more efficient computations and reduced resource consumption.
  • Better Visualization: Sorted data is often easier to comprehend and visualize, especially in graphs and charts. It can reveal patterns, trends, or anomalies more effectively.
  • Enhanced User Experience : In user interfaces, sorted lists or arrays make it simpler for users to navigate and find information. For instance, sorting product listings on e-commerce websites by price or popularity aids in decision-making.