














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
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 02/07/2023 Date Received 1st submission Re-submission Date Date Received 2nd submission Student Name Student ID Class GCH1103 Assessor name Do Hong Quan 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 P 4 P 5 P 6 P 7 M 4 M 5 D3 D
Grade: Assessor Signature: Date: Internal Verifier’s Comments: IV Signature:
A stack is an abstract data type (ADT) that represents a collection of elements with a specific set of operations. It follows the Last-In- First-Out (LIFO) principle, where the most recently added element is the first one to be removed. (GeeksforGeeks, n.d.) To implement the stack, it is required to maintain the pointer to the top of the stack, which is the last element to be inserted because we can access the elements only on the top of the stack.
Push(E data): void Figure 1 : Push operation
isEmpty(): Boolean Figure 4 : isEmpty operation size(): int Figure 5 : size operation search(E element): int
Figure 6 : search operation
2. Queue
A queue is an abstract data type (ADT) that represents a collection of elements with a specific set of operations. It follows the First- In-First-Out (FIFO) principle, where the first element added is the first one to be removed. In a queue, elements are added to the rear (also known as the "back") and removed from the front (also known as the "front" or "head") of the queue.
Enqueue(E data): void
Figure 8 : Dequeue() operation getFront(): E Figure 9 : getFront() operation getRear(): E
Figure 10 : getRear() operation isEmpty(): boolean Figure 11 : isEmpty() operation size(): int Figure 12 : size() operation search(E element): int
Figure 14 : Error handling example 1 The code uses if/else statements to handle error conditions related to empty stacks or specific conditions within the code logic. In the pop() operation, an if condition is used to check if the stack is empty ( top == null ). If it is empty, null is returned to indicate the error condition. The code also includes a try/catch block to handle a NullPointerException that may occur during the pop() operation. If a NullPointerException is caught, the catch block executes, printing an error message to the console and returning null to indicate the error condition.
Figure 15 : Error handling example 2 The code uses an if/else statement to check if the queue is empty by calling the isEmpty() method. If the queue is empty, null is returned to indicate the error condition. The code includes a try/catch block to catch a NullPointerException. If a NullPointerException occurs during the execution of the method, the catch block is executed. It prints an error message to the console and returns null to indicate the error condition.
2. Testing plan
Enqueue(1) search(2) - > return index 0 7 Enqueue() Normal MyQueue
3. Evaluation
After 12 test plans, 8 out of 12 test plans were successful, indicating that the majority of the stack operations are functioning correctly. These successful test plans covered various aspects, including push(), pop(), size(), and isEmpty() operations. Beside that, 3 out of 12 test plans were failed. These failures were mainly related to the search operation, which only supported searching for integer values. Additionally, insufficient error handling was identified as another reason for the failures. Overall, I need to improve searching operation for different data types and enhancing error handling to provide reliable functionality.
Big-O notation is a mathematical notation used to describe the upper bound or worst-case scenario of an algorithm's time complexity or space complexity. It provides a way to analyze and compare the efficiency of different algorithms by focusing on their growth rates as the input size increases. The formal definition of Big O notation is as follows: f(n) = O(g(n)) if and only if there exist positive constants C and K such that 0 ≤ f(n) ≤ c · g(n) for all sufficiently large values of n. In other words, Big O notation says that the function f(n) is asymptotically bounded by the function g(n). This means that as the input size n gets very large, the two functions will become increasingly close. The Big-O notation is useful when we only have an upper bound on the time complexity of an algorithm. Many times we easily find an upper bound by simply looking at the algorithm. (GeeksforGeeks, 2023)
The above expression can be described as if f(n) is theta of g(n), then the value f(n) is always between c1 * g(n) and c2 * g(n) for large values of n (n ≥ n0). The definition of theta also requires that f(n) must be non-negative for values of n greater than n0. (GeeksforGeeks, 2023)
4. Example Figure 16 : Constant time complexity In this case, the time it takes to retrieve an element from the array does not depend on the size of the array. Regardless of how many elements are in the array, accessing a specific element directly using its index can be done in constant time. Figure 17 : Linear time complexity This method runs from 0 to arr.length. The time complexity of this algorithm is O(n), as the running time grows linearly with value of arr.
Figure 18 : Logarithmic time complexity The code in this example is logarithmic time because the number of times the code executes is proportional to the logarithm of the size of the input. In this case, the code executes once for each power of 2 less than or equal to n, so the code will take O(log n) time to run, where n is the input
Time complexity (Big-O notation): Time complexity represents the amount of time an algorithm takes to run, based on the input size. It characterizes the upper bound or worst-case scenario of an algorithm's time requirements. For example, an algorithm with a time complexity of O(n) will take a time proportional to the size of the input, while an algorithm with a time complexity of O(n^2) will take a time proportional to the square of the size of the input. Space Complexity (Big-O notation): Space complexity refers to the amount of memory or space required by an algorithm to solve a problem based on the input size. It characterizes the upper bound or worst-case scenario of an algorithm's space requirements. For example, an algorithm with a space complexity of O(1) will use a constant amount of memory, while an algorithm with a space complexity of O(n) will use a amount of memory proportional to the size of the input.