Data Structure - Jumail
Data Structure - Jumail
What is sorting?
Arranging items in ascending or descending
order is called as sorting.
There are different types of sorting techniques
Advantages of Binary Tree: each of which is good for some cases such as
Quick search, Quick inserts, Quick deletes nearly sorted, reversed, random, etc.
(If the tree remains balanced)
Disadvantages of Binary Tree: What is search algorithm?
Deletion algorithm is complex A search algorithm is an algorithm for finding
2. Binary Search Tree: an item among a collection of items.
It is a binary tree such that for every node Sequential/Linear Search Algorithm:
N in the tree. It examines the first element in the list and
All keys in N's left sub tree are less than then second element and so on until a much is
the key in N, and All keys in N's right sub found.
tree are greater than the key in N.
5|Page
Why need Algorithm Analysis Briefly describe the enqueue and dequeue operations
Generally, there are multiple of the queue data structure using diagrams.
approaches to solve one problems 1.Enqueue (insert):
statement. Algorithm analysis is *Check if the queue is full.
performed to figure out which is *If the queue is full, produce overflow error and exit.
the better algorithms out of this *If the queue is not full increment rear point the next
options. empty space.
What is better algorithm *Add data element to the queue location where the
Faster time complexity rear is pointing.
Less Memory (Space Complexity
REAR FRONT
Easy to read
Less line of code
Before
How to Determine Best D C B A
Algorithm
1.Time Complexity D C B A After
2.Space Complexity
Time Complexity
REAR FRONT
It is amount of time taken by
algorithm to run. The input
2.Dequeue (insert):
processed by an algorithm helps in
*Check if the queue.
determine the time complexity.
*If the queue is empty produce under flow error and
Space Complexity
exit.
It is amount of memory or space
*If the queue is not empty access the data where front
taken by algorithm to run. The
is pointing.
memory required to process the
*Increment front pointer to point next available data
input by an algorithm helps in
element.
determining the space
complexity.
Asymptotic analysis helps in
evaluating performance of
Different Stack and Queue
an algorithm in terms of input size
Stack:
and its increase.
*Last in first out. (LIFO)
Rules of Big O (O) Notation
*Push, POP, Peek.
*Running PC it has single *Stack of plate.
processor assume
*It performs sequential Execution Queue:
of statements *First in first out. (FIFO)
*Assignment operation takes 1 *Enqueue, dequeuer, front.
unit of time *Queue in a grocery store.
*Return statement takes 1 unit of
time
*Arithmetical operation takes 1
unit of time
6|Page
Different between Array and Linked Difference between Linear and Non-linear Data
list ? Structures:
Array: Linear Data Structures:
*Providing direct access to any Elements are arranged in a linear or sequential
element using its index. order. Examples include arrays, linked lists, queues,
*making it less flexible in terms to
and stacks.
size adjustment.
Non-linear Data Structures:
Linked list:
*Utilizes noncontiguous memory Elements are not arranged in a sequential order.
locations. Examples include: trees and graphs. Non-linear
*Can dynamically adjust list sixe structures allow for more complex relationships
during runtime by adding or between elements.
removing node. Two Applications of LinkedList:
Memory Management:
Different between Singly Linked List Linked lists are used in dynamic memory allocation
and Doubly Linked list? where memory can be efficiently allocated and
Singly: Each node contains a data deallocated.
element next node. Implementation of Data Structures:
Double: Each node contains a data Linked lists are a fundamental building block for
element and references of both the implementing other data structures like stacks,
next previous nodes in the queues, and symbol tables.
sequences. Two Implementations of Queue Data Structure:
Best case efficiency is the minimum Using Arrays:
number of steps that an algorithm Elements are stored in an array, and two pointers
can take any collection of data (front and rear) are used to
values. maintain the queue.
Worst case efficiency is the Using Linked List:
maximum number of steps that an Nodes are linked, and pointers (front and rear)
algorithm can take for any collection indicate the start and end of
of data values. the queue.
Average case efficiency Difference between Perfect Binary Tree and Full
Is the efficiency averaged on all Binary Tree:
possible input. Must assume a Perfect Binary Tree:
distribution of the input. All levels are completely filled with nodes.
Abstract Data Types (ADTs): The number of nodes is 2^{h+1} – 12 h+1 −1
Abstract Data Types refer to high- Full Binary Tree:
level descriptions of data structures Every node has either 0 or 2 children.
that specify the behavior rather than No node has only one child.
the implementation details. They
define a set of operations that can be
performed on the data, without
specifying how these operations are
implemented. Examples include
stacks, queues, lists, and trees.
7|Page
Write a java program that stores the given marks of the student and display the average
mark of each student. Use 2D array to store the subject marks. Mathematics Science
75 62 88 85 82 90
Write a java code that calculate and returns the sum of all the even numbers of a given
array.
public class SumOfEvenNumbers {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sumOfEvenNumbers = calculateSumOfEvenNumbers(numbers);
System.out.println("Sum of Even Numbers: " + sumOfEvenNumbers);
}
public static int calculateSumOfEvenNumbers(int[] array) {
int sum = 0;
for (int number : array) {
if (number % 2 == 0) {
sum += number;
}
}
return sum;
}
}
9|Page
Write a java code for find the maximum and minimum elements in the following array.
78, 65,12, 48, 27, 86
public class FindMaxMin {
public static void main(String[] args) {
int[] numbers = {78, 65, 12, 48, 27, 86};
int max = findMax(numbers);
int min = findMin(numbers);
System.out.println("Maximum Element: " + max);
System.out.println("Minimum Element: " + min);
}
public static int findMax(int[] array) {
int max = array[0];
for (int number : array) {
if (number > max) {
max = number;
}
}
return max;
}
public static int findMin(int[] array) {
int min = array[0];
for (int number : array) {
if (number < min) {
min = number; Write java code to reverse the inserted string
} using a for loop.
} Note: Reverse string of "Hello" would be "olleH"
return min;
} public class ReverseString {
} public static void main(String[] args) {
String original = "Hello";
String reversed = reverseString(original);
System.out.println("Original String: " + original);
System.out.println("Reversed: " + reversed);
}
Stack Algorithm
Queue Algorithm
Insert Algorithm for Queue Display Algorithm for Delete Algorithm for Queue
Insert (item) Queue {
{ { If front = rear
If rear = max -1 then If front=rear print “queue is empty”
print “ queue is full” print “queue is empty “
else else
{ else Increment front
Increment rear For i = front to rear Array[front]= NULL;
Queue [rear]=item; Print queue [i]; }
} }