You are on page 1of 37

Higher Nationals in Computing

Unit 19: Data Structures and Algorithms


ASSIGNMENT 1

Learner’s name: Nguyen Minh Triet


ID: GCS210026
Class: GCS0905A
Subject code: 1649
Assessor name: Le Ngoc Thanh

Assignment due: 1 2 t h O c t o b e r 2 0 2 2
Assignment submitted: 1
ASSIGNMENT 1 FRONT SHEET
Qualification BTEC Level 5 HND Diploma in Computing

Unit number and title Unit 19: Data Structures and Algorithm

Submission date Date Received 1st submission

Re-submission Date Date Received 2nd submission

Student Name Nguyen Minh Triet Student ID GCS210026

Class GCS0905A Assessor name Nguyen Ngoc Tu

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: ❒ Resubmission Feedback:

Grade: Assessor Signature: Date:

Signature & Date:


Assignment Brief 1 (RQF)
Higher National Certificate/Diploma in Business

Student Name/ID Number: Nguyen Minh Triet / GCS210026

Unit Number and Title: Unit 19: Data Structures and Algorithms

Academic Year: 2021

Unit Assessor: Le Ngoc Thanh

Assignment Title: Examine and specify ADT and DSA

Issue Date:

Submission Date:

Internal Verifier Name:

Date:

Submission Format:

Format:

● The submission is in the form of an individual written report and a presentation. This should be written in a concise,
formal business style using single spacing and font size 12. You are required to make use of headings, paragraphs
and subsections as appropriate, and all work must be supported with research and referenced using the Harvard
referencing system. Please also provide a bibliography using the Harvard referencing system.

Submission

● Students are compulsory to submit the assignment in due date and in a way requested by the Tutor.
● The form of submission will be a soft copy posted on http://cms.greenwich.edu.vn/.
● Remember to convert the word file into PDF file before the submission on CMS.

Note:

● The individual Assignment must be your own work, and not copied by or from another student.
● If you use ideas, quotes or data (such as diagrams) from books, journals or other sources, you must reference your
sources, using the Harvard style.
● Make sure that you understand and follow the guidelines to avoid plagiarism. Failure to comply this requirement will
result in a failed assignment.

Unit Learning Outcomes:


LO1 Examine abstract data types, concrete data structures and algorithms

LO2 Specify abstract data types and algorithms in a formal notation

Assignment Brief and Guidance:

Assignment scenario

You work as in-house software developer for Softnet Development Ltd, a software body-shop providing network
provisioning solutions. Your company is part of a collaborative service provisioning development project and your company
has won the contract to design and develop a middleware solution that will interface at the front-end to multiple computer
provisioning interfaces including SOAP, HTTP, JML and CLI, and the back-end telecom provisioning network via CLI .

Your account manager has assigned you a special role that is to inform your team about designing and implementing abstract
data types. You have been asked to create a presentation for all collaborating partners on how ADTs can be utilised to
improve software design, development and testing. Further, you have been asked to write an introductory report for
distribution to all partners on how to specify abstract data types and algorithms in a formal notation.

Tasks
Part 1

You will need to prepare a presentation on how to create a design specification for data structures, explaining the valid
operations that can be carried out on the structures using the example of:

1. A stack ADT, a concrete data structure for a First In First out (FIFO) queue.
2. Two sorting algorithms.
3. Two network shortest path algorithms.
Part 2

You will need to provide a formal written report that includes the following:

1. Explanation on how to specify an abstract data type using the example of software stack.
2. Explanation of the advantages of encapsulation and information hiding when using an ADT.
3. Discussion of imperative ADTs with regard to object orientation.
Learning Outcomes and Assessment Criteria (Assignment 1)

Pass Merit Distinction

LO1 Examine abstract data types, concrete data structures and


algorithms
D1 Analyse the operation,
P1 Create a design M1 Illustrate, with an example, a using illustrations, of two
specification for data structures concrete data structure for a First network shortest path
explaining the valid operations In First out (FIFO) queue. algorithms, providing an
that can be carried out on the M2 Compare the performance of example of each.
structures. two sorting algorithms.

P2 Determine the operations of


a memory stack and how it is
used to implement function
calls in a computer.

