Professional Documents
Culture Documents
Faculty of Engineering
Module Details
Module Code EC2731 Module Title Data Structures and Algorithms
Program: SLIIT Course: BSc
Stream: Civil/Electronics/Mechanical/
Assessment details
Receiving Officer Specific comments about the work (including overall comments and guidelines for
(seal, signature, date) improvement)
Tutor: Signature: Date:
Marks: [All marks are subject to external moderation and approval of board of
examinations]
1|Page
SRI LANKA INSTITUTE OF
INFORMATION TECHNOLOGY
EN Number - EN21464614
2|Page
Task 01
Developing a container for 10,000 integer data points using a dynamic array.
A dynamic array is an array that has automated scaling as a significant enhancement. Because arrays
have a fixed size, one drawback is that you must predetermine how many elements your array will
include. The size of a dynamic array grows as you add additional components. Therefore, there is no
need to estimate the size in advance because the size is doubled once more when this array's maximum
is reached.
package task1;
import task1.task1;
3|Page
void pop(){current--;}
for(int i=0;i<n;i++) {
v.push(i+1);
}
// Printing Statements
System.out.println("Array List Elements:- ");
v.print();
System.out.println("Size of the Array List: " + v.size());
System.out.println("Capacity of the Array List: " + v.getcapacity());
}
}
The results which obtained after running the above code are below.
4|Page
Task 02
Finding the worst-case running times for indexing for the dynamic array implementation
To find the worst-case running times for indexing, main code was modified as this.
The results which obtained after running the above code are below.
According to the Results the running times for array sizes are mentioned below.
5|Page
4096 400
8192 600
Then, the plot of array size vs running time was created using above data.
2000
Running Time (ns)
1500
1000
500
0
0 1000 2000 3000 4000 5000 6000 7000 8000
Array Size
This proves that the time taken to index an array is irrespective to its size and i=that time is about 300-
400 ns. Here, the Big-O is also a constant ( O[1] ). That time complexity confirms it.
6|Page
Task 03
Developing a container for 10,000 integer data points using a singly linked list.
A variation of the linked list known as the singly linked list only permits forward traversal of linked
lists. This is a straightforward form, but it works for a variety of issues, including Big Integer
computations.
package task3;
import task3.Node;
class Node {
int data;
Node next;
public task3() {
head = null;
tail = null; // Setting head and tail as null values
size = 0;
}
7|Page
// Inserting new node at the front
public void push(int new_data){
Node new_node = new Node(new_data, null); // New node and with new
data
size++ ;
8|Page
int n=10000;
for(int i=0;i<n;i++) {
x.push(n-i);
}
x.display();
}
}
The results which obtained after running the above code are below.
Task 04
Finding the worst-case running times for indexing for the singly linked list implementation.
To find the worst-case running times for indexing, main code was modified as this.
The results which obtained after running the above code are below.
The Worst Case Running Time for Singly Linked List 1 is 1400 ns
The Worst Case Running Time for Singly Linked List 2 is 700 ns
The Worst Case Running Time for Singly Linked List 4 is 300 ns
The Worst Case Running Time for Singly Linked List 8 is 300 ns
The Worst Case Running Time for Singly Linked List 16 is 300 ns
The Worst Case Running Time for Singly Linked List 32 is 300 ns
The Worst Case Running Time for Singly Linked List 64 is 500 ns
The Worst Case Running Time for Singly Linked List 128 is 1000 ns
The Worst Case Running Time for Singly Linked List 256 is 1500 ns
The Worst Case Running Time for Singly Linked List 512 is 2900 ns
The Worst Case Running Time for Singly Linked List 1024 is 5900 ns
The Worst Case Running Time for Singly Linked List 2048 is 11500 ns
The Worst Case Running Time for Singly Linked List 4096 is 22600 ns
The Worst Case Running Time for Singly Linked List 8192 is 45000 ns
9|Page
According to the results the running times for single linked list size sizes are mentioned below.
Then, the plot of Size vs Time was created using above data.
30000
25000
20000
15000
10000
5000
0
0 1000 2000 3000 4000 5000 6000 7000 8000 9000
Size
This proves that in the worst-case time taken to index a single linked list increasing linearly. Here, the
Big-O is also a “n” ( O[n] ). That time complexity confirms it.
10 | P a g e
Task 05
Developing a container for 10,000 integer data points using a doubly linked list.
A variant of a linked list called a doubly linked list makes it easier to go both ahead and backward than a
single linked list does.
package t5;
public class t5 {
Node head;
Node tail;
int size;
11 | P a g e
// Print Statements
public void display() {
System.out.print("Double linked list elements = ");
if (size == 0){
System.out.print("empty\n");
return;
}
if (head.getLinkNext() == null) {
System.out.println(head.getData() );
return;
}
Node ptr = head;
System.out.print(head.getData()+ " <> ");
ptr = head.getLinkNext();
while (ptr.getLinkNext() != null) {
System.out.print(ptr.getData()+ " <> ");
ptr = ptr.getLinkNext();
}
System.out.print(ptr.getData()+ "\n");
}
// Main Function
public static void main(String args[]) {
t5 x = new t5(); //Start with the empty list
int n=10000;
for(int i=0;i<n;i++) {
x.push(n-i);
}
x.display();
}
}
class Node {
int data;
Node prev;
Node next;
public Node() {
next = null;
prev = null;
data = 0;
}
public Node(int d, Node n, Node p) {
data = d;
next = n;
prev = p;
}
12 | P a g e
}
The results which obtained after running the above code are below.
Double linked list elements = 1 <> 2 <> 3 <> 4 <> 5 <> 6 <> 7 <> 8 <> 9 <>
10 <> 11 <> 12 <> 13 <> 14 <> 15 <> 16 <> 17 <> 18 <> 19 <> 20 <> 21 <> 22
<> 23…<>10000
Task 06
Finding the worst-case running times for indexing for the doubly linked list implementation
To find the worst-case running times for indexing, main section was modified as this.
13 | P a g e
The results which obtained after running the above code are below.
The Worst Case Running Time for the Doubly Linked List 1 is 1700 ns
The Worst Case Running Time for the Doubly Linked List 2 is 800 ns
The Worst Case Running Time for the Doubly Linked List 4 is 300 ns
The Worst Case Running Time for the Doubly Linked List 8 is 300 ns
The Worst Case Running Time for the Doubly Linked List 16 is 300 ns
The Worst Case Running Time for the Doubly Linked List 32 is 400 ns
The Worst Case Running Time for the Doubly Linked List 64 is 600 ns
The Worst Case Running Time for the Doubly Linked List 128 is 1100 ns
The Worst Case Running Time for the Doubly Linked List 256 is 1700 ns
The Worst Case Running Time for the Doubly Linked List 512 is 3200 ns
The Worst Case Running Time for the Doubly Linked List 1024 is 6700 ns
The Worst Case Running Time for the Doubly Linked List 2048 is 12900 ns
The Worst Case Running Time for the Doubly Linked List 4096 is 25500 ns
The worst Case Running Time for the Doubly Linked List 8192 is 50600 ns
According to the results the running times for double linked list size sizes are mentioned below.
Then, the plot of Size vs Time was created using above data.
60000
50000
Running Time (ns)
40000
30000
20000
10000
0
0 1000 2000 3000 4000 5000 6000 7000 8000 9000
Size
14 | P a g e
This proves that in the worst-case time taken to index a double linked list increasing linearly. Here, the
Big-O is also a “n” ( O[n] ). That time complexity confirms it.
When considering all three cases, using dynamic array structure is more efficient in terms of the
indexing operation. The Big-O notation justify it.
15 | P a g e
DISCUSSION
The structure of the two data types, array and linked list, is where they diverge most. Each member of an
array has an associated index since they are index-based data structures. The linked list, on the other
hand, depends on references, and each node contains data as well as pointers to the element before it and
element after it. Basically, an array is a set of similar data objects stored in sequential memory locations
under a common heading or a variable name.
A linked list is not constrained to a certain size and can grow and shrink as needed during execution,
whereas an array has a set size and must be specified beforehand.
While a linked list is a type of data structure that holds a succession of entries connected to one another.
A linked list element has two fields. The actual value to be saved and processed is included in the Data
field, while the Link field is the other. The address of the following data item in the linked list is also
included in the link field. A pointer is the address used to reach a certain node.
An array is a data structure that consists of a group of similar-type data elements, whereas a
linked list is a non-primitive data structure that consists of a group of unordered linked items
called nodes.
The items of the array are indexed, therefore, to access the fourth element you must provide the
variable's name in the square bracket along with its index or location. But to get to the fourth
entry in a linked list, you must start at the top and work your way down.
A linked list is much slower than an element array since it takes linear time to reach each item.
The insertion and deletion operations in arrays take a long time. However, these actions are
carried out quickly in linked lists.
Arrays have a set size. Linked lists, on the other hand, are dynamic and adaptable and can grow
and shrink in size.
Memory is allocated for arrays at build time, but for linked lists, it is done during runtime.
Linked lists store items randomly, while arrays store them in a sequential order.
Because the actual data is contained within the index of the array, less memory is needed. In
contrast, Linked Lists use more memory since they must store extra next and prior referencing
components.
Additionally, the array's use of memory is wasteful. In contrast, the array's memory usage is
effective.
A singly linked list is a set of nodes where each node has two fields ‘data’ and ‘link’. The ‘data’ field
stores actual piece of information and ‘link’ field is used to point to next node. A Doubly Linked
List contains an extra pointer, typically called previous pointer, together with next pointer and data
which are there in singly linked list.
Singly Doubly
16 | P a g e
SLL DLL
Contain 02 fields called data fields called ‘data Contain 03 fields called data fields called ‘data
field’ and ‘next link field’ field’, ‘previous link field’ and ‘next link field’
Only the next node link can be used to do the Either the previous node connection or the
traverse. This means that just one direction of following node link can be used to do the traverse.
traversal is available. As a result, traversal is feasible both ways
(forward and backward).
As it only contains 2 fields, the SLL uses less Due to its three fields, the DLL uses more memory
memory than the DLL. than the SLL.
If possible, utilize a singly linked list to execute Due to the fact that traversals can be done from
stacks. either the beginning or the end
We choose a single linked list when we don't need When better implementation is available, we favor
to execute any searching operations and wish to using doubly linked lists for searching.
conserve memory.
Memory usage is lower for a singly linked list Memory usage for the doubly linked list is higher
compared to a doubly linked list. than for the single linked list.
Reference
17 | P a g e