Data Structures and Algorithms - HE12628
Data Structures and Algorithms - HE12628
0
Table of Contents
Introduction............................................................................................................................... 3
Task 1 .................................................................................................................................... 3
Description........................................................................................................................ 3
Linear Search................................................................................................................... 4
Bubble Sort....................................................................................................................... 6
Big O Notations.............................................................................................................. 16
Output.............................................................................................................................. 20
Task 2_1.............................................................................................................................. 20
Description...................................................................................................................... 20
Output.............................................................................................................................. 30
Task 2_2.............................................................................................................................. 30
1
Description...................................................................................................................... 30
Output.............................................................................................................................. 40
Task 2_3.............................................................................................................................. 40
Description...................................................................................................................... 40
Output.............................................................................................................................. 48
Task 3 .................................................................................................................................. 48
References ............................................................................................................................. 49
Appendix ................................................................................................................................. 50
2
Introduction
This Java programming project will cover a wide range of computer science
programmes. In the first task, various data structures, including bubble sort, insertion
sort, and linear search, are implemented in a given array. There are three smaller jobs
in the second task. ArrayList and Shallow copy are implemented in the first subtask;
Task 1
Description
The first task involves a thorough analysis of the Java implementation of the
linear search algorithm and how it works to locate a particular member in an unsorted
array. The design, implementation, and discussion of the linear search will be followed
by an examination of the subtle differences between Bubble Sort and Insertion Sort.
explanations. Code samples, testing proof, and a comparative study of the Big O
3
Linear Search
Explanation
The linear search is started by comparing the first item of given array with the
searched item by user (Horstmann 2020). If matched, then the search is ended and
returns the index of the item; if not, -1 is returned to indicate that the target is not in
Pseudocode
4
Flowchart
Code Snippet
5
Bubble Sort
Explanation
if these elements are present in the wrong order till no further swaps are required
(Insanudin 2019).
Pseudocode
6
Flowchart
7
Graphical Visualisation
i)
ii)
iii)
8
iv)
v)
vi)
vii)
9
viii)
ix)
x)
xi)
10
xii)
Code Snippet
Insertion Sort
Explanation
The stated sorting algorithm is done by making a sorted array a single item at
a time (Ali, Nawaz and Maitlo 2021). For each iteration, a single item is taken from the
input data to find its perfect position within the sorted list and moved higher elements
11
Pseudocode
12
Flowchart
13
Graphical Visualisation
i)
ii)
iii)
14
iv)
v)
vi)
vii)
15
viii)
Code Snippet
Big O Notations
While Bubble Sort and Insertion Sort have similar worst-case and average time
complexity (O(n^2)), Insertion Sort works better with smaller arrays or arrays that are
16
almost sorted. Although it isn't related to the sorting, Linear Search has an O(n) time
17
Complete Code
18
Test Logs
Name Output
30, 41,
22, 15,
25, 6,
33, 20,
13, 44,
39, 27,
5, 11}
44
30, 41, 13, 15, 20, 22, 13, 15, 20, 22,
22, 15, 25, 27, 30, 33, 25, 27, 30, 33,
33, 20,
13, 44,
39, 27,
5, 11}
30, 41, 13, 15, 20, 22, 13, 15, 20, 22,
22, 15, 25, 27, 30, 33, 25, 27, 30, 33,
33, 20,
19
13, 44,
39, 27,
5, 11}
Output
Task 2_1
Description
oriented design. A "Flight" class and a "Airline" class must be created, with the latter
constructors, accessors, and show how objects interact with one another in a Java
project. We'll also talk about the benefits and drawbacks of managing Flight objects
with an ArrayList and investigate the effects of object cloning via shallow copies.
Complete Code
20
21
22
23
24
Shallow Copy
In the Program class, the concept of shallow copy is executed. In this, original
object is preserved, and only address of the reference is copied (J Eck 2021). Here, a
clone of a Flight object is made and printed details in console. Then, destination of
original object is modified, and the object’s details are printed in console.
25
Advantages of ArrayList
iii) Provide built-in methods for some operations, for example, add, remove, size and
many more.
Disadvantages of ArrayList
Test Logs
Name Output
Paris,
26
Capacity: Flight ID: Flight ID:
27
international, UK, Capacity: UK, Capacity:
Sydney, 13/6/2024
Capacity:
149, Priority
Code: P37,
Flight Type:
international,
Date:
13/6/2024
28
Land the
flight object
The The
destination of destination of
Flight: Flight:
Australia Australia
2:
The
destination of The
The 2: Brisbane
destination of The
the Cloned
29
Flight: Flight:
Australia Australia
Output
Task 2_2
Description
In order to understand how collections can operate as both a queue and a stack,
Task 2_2 ask us to convert the data structure from an ArrayList to an ArrayDeque. It
30
Complete Code
31
32
33
34
35
Advantages of ArrayDeque that implements Deque interface
ii) When it comes to stack operations, ArrayDeque outperforms LinkedList (push and
pop)
36
Disadvantages of ArrayDeque that implements Deque interface
ii) In constant time, not have the ability to access items with the index.
Test Logs
Name Output
37
Code: P27, P19, Flight Flight ID: FL9146,
15/5/2024 13/12/2023
FL0154, FL7412,
Destination: Destination:
UK, Paris,
national, international,
38
Date: Date:
10/11/2023 30/12/2023
Flight ID:
FL7913,
Destination:
Sydney,
Capacity:
149, Priority
Code: P37,
Flight Type:
international,
Date:
13/6/2024
Popping a
flight object
39
Output
Task 2_3
Description
In this task, the previous two classes that are Flight and Airline classes are
copied and after that, some changes are made in the Airline class and we are asked
40
Complete Code
41
42
43
44
Advantages of PriorityQueue
Disadvantages of PriorityQueue
Test Logs
Name Output
FL7412,
Destination:
45
Paris, Landing Flight: Landing Flight:
Capacity:
127, Priority
Code: P85,
Flight Type:
national,
Date:
10/11/2023
Flight ID:
FL1235,
Destination:
Sydney,
Capacity:
169, Priority
46
Code: P19,
Flight Type:
international,
Date:
15/5/2024
Flight ID:
FL9146,
Destination:
Australia,
Capacity:
187, Priority
Code: P27,
Flight Type:
cargo, Date:
13/12/2023
Flight ID:
FL7913,
Destination:
Sydney,
Capacity:
149, Priority
Code: P37,
Flight Type:
international,
47
Date:
13/6/2024
Output
Task 3
discussed, and two sorting algorithms Bubble Sort and Insertion Sort, are elaborated.
The first achieved learning outcome was the capability of unique distinguishing
PriorityQueue. This is used in the perfect position for all the given tasks. In this,
concepts of package, class, object, methods, and several other things are correctly
declared and discussed. Also, Javadoc is created for the Java programming
developed in Task 2_1, 2_2 and 2_3. Javadoc is the process of commenting in Java
programming. In the Task 2_1, the concept of ArrayList and the shallow copy are
properly executed. Also, in the Task 2_2, the concept of ArrayDeque is used instead
48
References
Ali, H., Nawaz, H. and Maitlo, A., 2021. Performance Analysis of Heap Sort and
Horstmann, C.S., 2020. Big Java: Early Objects. John Wiley & Sons.
results with Java using bubble sort method. In Journal of Physics: Conference Series
J Eck, D., 2021. Introduction to programming using Java. Hobart and William Smith
Colleges.
Santos, J.C., Zhang, X. and Mirakhorli, M., 2022, November. Counterfeit object-
49
Appendix
Javadoc file
50