LO2 Specify abstract data types and algorithms in a formal notation

P3 Using an imperative M3 Examine the advantages of D2 Discuss the view that


definition, specify the encapsulation and information imperative ADTs are a basis
abstract data type for a hiding when using an ADT. for object orientation and,
software stack. with justification, state
whether you agree.
Table of Contents
Part 1:

Part 2:

References
ASSIGNMENT 1 ANSWERS

Part 1: Data structures

P1: Abstract Data Type

This section will introduce the definition along with examples for abstract data type

1. Definition

Before diving into abstract data type, let’s first discuss the concept of “Abstraction”. It is one of the
main concepts of object-oriented programming (OOP) paradigm. The main goal of this concept is to
handle complexity which is achieved by hiding unnecessary details from the user. Which in turns allow
the user to implement complex logic on top of the provided abstraction without having to think about
the hidden complexity. The concept is not only limited to OOP but it can be found everywhere in real
world. (See Refs. [1])

Data types such as int, float, double are built-in data types which we can perform basic operations like
addition, subtraction, division, multiplication. But there may be situations when we need operations
on the data types that we defined, these operations can only be defined when and as we require
them. So, to simplify this process, we can create data structures with their operations, and these non-
built-in data structures are known as Abstract Data Type (ADT). ADT is a type of objects whose
behaviour is defined by a set of values and a set of operations. Definitions of ADT only contains what
operations can be performed but not how these operations will be implemented. It does not
determine how data will be organized in memory and what algorithms are needed for the
implementation. A representation of ADT is shown in Figure 1.

Page |1
Figure 1: Abstract Data Type

An ADT consists of declaration of data and operation as well as encapsulation of data and operations.
It is done so that data is hidden from users and can only be manipulated by using operations. An ADT
can be represented as a set of objects sharing the same properties and behaviors with properties
being its data such as “double d;” and behaviors being its operations or functions such as “sqrt(d) /
2;”. Therefore, an ADT couples its data and operations and OOP emphasizes data abstraction. (See
Refs. [2])

An ADT is a formal description of a data structure but not includes the code of that structure, making it
independent of any programming language. Code independence is a very great practice because the
determined logic can be implemented in any programming language. And the same logic can be used
in different languages and still produce the same desired output. ADT also promotes design by
contract which specifies responsibilities of suppliers and clients clearly so that those responsibilities
can be enforced when required. Three common representations of ADT are List ADT, Stack ADT, and
Queue ADT.

Page |2
The process of using ADT from definition to implementation is demonstrated below.

Figure 2: System design process with ADT

After identifying the problem, the next step will be to identify data or properties of ADTs, then the
operations are specified. These identification steps can generate the outcome as shown in Figure 3
below.

Figure 3: Identification of data and operations

After having a complete ADT specification, the process will move on to specify ADT interactions and
the last step will be to implement the ADTs. Following this process will generate benefits such as ease
in modification and maintenance, profit, and reusability for manufacturer (the entity that creates
ADT). For the client who uses the ADT can also benefits with understanding, familiarity, and cost.

Page |3
2. Examples

Figure 4: Abstraction example (See Refs. [3])

A real-world example of abstraction concept is about a person who is addicted to coffee. Everyday,
that person wakes up in the morning and switch on the coffee machine to make coffee. That person
only needs to know how to use the coffee machine like put water and coffee beans in, switch on and
choose the operating mode. However, the person does not need to know how the coffee machine is
operating internally nor the ideal water temperature or the amount of coffee needed to make one
cup. Someone else created the coffee machine with all the internal logic and now it acts as an
abstraction and will hide all the complex details. Thanks to this, the person can interact with a simple
interface without needing any knowledge. When we compare this example with programming,
abstraction means that the programmer only knows what a data type can do but how it is
implemented is hidden. (See Refs. [1])

We can take floating point numbers as an example for ADT because we do not need to know how

Page |4
floating-point arithmetic works to be able to use float data type. The details can vary depending on the
processor but the compiler hides all those details so users can use float as a built-in ADT. This is the
advantage of being programming language independent as no matter what language the user uses,
the compiler will show float as one of the built-in data types. All that users need to know is the syntax
and meaning of operators like addition (+), subtraction (-), multiplication (*). Hiding the details of
implementation is called encapsulation or data hiding.

