Data Structures and Algorithms
Data Structures and Algorithms
✘ By
✘ Engr. Isi Edeoghon
Compiled by Isi Edeoghon
What is an Algorithm?
Categories of Algorithms?
✘ Search − Algorithm to search an item in a data structure.
✘ Sort − Algorithm to sort items in a certain order.
✘ Insert − Algorithm to insert item in a data structure.
✘ Update − Algorithm to update an existing item in a data structure.
✘ Delete − Algorithm to delete an existing item from a data structure.
Compiled by Isi Edeoghon
Characteristics of Algorithms
Not all procedures can be called an algorithm. An algorithm should have the following characteristics −
✘ Unambiguous − Algorithm should be clear and unambiguous. Each of its steps (or phases), and their
inputs/outputs should be clear and must lead to only one meaning.
✘ Output − An algorithm should have 1 or more well-defined outputs, and should match the desired output.
✘ Independent − An algorithm should have step-by-step directions, which should be independent of any
programming code.
Compiled by Isi Edeoghon
✘ Hence, many solution algorithms can be derived for a given problem. When this is done,
we analyze the proposed solution algorithms and implement the most suitable solution.
Compiled by Isi Edeoghon
Algorithm Complexity
✘ Suppose X is an algorithm and n is the size of input data, the time and space used by the
algorithm X are the two main factors, which decide the efficiency of X.
✘ Time Factor − Time is measured by counting the number of key operations such as
comparisons in the sorting algorithm.
✘ Space Factor − Space is measured by counting the maximum memory space required
by the algorithm.
The complexity of an algorithm f(n) gives the running time and/or the storage space
required by the algorithm in terms of n as the size of input data.
Compiled by Isi Edeoghon
Space Complexity
✘ Space complexity of an algorithm represents the amount of memory space required by the algorithm in its life cycle. The space
required by an algorithm is equal to the sum of the following two components −
✘ A fixed part that is a space required to store certain data and variables, that are independent of the size of the problem. For
example, simple variables and constants used, program size, etc.
✘ A variable part is a space required by variables, whose size depends on the size of the problem. For example, dynamic memory
allocation, recursion stack space, etc.
✘ Space complexity S(P) of any algorithm P is S(P) = C + SP(I), where C is the fixed part and S(I) is the variable part of the algorithm, which
depends on instance characteristic I. Following is a simple example that tries to explain the concept −
✘ Algorithm: SUM(A, B)
✘ Step 1 - START
✘ Step 2 - C ← A + B + 10
✘ Step 3 - Stop
✘ Here we have three variables A, B, and C and one constant. Hence S(P) = 1 + 3. Now, space depends on data types of given variables
and constant types and it will be multiplied accordingly.
Compiled by Isi Edeoghon
Time Complexity
✘ Array
✘ Linked List
✘ Stack
✘ Queue
✘ Binary Tree
✘ Binary Search Tree
✘ Heap
✘ Hashing
✘ Graph
Compiled by Isi Edeoghon
Arrays
✘ Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value.
✘ To declare an array in Java for instance, define the variable type with square
brackets:
Characteristics of an Array
✘ Linear Data Structure
✘ Elements are stored in adjoining memory locations
✘ Elements can be accessed randomly using index
✘ Contains elements of similar data type
Syntax:
✘ Array declaration in Java:
datatype varname []=new datatype[size];
datatype[] varname=new datatype[size];
✘ Declaration and initialization together:
Accessing an Array
Applications of Arrays
Linked List
A linked list is a sequence of data structures, connected via links.
Each link contains a connection to another link.
✘ .
Compiled by Isi Edeoghon
Linked List
Linked List
✘ A Linked List acts as a dynamic array and there is no need to specify size while
creating it, the size of the list automatically increases when items are added or
removed dynamically
Compiled by Isi Edeoghon
}
Compiled by Isi Edeoghon
Stack
✘ Stacks are abstractly equivalent to linked lists. They are used to model a
First-In-Last-Out (FILO), or Last-In-First-Out (LIFO) search method.
Compiled by Isi Edeoghon
Stack
✘ A stack can be implemented by means of Array, Structure, Pointer, and
Linked List. Stack can either be a fixed size one or it may have a sense of
dynamic resizing
Compiled by Isi Edeoghon
✘ Stack operations may involve initializing the stack, using it and then de-initializing it. A
stack is also used for the following four primary operations −
✘ 4 major operations:
Advantages
Disadvantages
✘ To create a stack, you must import java.util.stack package and use the
Stack() constructor of this class. The line below creates an empty Stack.
(……………………………continued)
// Displaying the Stack
System.out.println("Initial Stack: " + stack);
(…………………………..continued) {
Integer pos = (Integer)
// Displaying element on the top of the stack stack.search(element);
static void stack_peek(Stack<Integer> stack)
{ if(pos == -1)
Integer element = (Integer) stack.peek(); System.out.println("Element not
found");
System.out.println("Element on stack
top: " + element); else
} System.out.println("Element is found
at position: " + pos);
// Searching element in the stack
}
static void stack_search(Stack<Integer>
stack, int element)
Compiled by Isi Edeoghon
Output:
Pop Operation:
4
3
2
1
0
Element on stack top: 4
Element is found at position: 3
Element not found
Compiled by Isi Edeoghon
✘ search(Object element) It determines whether an object exists in the stack. If the element is found,
It returns the position of the element from the top of the stack. Else, it returns -1.
Compiled by Isi Edeoghon
Queues
✘ Queue is an abstract data structure, somewhat similar to Stacks. Unlike
stacks, a queue is open at both its ends. One end is always used to insert
data (enqueue) and the other is used to remove data (dequeue). Queue
follows First-In-First-Out methodology, i.e., the data item stored first will
be accessed first.
Compiled by Isi Edeoghon
✘ Few more functions are required to make the above-mentioned queue operation efficient. These are −
✘ peek() − Gets the element at the front of the queue without removing it.
✘ isfull() − Checks if the queue is full.
✘ isempty() − Checks if the queue is empty.
✘ In queue, we always dequeue (or access) data, pointed by front pointer and while enqueing (or storing) data in the
queue we take help of rear pointer.
Compiled by Isi Edeoghon
References
✘ Data Structures and Algorithms, Retrieved January 22 from
https://www.tutorialspoint.com/data_structures_algorithms/algorithms
_basics.htm
✘ Linked Lists Class/Stack Class, Retrieved January 25 from
https://www.geeksforgeeks.org/stack-class-in-java/
✘ Arrays/Linked List Retrieved January 29 from
✘ Data Structures & Algorithm using Java a Beginners Guide
Faizan Parvez , July 2020, FULLSTACK Development