Day 3 - Fundamental Data Structures I
Day 3 - Fundamental Data Structures I
and Algorithms
▪Fundamental Data Structures
Arrays
Objectives ArrayLists
Queues
Stacks
LinkedLists
DS types
▪Based on the organization of elements,
▪This is an array of seven elements. All the elements are integers and
homogeneous.
▪The green box below the array is called the index, which always starts from zero
and goes up to n-1 elements.
▪In this case, as there are seven elements, the index is from zero to six.
There are 3 main features of an array:
data_type[] variable_name;
data_type variable_name[];
string[] favPlayers =
{“Zidane", "Maradona",
“Pele"};
To declare arrays with default values
data_type[] variable_name = new data_type[size];
array[index]
Time complexity
▪Accessing an element in an array involves retrieving the value stored at a
specific index position within the array.
▪The time complexity for accessing an array element is constant, O(1), regardless
of the size of the array. This is because arrays offer direct access to elements
based on their indices, and accessing any element requires a single operation,
regardless of the size of the array.
public class ArrayAccessExample {
public static void main(String[] args) {
int[] array = {10, 20, 30, 40, 50}; // Creating an
array
intValues.length
length property
▪The time complexity of traversing an array is linear, O(n), where 'n' is the
number of elements in the array.
Let's break down why traversing an array has
linear time complexity:
▪ Sequential Access: Arrays provide contiguous memory allocation. Accessing elements
in an array sequentially involves visiting each element one by one, starting from the
first element and ending at the last. Since each element is visited exactly once, the
time taken is directly proportional to the number of elements in the array.
▪ Constant Time Access: Accessing individual elements in an array using their index has
constant time complexity, O(1). However, when traversing the entire array, each
element must be accessed, resulting in a linear increase in time as the array size
grows.
▪ Loop Iteration: Traversing an array typically involves using a loop (e.g., for loop) to
iterate over each element. The loop runs 'n' times, where 'n' is the size of the array.
Therefore, the time complexity of the traversal operation becomes O(n), as the loop
iterates once for each element in the array.
Searching an element from an array
insertion
▪Insertion at the end (appending)
▪Insertion at the start
▪Insertion at any index
Insertion at the end (when space is available)
▪If the array has space available at the end (i.e., the current size is less than the
capacity of the array), inserting an element at the end involves the following
steps:
1. Find the index where the new element should be inserted (which is the current
size of the array).
2. Assign the new element to that index.
Since these steps involve only constant-time operations (finding the index and
assigning the element), the time complexity for this scenario is O(1).
arr[arr.length - 1] = element;
public static int[] appendIfSpacious(int arr[], int element) {
arr[arr.length - 1] = element;
return arr;
}
Accessing the last element of the array (arr[arr.length - 1]) takes constant time
because it involves a single arithmetic operation and accessing an array element
by index, both of which are constant-time operations.
Assigning the new element also takes constant time.
public class AppendingIfRoomy {
public static int[] appendIfSpacious(int arr[], int elemnt)
{
arr[arr.length - 1] = element;
return arr; }
public static void main(String[] args) {
int[] evens = new int[4];
evens[0] = 2;
evens[1] = 4;
evens[2] = 6;
appendIfSpacious(evens, 8);
System.out.println(Arrays.toString(evens));
}
}
In this example, if the array has space available at the end, inserting the new
element is an O(1) operation
Appending(insertion at the end when no extra
slot)
▪In Java, arrays can’t grow once they have been created; so, to add an element,
you need to:
newArr[arr.length] = element;
return newArray;
}
public class AppendingElementsAtArrays {
public static int[] appendElement(int arr[], int element) {
return newArr;
}
public static void main(String[] args) {
int[] numbers = {2, 4, 6};
int[] newarr = appendElement(numbers, 8);
System.out.println(Arrays.toString(newarr));
}
}
Time complexity
▪Copying Existing Elements: The loop iterates over each element of the input
array arr and copies it to the new array newArray. This loop runs n times,
where n is the length of the input array arr.
▪Appending the New Element: After copying existing elements, the function
appends the new element to the end of the new array.
newArr[0] = element;
Inserting the New Element: The function inserts the new element at the beginning of the new
array.
Inserting the new element at the beginning of the array takes constant time, O(1).
Copying Existing Elements: The loop iterates over each element of the input array arr and copies it
to the new array newArr. This loop runs n times, where n is the length of the input array arr.
https://www.geeksforgeeks.org/search-insert-and-delete-in-an-unsorted-array/
Why not use arrays for everything?
▪Arrays seem to get the job done, so why not use them for all data storage?
We’ve already seen some of their disadvantages. In an unordered array you can
insert items quickly, in O(1) time, but searching takes slow O(N) time. In an
ordered array you can search quickly, in O(logN) time, but insertion takes O(N)
time.
▪For both kinds of arrays, deletion takes O(N) time because half the items (on
the average) must be moved to fill in the hole. It would be nice if there were
data structures that could do everything—insertion, deletion, and searching—
quickly, ideally in O(1) time, but if not that, then in O(logN) time.
References
▪https://www.javatpoint.com/abstract-class-in-java
▪https://www.tutorialspoint.com/data_structures_algorithms/dsa_queue.htm
▪https://www.geeksforgeeks.org/java-program-for-heap-sort/
▪https://www.geeksforgeeks.org/linked-list-in-java/
▪https://www.programiz.com/dsa/queue
▪https://www.javatpoint.com/tree
▪https://www.edureka.co/blog/java-binary-tree