DSA-Notes [JUMAIL]
DSA-Notes [JUMAIL]
Data Structures:
Data structure is the arrangement of data in a computer memory/storage.
Data structure is the implementation of ADT such as stack, queue, linked list, tree, graph.
When working with certain data structures we need to know how to insert new data, how to search
for a specific item, and how to delete a specific item.
Data structure helps for efficient programming.
Data structure reduces complexity of the program and its calculations.
There are two types of data structures: Linear data structure and Nonlinear data structure
What is Algorithm?
An algorithm is a step by step procedure for solving a problem in a finite amount of time.
Many algorithms apply directly to a specific data structures.
Efficiency of an Algorithm:
Some algorithms are more efficient than others. We would prefer to choose an efficient algorithm.
Running time of algorithms typically depends on the input set, and its size(n).
1
JUMAIL.WM Data Structure and Algorithm
2
JUMAIL.WM Data Structure and Algorithm
What is an array?
Arrays are a group of continuous similar type of variables referred by a common name.
In Java, array can be declared and used as follows:
0 1 2 3 4
intnum[5]; num
0 1 2 3 4
num[2]=3; num 3
0 1 2 3 4
num[4]=2*num[2]; num 6
3
JUMAIL.WM Data Structure and Algorithm
// Element to delete
int deleteElement = 3;
// The last element is now a duplicate of the second-to-last element, so we can delete it
array[array.length - 1] = 0;
}
4
JUMAIL.WM Data Structure and Algorithm
Multi-Dimensional Array:
Multi-dimensional array can be considered as an array of arrays.
For example a two dimensional array may look like as follows:
table
0 1 2 3 4 5 6
0
0 1 2 3 4 5 6
1 This location can be referred to as table[1][5]
0 1 2 3 4 5 6
2
table 0 1 2 3 4 5 6
0
1 This location can be referred to as table[1][5]
2
Advantages of arrays:
Fast access if index is known
Easy to add an element in a particular location.
Disadvantages of arrays:
Fixed size
Same data type
Slow in searching an element
Slow in inserting/deleting an element
5
JUMAIL.WM Data Structure and Algorithm
What is Pointer?
Pointer contains memory address of a particular type of data.
In java, we use * to declare a pointer.
2cdf 1a5c c75c
p iptr 1a5c cptr c75c
p 9 iptr 5 cptr $ t # /0
9 1a5c $t#
2cdf 5 $
2cdf 1a5c
p 9 iptr 2cdf 5 cptr $ t # /0
In Java, a structure is not a built-in data type like int, float, or String. However, you can create your own
custom data types using classes and objects.
A class in Java is a blueprint for creating objects (a particular data structure), providing initial values for
state (member variables or attributes), and implementations of behavior (member functions or
methods).
// Constructor
public Car(String color, String model, int year) {
this.color = color;
this.model = model;
this.year = year;
}
// Methods
public String getColor() {
return color;
}
6
JUMAIL.WM Data Structure and Algorithm
E.g1:
data
data
data
data
next
next
next
next
head 5 9 6 7 /
E.g2:
data
data
data
data
next
next
next
next
head
front 5 9 6 7 /
rear
7
JUMAIL.WM Data Structure and Algorithm
E.g3:
previous
previous
previous
previous
data
data
data
data
next
next
next
next
head / 5 9 6 7 /
1. initializeList() head /
2. insertFirstElt(5)
data
next
head 5 /
3.insertAtFront(3)
data
next
head 5 /
data
next
newNode 3
4. insertAtEnd(8)
data
data
next
next
head 3 5
newNode 8 /
5. insertAfter(5,7)
data
data
data
next
next
next
head 3 5 8 /
newNode 7
8
JUMAIL.WM Data Structure and Algorithm
6. deleteElt(5):
data
data
data
data
next
next
next
next
head 3 5 7 8 /
last.next = new_node;
return;
}
prev.next = temp.next;
}
9
JUMAIL.WM Data Structure and Algorithm
llist.insert(1);
llist.insert(2);
llist.insert(3);
llist.insert(4);
llist.insert(5);
llist.insert(6);
llist.insert(7);
llist.insert(8);
10
JUMAIL.WM Data Structure and Algorithm
1. initializeStack()
2. p =isEmpty()
p = true
3. push(5)
5
4. push(7)
7
5
5. push(6)
6
7
5
11
JUMAIL.WM Data Structure and Algorithm
6. q = isEmpty(); r = isFull();
6
7
q = false; r = false 5
7. x = pop()
7
x=6 5
8. y = topElt()
7
y=7 5
1. initializeStack() 4
3
2
1
top = -1 0
2. p =isEmpty() 4
3
2
1
p = true top = -1 0
3. push(5) 4
3
2
1
top = 0 5 0
4. push(7) 4
3
2
7 1
top = 1 5 0
12
JUMAIL.WM Data Structure and Algorithm
5. push(6) 4
3
6 2
7 1
top = 2 5 0
6. q = isEmpty(); r = isFull(); 4
3
6 2
7 1
q = false; r = false top = 2 5 0
7. x = pop() 4
3
2
7 1
x=6 top = 1 5 0
8. y = topElt() 4
3
2
7 1
y=7 top = 1 5 0
maxSize = size;
top = -1;
13
JUMAIL.WM Data Structure and Algorithm
stackArray[++top] = value;
} else {
if(top >= 0) {
return stackArray[top--];
} else {
System.out.println("Stack is empty");
return -1;
if(top >= 0) {
return stackArray[top];
} else {
System.out.println("Stack is empty");
return -1;
}
14
JUMAIL.WM Data Structure and Algorithm
}
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
stack.peek();
stack.pop();
stack.pop();
stack.peek();
15
JUMAIL.WM Data Structure and Algorithm
Advantages of Stack:
Last-in-first-out access
Disadvantages of Stack:
Difficult to access other items
16
JUMAIL.WM Data Structure and Algorithm
What is Queue?
Queue is a data structure which is used to handle data in a first-in-first-out (FIFO) method. That is
we can remove the element which has been added earlier from the queue first.
Common operations of Queue are:
initializeQueue() – initializes the queue as empty queue.
enQueue()- adds an element at the rear of the queue.
deQueue()-removes and returns the front element from the queue.
frontElt()-returns the front element without removing it.
isEmpty() - returns true if the queue has no elements and false otherwise.
isFull() - returns true if the queue is full of elements and false otherwise.
displayQueue() - displays all elements from front to rear.
1. initializeQueue()
2. p=isEmpty()
p = true
3. enQueue(5) 5
4. enQueue(9)
enQueue(7) 5 9 7
5. x=deQueue() 9 7
x=5
6. enQueue(2)
enQueue(6) 9 7 2 6
7. q = isFull() 9 7 2 6
q = false
8. enQueue(3) 9 7 2 6 3
9. r = isFull()
y = deQueue() 7 2 6 3
r = true
y=9
0 1 2 3 4
1. initializeQueue()
front -1
rear -1
size 0
17
JUMAIL.WM Data Structure and Algorithm
0 1 2 3 4
2. p=isEmpty()
front -1
rear -1
p = true size 0
0 1 2 3 4
3. enQueue(5) 5
front -1
rear 0
size 1
0 1 2 3 4
4. enQueue(9) 5 9 7
enQueue(7)
front -1
rear 2
size 3
0 1 2 3 4
5. x=deQueue() 9 7
front 0
rear 2
x=5 size 2
0 1 2 3 4
6. enQueue(2) 9 7 2 6
enQueue(6)
front 0
rear 4
size 4
0 1 2 3 4
7. q = isFull() 9 7 2 6
front 0
rear 4
q = false size 4
0 1 2 3 4
8. enQueue(3) 3 9 7 2 6
front 0
rear 0
18
JUMAIL.WM Data Structure and Algorithm
size 5
0 1 2 3 4
9. r = isFull() 3 7 2 6
y = deQueue()
front 1
r = true rear 0
y=9 size 4
19
JUMAIL.WM Data Structure and Algorithm
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
stack.peek();
stack.pop();
stack.pop();
stack.peek();
20
JUMAIL.WM Data Structure and Algorithm
Advantages of Queue:
First-in-first-out access
Disadvantages of Queue:
Difficult to access other items
21
JUMAIL.WM Data Structure and Algorithm
Size=9 Height=4
3. Degree
The degree of a node is the number of its children.
The degree of a tree is the maximum degree of any of its nodes.
22
JUMAIL.WM Data Structure and Algorithm
4. Path
Path between two nodes in a tree is a sequence of edges which connect those nodes.
23
JUMAIL.WM Data Structure and Algorithm
4 8
/ 2 / 5 / / 7 / / 9 /
/ 3 /
24
JUMAIL.WM Data Structure and Algorithm
7. Sorting Algorithms
What is sorting?
Arranging items in ascending or descending order is called as sorting.
There are different types of sorting techniques each of which is good for some cases such as nearly
sorted, reversed, random, etc.
Pass1 4 7 72 6 32 65 9 56
Pass2 4 6 72 7 32 65 9 56
Pass3 4 6 7 72 32 65 9 56
Pass4 4 6 7 9 32 65 72 56
Pass5 4 6 7 9 32 65 72 56
Pass6 4 6 7 9 32 56 72 65
Pass7 4 6 7 9 32 56 65 72 Sorted
Pseudo Code:
swap(x, y)
t=x
x=y
y=t
Java Code:
25
JUMAIL.WM Data Structure and Algorithm
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
stack.peek();
stack.pop();
stack.pop();
stack.peek();
26
JUMAIL.WM Data Structure and Algorithm
Pass1 6 7 4 32 65 9 56 72
Pass2 6 4 7 32 9 56 65 72
Pass3 4 6 7 9 32 56 65 72
Pass4 4 6 7 9 32 56 65 72
Pass5 4 6 7 9 32 56 65 72 Sorted
Pseudo Code:
bubbleSort(a[],n) //Let ‘a’ be an array containing ‘n’ items
max = n-2
swapped = true
while (max>0 AND swapped=true)
swapped = false
for j = 0 to max
27
JUMAIL.WM Data Structure and Algorithm
Java Code:
int n = arr.length;
arr[j] = arr[j+1];
arr[j+1] = temp;
int n = arr.length;
System.out.println();
ob.bubbleSort(arr);
System.out.println("Sorted array");
ob.printArray(arr);
28
JUMAIL.WM Data Structure and Algorithm
Efficiency of the Sort Algorithms (Best, Worst and Average Case Comparison):
29
JUMAIL.WM Data Structure and Algorithm
8. Searching Algorithm
What is search algorithm?
A search algorithm is an algorithm for finding an item among a collection of items.
Pseudo code:
int sequentialSearch(a[],n,t) //It returns the location of the target t in the array a[] with n elements.
for i = 0 to n-1
if (a[i]=t) return i;
next i
return -1;
Java Implementation:
30
JUMAIL.WM Data Structure and Algorithm
Pseudo code:
int binarySearch(a[],l,u,t) //It returns the location of t in the array a[] from the index l to u.
p = ( l + u) / 2;
while(a[p] ≠ t AND l<=u)
if (a[p] > t)
u=p-1
else
l=p+1
p = (l + u) / 2
end while
if (l <= u)
return p
else
return -1
31
JUMAIL.WM Data Structure and Algorithm
Java Implementation:
int l = 0, r = arr.length - 1;
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
return -1;
int n = arr.length;
int x = 10;
if (result == -1)
else
}
32
JUMAIL.WM Data Structure and Algorithm
}
33
JUMAIL.WM Data Structure and Algorithm
34