Another example of ADT is array ADT, the data of an array ADT are included in Figure 5 below.

Figure 5: Data on Array ADT (See Refs. [4])

Some basic operations on the Array ADT are shown in Figure 6 below and we can also perform more
operations on this data type.

Operation Name Description


Display() To display the entire array on the screen
Add(n) / Append(n) To add a particular element (n) at the end of the array
Insert(index, n) To add a particular element (n) to a particular index (index)
Delete(index) To delete an element at a particular index (index)
Search(n) To check whether a particular element (n) is present in the array or not
Get(index) To return the element at a particular index (index)
Set(index, x) To change element at a particular index (index) with an element (x)
Max() / Min() To return the max and min element in the array
Reverse() To reverse the order of the elements in the array
Shift() To shift the all elements either to the right or left
Figure 6: Operations on Array ADT (See Refs. [4])

P2: Application of Stack in memory

This section will discuss the definition of stacks then list the basic operations on a stack. The last part
Page |5
of this section will be able the memory stack, its features and an example.

1. Definition

Stack is a linear abstract data type following a particular order for the operations are performed. It
serves as a collection of elements that follows the Last In First Out (LIFO) strategy. This strategy states
that the element inserted last will come out first. The stack data type is similar to a stack of plates in
Figure 7 below.

Figure 7: Stack of plates (See Refs. [5])

For a stack of plates, it is only possible to put more plates at the top and take out the plate at the
highest position so the last plate that came into the stack will come out first.

Page |6
2. Basic operations on stack

Two main operations on stack are push() and pop() which is illustrated in Figure 8 below.

Figure 8: Push and pop operations on stack (See Refs. [5])

The push() operation as seen in Figure 8 above is used to add an item to the stack. However, if the
stack is full, it will be an Overflow condition. The logic of a push() operation is as shown in Figure 9
below.

Figure 9: Logic of push() operation (See Refs. [5])

The pop() operation as seen in Figure 8 above is opposite to the push() and will remove an item from

Page |7
the stack. And it will pop items in a reversed order that they were pushed in which will remove the top
item first. If the stack is empty, it will be an Underflow condition. The logic of pop() operation is shown
in Figure 10 below.

Figure 10: Logic of pop() operation (See Refs. [5])

The other additional operations on stack are top(), isEmpty(), and many more.

The top() operation will return the top element of the stack. The logic of top() operation is shown in
Figure 11 below.

Figure 11: Logic of top() operation (See Refs. [5])

Page |8
The isEmpty() operation is a little different from the previous operations in which its data type is
Boolean. This operation will return true if the stack is empty, otherwise, it will return false. The logic of
isEmpty() operation is as shown in Figure 12 below.

Figure 12: Logic of isEmpty() operation (See Refs. [5])

The applications of stack are listed below:

 Redo and undo features in many applications like Word or Microsoft Visual Studio Code

 Forward and backward features in web browsers

 Recursive algorithms

 Backtracking is one of the algorithm designing techniques in which the program goes into someway
and if that way is not efficient then it will come back to the previous state to find another path.

 Memory management as any modern computer uses a stack as the primary management for a
running purpose so each program has its own memory allocations

 String reversal is when each character of a string is pushed into a stack one by one so that the first
character is at the bottom of the stack while the last character is at the top. This way, the pop()
operation can be executed to get a string in reversed order

Page |9
3. Memory stack

Memory stacks are regions of memory where data is added or removed following the LIFO strategy. In
modern computers, each thread has a reserved memory region referred to as its stack. When a
function executes, it may add some of its local data to the top of the stack and when the function exits
it must remove that data from the stack. A memory stack is used for static memory allocation and the
execution of a thread. Such stack contains primitive values specific to a method and references to
objects referred from the method and stored in heap space. Due to the LIFO order, whenever a
method is called, a new block is created on top of the stack and also contain primitive variables and
references to objects specific to that new method. A programmer does not have to worry about
memory allocation and de-allocation of stack variables. This kind of memory allocation is known as
Temporary memory allocation because as soon as the method finishes its execution all the memory
blocks related to that method flushes out from the stack automatically. So data stored in the memory
stack is only accessible when the method is running and will be lost as soon as the method finishes
execution.

Features of Memory stack:

 Because the data is added and removed as stated in LIFO strategy, this memory allocation is very
simple and much faster than heap memory allocation

 Memory allocation on the stack is automatically and efficiently reclaimed when the function exits
which is convenient for the programmer if the data is not required any longer

 If data stored in memory stack needs to be kept then it must be copied to the heap before the
function ends

 It grows and shrinks as new methods are called and returned respectively

 If the memory stack is full, it will throw stack overflow error

 Access to this memory is faster than heap memory

 Each thread operates on its own stack so it is safe thread-safe

P a g e | 10
Let’s discuss how stack memory works by examining an example code snippet in Figure 13 below.

Figure 13: Example of a memory stack (See Refs. [7])

P a g e | 11
Firstly, when the program enters the main() method, a space is created in memory stack to store
primitive values and references of this method. This memory stack space will directly store the value
of integer id = 23. A reference for variable person of type Person will also be stored in the memory
stack pointing to the actual object in the heap as shown in Figure 14 below.

Figure 14: Memory stack allocation diagram (See Refs. [7])

Secondly, when running the main() method, the program will also run the buildPerson(int, String)
method which is also demonstrated in Figure 14 above and the reference is similar to that of the
main() method. The memory allocated for buildPerson() method will be stacked upon the main()
method’s block. While executing the buildPerson() method, the program will call the Person
constructor which will be stored on top of the buildPerson() memory block. (See Refs. [6], [7])

P a g e | 12
P3: Application of Stack on software (See Refs. [8])

This section will discuss the software stack from definition to some real applications of it.

A software stack is a collection of independent components working together to facilitate the


execution of an application. The components in these stacks can include OS, architectural layers,
protocols, runtime, middleware and function calls; these components are stacked upon each other in
a hierarchy as shown in Figure 15 below.

Figure 15: Software stack (See Refs. [8])

The lower-level components often interact with the hardware while the higher-level components
perform tasks and services for the end user. Components interact directly with the application
through a series of complex instructions traversing through the stack.

Software stacks can be simple or complicated depending on the application complexity. Software
stacks can incorporate components and services from an organization own resource, third-party
providers or supplied by a cloud provider. There is no minimum requirement for the components and
services that must be present in the software stack, except for the functions that support the
application’s development, delivery and operation. The minimum requirement for software stack
includes an OS, database, tools for programming language, and the application.

Many components that frequently appear in the software stack become commonly recognized by

P a g e | 13
their names which also represent their core components. Some common software stacks that are used
by enterprises are listed below.

 LAMP including Linux, Apache, MySQL, and PHP is well-known for web development. The lowest
layer is the Linux operating system that interfaces with the Apache web server. And the highest
layer is the programming language. LAMP stacks are popular because all the components are open
source and can run on commodity hardware.

 MEAN including MongoDB, Express, Angular, and Node.js helps eliminates the language barriers
which is common in software development. The foundation of MEAN stack is MongoDB which is a
non-SQL document database. The HTTP server is Express and Angular is the framework for front-
end JavaScript. The highest layer of this stack is Node which is a platform for server-side scripting.
The MEAN stack does not rely on any OS so it gives developers more flexibility.

 Apache CloudStack is an open-source cloud management stack to provide infrastructure as a


service for customers. CloudStack also provides multiple layers of optional services and support for
several hypervisors as well as application program interfaces (APIs).

 BHCS includes OpenBSD, C, httpd, and SQLite is also a collection of open-source tools for web
applications built based on the OpenBSD OS and httpd web server, and written in C language.

Software stacks provide several advantages which are listed below.

 It can provide predetermined answers to issues which can sometimes be the best solution

 They give the bare minimum of software requirements to get the desired results

 Software stacks can be installed in a computer or automatically installed in computer templates

 Similar installation and operation as customizable systems so the answers offered are consistent

 The majority of software stacks have support for the complete bundle as well as community forums

 Software stacks can be installed using an image or software specifications

 Each component adds a layer of compatibility to the others so bundling them makes them easy to
download and deploy all at once

M1: Application of Queue


P a g e | 14
This section will discuss queue from definition to its operations and then compare some of its
advantages and disadvantages.

1. Definition (See Refs. [9])

Queue is an abstract data structure that is somewhat similar to stack. However, a queue is open at
both ends with one end used to insert data (enqueue) and the other to remove data (dequeue). This
data structure follows an order called First In First Out (FIFO) meaning the element inserted first in the
queue will come out first and the last element inserted will come out last. It is basically an ordered list
where element insertion is done from the rear end while deletion is done at the front end. A queue
can be represented as Figure 16 below.

Figure 16: Queue illustration (See Refs. [9])

It should be noted that enqueue operation can not be performed if the queue size is full, if enqueue is
executed when the queue size is full, it is called overflow. On the other hand, if dequeue is executed
when the queue size is empty then it is called underflow. A queue can be implemented using an array
but can only organize a limited number of elements, it can also be implemented using a linked list
which can organize an unlimited number of elements.

P a g e | 15
2. Operations on queue

The enqueue() operation is used to insert data to the rear end. Firstly, the program will check if the
queue is full or not, if it is full then an overflow error will be shown. Otherwise, the pointer that is
pointing to the rear element will be incremented by one so it will point to the empty space next the
rear element. Then the element is added to the queue at the position that the rear pointer is pointing
and return success. The complexity of this operation is O(1). An illustration of enqueue() is shown in
Figure 17 and the logic of it is shown in Figure 18 below.

Figure 17: enqueue() operation illustration (See Refs. [10])

Figure 18: Logic for enqueue() operation (See Refs. [10])

P a g e | 16
The dequeue() operation is used to remove the data at the front of the queue. The steps of this
operation are as follow: first, check if the queue is empty or not, if it is empty then produce the
underflow error. Otherwise, access the data where the front pointer is pointing to, this pointer will be
incremented by 1 so that it points to the next available element. After that, the operation return
success. The complexity of this operation is O(1). An illustration of dequeue() is shown in Figure 19 and
the logic of it is shown in Figure 20 below.

Figure 19: dequeue() operation illustration (See Refs. [10])

Figure 20: Logic for dequeue() operation (See Refs. [10])

P a g e | 17
The peek() operation helps to see the data at the front of the queue. The logic of this operation is as
shown in Figure 21 below.

Figure 21: Logic for peek() operation (See Refs. [10])

The isEmpty() operation helps determine whether the queue is empty or not. If the value of the front
pointer is less than 0 then the queue is not initialized yet so it is empty. The logic of this operation is as
shown in Figure 22 below.

Figure 22: Logic for isEmpty() operation (See Refs. [10])

P a g e | 18
3. Types of queues (See Refs. [9])

There are four primary types of queues, all of which are listed below:

 Simple queue also known as linear queue is the most basic version of queue. Insertion to and
deletion from this type of queue is done at the rear and the front ends of the queue, respectively.

 Circular queue is where the elements act as a circular ring, the only difference with the simple
queue is that the read element is connected with the front element. The memory is utilized better
when using this type of queue because when there is an empty space at a certain position, another
element can be easily added to that position.

 Priority queue is a special type of queue in which It arranges the elements based on some priority.
The priority can be the element with highest value will create a queue with decreasing order of
values.

 Double Ended queue means that elements can be inserted or removed from both of the ends so
this type of queue may not obey the First In First Out rule.

4. Applications of queues (See Refs. [9])

There are many applications for queues in real life, some of which are listed below:

 Multi programming is when multiple programs are running in the main memory so queues can be
used to organize these programs

 Queues can be used in routers or switches in a network

 Mail queue is a directory that stores data and controls files for mail messages

 Job scheduling in computer so that the computer will execute those jobs one after another, these
jobs are assigned to the processor one by one using an organized queue

 ATM booth line

 Ticket counter line

 Key press sequence on the keyboard

P a g e | 19
5. Advantages of queues (See Refs. [9])

 A large amount of data can be managed efficiently with ease

 Insertion and deletion operations can be performed with ease as it follows the FIFO rule

 Queues are useful when a service is being used by multiple customers

 Queues are fast in speed for data communication between operations

 Queues can be used in the implementation of other data structures

6. Disadvantages of queues (See Refs. [9])

 Insertion and deletion elements to the middle of the queue is time consuming

 Limited space

 Searching for an element takes O(n) time

 Maximum size of queue must be defined prior

M3: Using OOP to represent ADT

D2: Discussion on ADT versus OOP

P a g e | 20
Part 2: Algorithms

M2: Sorting algorithms

1. Bubble Sort Algorithm

Bubble sort algorithm is the simplest sorting algorithm that runs through the array repeatedly
comparing two contiguous elements and swap them if they are out of order. We will discuss the
process that bubble sort algorithm works below. Assuming the array is 7, 2, 9, 6, 4 and it is required to
sort in ascending order.

Figure 23: First pass of bubble sort (See Refs. [11])

As seen in Figure 23 above, the program will compare the first and second elements of the array
starting from the first index. In this case, they are 7 and 2, because 7 is greater than 2, they are
swapped so 2 will be the first element and 7 will be the second. The program will continue with the
second and third elements are 7 and 9. This process of comparing two contiguous elements will
continue until the end of the array. After the first pass, the array will be 2, 7, 6, 4, 9. The second, third,
and fourth pass on this array are shown in Figure 24 and 25 below.

P a g e | 21
Figure 24: Second pass of bubble sort (See Refs. [11])

Figure 25: Third pass of bubble sort (See Refs. [11])

Figure 26: The sorted array (See Refs. [11])

P a g e | 22
The pseudo code of the bubble sort algorithm is as shown in Figure 27 below.

Figure 27: Pseudo code of bubble sort algorithm (See Refs. [11])

P a g e | 23
Time complexity

The bubble sort approach uses an inner loop and an outer loop with the inner loop performs O(n)
comparisons. The worst case is when the outer loop runs O(n) times; in which case, the time
complexity is O(n x n) = O(n 2). While the best-case scenario is when the array is already sorted; in such
case, only the inner loop performs O(n) times so the time complexity for the best case is O(n). For the
average case, the inner loop still requires O(n) but the outer loop required O(n/2); so the time
complexity of the average case is O(n/2 x n) = O(n 2).

Memory complexity

Bubble sort requires only a fixed amount of space for the flag, i, and size variables. This is an in-place
sorting algorithm which will modify the original array to sort it so the space complexity is O(1).

Advantages

 The bubble sort requires very little memory and as the array gets longer, the memory complexity
still stay the same

 The bubble sort is coded using only a few lines so it is suited for small programs and new
programmers

 With the best-case scenario is when the array is already sorted, the time complexity is O(n) which
will be helpful in determining whether the array is sorted or not

Disadvantages

 The main disadvantage is the amount of time it takes, because its complexity is O(n 2), it become
highly inefficient when working with large data sets

 The presence of turtles can significantly slow the sorting algorithm

P a g e | 24
2. Quick Sort Algorithm (See Refs. [12], [13])

Quick sort is a sorting algorithm that uses the divide and conquer approach in which the program picks
an element as the pivot and partition the array around the pivot. There are many different versions of
quick sort depending on the pivot: always pick the first element as pivot, always pick the last element
as pivot, pick a random element as pivot, and pick median as pivot. The key process of quick sort is
partition which will get an element x from the array then compare other elements with x. All elements
smaller than x is put before x, and elements greater than x is put after x. All of which are done in linear
time. The implementation of quick sort is demonstrated in Figure 28 below.

Figure 28: Quick Sort Algorithm (See Refs. [13])

As shown in Figure 27, the chosen pivot is 70 so element greater than 70 are 80 and 90 are put on the
right side of 70. The other numbers are smaller then 70 so it is put to the left including 10, 30, 40, and
50. Next, the program will choose 50 as the pivot for the sub-array on the left, because 10, 30 and 40
are smaller than 50 so it is all put on the left of 50. The similar process repeat until the array is sorted.
There are two functions to implement the quick sort algorithm: recursive quick sort function and
partition() function. Pseudo code for partition() function will be shown in Figure 29 and pseudo code
for quick sort function will be in Figure 30 below.

P a g e | 25
Figure 29: Pseudo code for partition function() (See Refs. [13])

Figure 30: Pseudo code for quick sort function (See Refs. [13])

P a g e | 26
Time complexity

The worst case of the quick sort algorithm is O(n 2) is when the pivot is either the greatest or the
smallest element. This leads to the case where the pivot is at the extreme end of the array and there is
only one sub-array, the other is empty. Therefore, quick sort can only process one array at a time
which is slower, and it has better performance when the pivot is scattered. The best case scenario is
O(n * log n) when the pivot is the middle element or near to the middle element because in this case,
the two sub-arrays are similar in length and equal half the length of the original array. And the average
case is also O(n * log n).

Memory complexity

A good implementation of quick sort uses O(log n) of memory for stack space for recursion.

3. Bubble sort versus quick sort

The performance of both algorithms is compared against each other in Figure 28 below.

Name Best Average Worst Memory Stable


Bubble sort n n2 n2 1 Yes
Quick sort n * log(n) n * log(n) n2 log(n) Depends
Figure 31: Performance comparison between bubble and quick sort

The quick sort method is recommended when the stability is not required and average case
performance matters more than the worst-case performance. The bubble sort algorithm can work
better on small data sets and it will produce more stable results. However, the average time for a
bubble sort is not as good as quick sort.

D1: Shortest path algorithms

P a g e | 27
REFERENCES
[1] Janssen, T. (2017). “OOP Concept for Beginners: What is Abstraction?". [Online] Stackify. Available
at: https://stackify.com/oop-concept-abstraction/ [Accessed 06 October 2022].

[2] GeeksforGeeks, (2022). “Abstract Data Types”. [Online] Available at:


https://www.geeksforgeeks.org/abstract-data-types/ [Accessed 06 October 2022].

[3] Pandey, S. (2021). “4 Important programming concepts Explained using simple real-life examples”.
[Online] Medium. Available at: https://samarthya31.medium.com/understanding-oop-concepts-using-
simple-real-life-examples-50531b5b7765 [Accessed 06 October 2022].

[4] Dot Net Tutorials, (2021). “Array Abstract Data Type in C”. [Online] Available at:
https://dotnettutorials.net/lesson/array-abstract-data-type-in-c/ [Accessed 06 October 2022].

[5] GeeksforGeeks, (2022). “Introduction to Stack - Data Structure and Algorithm Tutorials”. [Online]
Available at: https://www.geeksforgeeks.org/introduction-to-stack-data-structure-and-algorithm-
tutorials/ [Accessed 06 October 2022].

[6] GeeksforGeeks, (2022). “Stack versus Heap Memory Allocation”. [Online] Available at:
https://www.geeksforgeeks.org/stack-vs-heap-memory-allocation/ [Accessed 07 October 2022].

[7] Baeldung, (2022). “Stack Memory and Heap Space in Java”. [Online] Available at:
https://www.baeldung.com/java-stack-heap [Accessed 07 October 2022].

[8] Semilof, M. (2020). “Software Stack”. [Online] TechTarget. Available at:


https://www.techtarget.com/searchapparchitecture/definition/software-stack [Accessed 07 October
2022].

[9] GeeksforGeeks, (2022). “Applications, Advantages and Disadvantages of Queue”. [Online] Available
at: https://www.geeksforgeeks.org/applications-advantages-and-disadvantages-of-queue/ [Accessed
07 October 2022].

[10] TutorialsPoint, (2020). “Data Structure and Algorithms – Queue”. [Online] Available at:
https://www.tutorialspoint.com/data_structures_algorithms/dsa_queue.htm [Accessed 07 October
2022].

P a g e | 28
[11] Upadhyay, S. (2022). “What is Bubble Sort Algorithm? Time Complexity & Pseudocode”. [Online]
Simplilearn. Available at: https://www.simplilearn.com/tutorials/data-structure-tutorial/bubble-sort-
algorithm [Accessed 07 October 2022].

[12] Nagarajan, M. (2020). “Algorithms — Quick Sort”. [Online] Medium. Available at:
https://medium.com/swlh/algorithms-quick-sort-4e611ec07042 [Accessed 08 October 2022].

[13] GeeksforGeeks, (2022). “QuickSort”. [Online] Available at: https://www.geeksforgeeks.org/quick-


sort/ [Accessed 08 October 2022].

P a g e | 29

You might also like