0% found this document useful (0 votes)
47 views9 pages

Dsa Reviewer 1

Data structures and algorithms are important programming concepts. Data structures, such as lists, arrays, stacks and queues, are ways of organizing data for efficient use. Algorithms specify the steps to solve computational problems. Common algorithms include searching, sorting, and recursion. Searching algorithms like binary search and sorting algorithms like quicksort and merge sort are examples of efficient approaches.

Uploaded by

kenneth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views9 pages

Dsa Reviewer 1

Data structures and algorithms are important programming concepts. Data structures, such as lists, arrays, stacks and queues, are ways of organizing data for efficient use. Algorithms specify the steps to solve computational problems. Common algorithms include searching, sorting, and recursion. Searching algorithms like binary search and sorting algorithms like quicksort and merge sort are examples of efficient approaches.

Uploaded by

kenneth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

DATA STRUCTURES AND ALGORITHMS

Data Structure

- Programmatic way of storing data so that it can be used efficiently and will allow the most
efficient algorithm to be used.
- The functional definition of a data structure is known as Abstract Data Type (ADT), which is
independent of implementation.
- The way of which the data is organized affects the performance of a program for different
tasks.
- Examples: lists, arrays, stacks, queues, heaps, trees, and graphs.

Classification of Data Structures

o Simple Data Structures


- can be constructed with the help of primitive data structure.
• Primitive Data Structure – used to represent the standard date types of any of the
computer languages.
• Examples: variables, arrays, pointers, structures, and unions.
o Compound Data Structures
- can be constructed with the help of one or more primitive data structures.
- has as specific functionality.
- designed by the user.
• Linear Data Structure
- continuous arrangement of data elements in the memory.
- can be constructed by using array data type.
- relationship of adjacency is maintained between the data elements.
- Example: stack, queue, tables, list, and linked lists.
• Operations Applied on Linear Data Structure
1. Add an element.
2. Delete an element.
3. Traverse.
4. Sort the list of elements.
5. Search for a data element.
• Non-linear Data Structure
- collection of randomly distributed set of data item joined together by using a special
pointer (tag).
- relationship of adjacency is NOT maintained between the data items.
- Example: tree, decision tree, graph, and forest.
- Operations Applied on Non-linear Data Structure
1. Add elements.
2. Delete elements.
3. Display the elements.
4. Sort the list of elements.
5. Search for a data element.

Abstract Data Type (ADT)

- a logical description of how we view the data and the operations that are allowed without regard
to how they will be implemented.
• Information hiding – encapsulating the details of the implementation/hiding them from the
user’s view.

Algorithms

• Structure of Algorithm
1. Input Step
2. Assignment Step
3. Decision Step
4. Repetitive Step
5. Output Step
• Properties of Algorithm
o Finiteness – must terminate after a finite number of steps.
o Definiteness – steps must be precisely defined or unambiguously specified.
o Generality – generic enough to solve all the problems of a particular class.
o Effectiveness – operations of the algorithm must be basic enough to put down on pencil and
paper. They should not be too complex to warrant writing another algorithm.
o Input-Output – must have certain initial and precise inputs, and outputs that may be
generated both as its intermediate and final steps.

DIFFERENT APPROACHES TO DESIGN AN ALGORITHM

Searching – process of finding a particular data item from a collection of data items based on
specific criteria. A search typically performed using a search key and it answers either True or False
based on the item is present or not in the list.

Searching Techniques

• Linear Search – simplest algorithm to do


if key in theArray:
sequential search and involves iterating
print(“The key is in the array”)
over the sequence and checking one item at
else:
a time until the desired item is found or all
print(“The key is not in the array.”)
the items have been examined.
while True:
if middleItem == target value:
• Binary Search – the target key is examined
return True
in a sorted sequence and this algorithm
else if middleItem > target value:
starts with the middle item of the sorted
search in the first half of the list
sequence.
else if middleItem > target value:
search in the second half of the list
Sorting – various methods of arranging or ordering things based on criteria’s (numerical,
chronological, alphabetical, hierarchical, etc.)

Sorting Techniques

BubbleSort(Array, n) {
for i = 0 to n-2 {
for j = 0 to n-2 {
• Bubble sort/exchange sort – arranges if Array[j] > Array[j+1] {
values by iterating over the list several swap(Array[j], Array[j+1])
times and in each iteration, the larger value }
gets bubble up to the end of the list. }
}
}

SelectionSort(Array, n) {
• Selection sort - a simple sorting algorithm for i = 0 to n-1 {
that sorts an array by repeatedly finding the min_idx = i
minimum element from the unsorted part for j = i+1 to n {
and moving it to the beginning. It improves if Array[j] < Array[min_idx] {
over bubble sort by making only one min_idx = j
exchange per pass. It maintains two }
subarrays: one sorted and one unsorted. In }
each iteration, the minimum element is swap(Array[min_idx], Array[i])
moved from the unsorted to the sorted }
subarray. }

InsertionSort(Array, n) {
for i = 1 to n {
key = Array[i]
• Insertion Sort - considers the elements one j=i-1
at a time, inserting each in its suitable place while (j >= 0 and Array[j] > key) {
among those already considered (keeping Array[j + 1] = Array[j]
them sorted). Insertion sort is an example of j=j-1
an incremental algorithm. It builds the }
sorted sequence one number at a time. Array[j + 1] = key
}
}

QuickSort(Array, low, high) {


• Quick Sort - a divide and conquer algorithm.
if (low < high) {
Quick sort first divides a large list into two
pi = Partition(Array, low, high)
smaller sub-lists: the low elements and the
QuickSort(Array, low, pi - 1)
high elements. Quick sort can then
QuickSort(Array, pi + 1, high)
recursively sort the sub-lists. The steps are:
}
1. Pick an element, called a pivot, from the }
list.
2. Reorder the list so that all elements with Partition(Array, low, high) {
values less than the pivot come before pivot = Array[high]
the pivot, while all elements with values i = low - 1
greater than the pivot come after it for j = low to high-1 {
(equal values can go either way). After if Array[j] <= pivot {
this partitioning, the pivot is in its final i=i+1
position. This is called the partition swap(Array[i], Array[j])
operation. }
3. Recursively apply the above steps to the }
sub-list of elements with smaller values swap(Array[i + 1], Array[high])
and separately the sub-list of elements return (i + 1)
with greater values. }

Merge(Array, left, mid, right) {


n1 = mid - left + 1
n2 = right - mid
LeftArray[n1], RightArray[n2]
for i = 0 to n1 {
LeftArray[i] = Array[left + i]
}
for j = 0 to n2 {
RightArray[j] = Array[mid + 1 + j]
}
i = 0, j = 0, k = left
• Merge Sort
while (i < n1 and j < n2) {
1. Divide the input which we must sort
if LeftArray[i] <= RightArray[j] {
into two parts in the middle. Call it the
Array[k] = LeftArray[i]
left part and right part.
i++
2. Sort each of them separately. Note
} else {
that here sort does not mean to sort
Array[k] = RightArray[j]
it using some other method. We use
j++
the same function recursively.
}
3. Then merge the two sorted parts.
k++
}
while (i < n1) {
Array[k] = LeftArray[i]
i++
k++
}
while (j < n2) {
Array[k] = RightArray[j]
j++
k++
LINEAR DATA STRUCTURES

Stack

- a container of objects that are inserted and removed according to the last-in first-out (LIFO)
principle.
- a limited access data structure (elements can be added and removed from the stack only at the
top.
- may be implemented to have a bounded capacity.
o overflow state – stack is full, and no items can be pushed.
o underflow state – stack is empty, and no items can be popped.
• Stack ADT
- works on the last-in first-out (LIFO) principle.
- insertion and deletion take place at one end called TOP.
▪ PUSH – add operation of the stack.
▪ POP – delete operation of the stack.
▪ Stack Overflow – PUSH operation on a full stack.
▪ Stack Underflow – POP operation on an empty stack.
▪ SP is a pointer used to access the top element of the stack.
• Stack Using Arrays
o Preliminaries
▪ Create a one-dimensional array with fixed size (int stack[SIZE])
▪ Define an integer variable 'top' and initialize with '-1'. (int top = -1)
o PUSH o POP
▪ if top == SIZE-1: ▪ if top == -1:
print(“Stack Overflow”) print(“Stack Underflow”)
else: else:
top++ delete stack[top]
stack[top] = data top—
o DISPLAY
▪ if top == -1:
print(“Stack Empty”)
else:
for i in range(-1, top+1):
print(stack[top])
• Stack Applications
o Stack is used by compilers to check for balancing of parentheses, brackets, and braces.
o Stack is used to evaluate a postfix expression.
o Stack is used to convert an infix expression into postfix/prefix form.
o In recursion, all intermediate arguments and return values are stored on the processor’s
stack.
o During a function call the return address and arguments are pushed onto a stack and on
return they are popped off.
o Depth first search uses a stack data structure to find an element from a graph.

Queue

- data structure that is based on first-in first-out (FIFO) principle.


- Items are inserted at one end called the REAR and deleted at the other end called the FRONT.
• Queue Using Arrays
o Preliminaries
▪ Create a one-dimensional array with fixed size (int queue[SIZE])
▪ FRONT = REAR = O
o ENQUEUE (INSERT) o DEQUEUE (DELETE)
▪ if REAR >= SIZE-1: ▪ if REAR == 0:
print(“Overflow”) print(“Stack Underflow”)
else: else:
queue[REAR] = data for i in range(0, REAR-1):
REAR++ queue[i] = queue[i + 1]
REAR—
o DISPLAY
▪ if (front == rear):
print("Queue is Empty ")

else:
for i in range(FRONT, REAR-1):
print(queue[i])
• Applications of Queue
o Used to schedule jobs to be processed by the CPU.
o When multiple users send print jobs to a printer, each printing job is kept in the printing
queue.
o Breadth first search uses a queue data structure to find an element from a graph.
• Disadvantages of Linear Queue
o Time consuming – linear time to be spent in shifting the elements to the beginning of the
queue.
o Signaling queue full even if the queue is having vacant position.
• DEQUE/DEQUEUE (Double-ended Queue)
- generalizes a queue, for which elements can be added or removed from either the front or
the rear.
- Also known as head-tail linked list.
o DEQUE REPRESENTATIONS ▪ Output-restricted DEQUE (ORD)
▪ Input-restricted DEQUE (IRD) - allows deletion from only one
- allows insertions from only one end.
end. o DEQUE OPERATIONS
▪ Insert element at FRONT. ▪ Remove element at FRONT.
▪ Insert element at REAR. ▪ Remove element at REAR.

LINKED LIST (skipped for now)

NON-LINEAR DATA STRUCTURES

Tree

- a non-empty set, one element of which is designated the root of the tree, while the remaining
elements are partitioned into non-empty sets, each of which is a subtree of the root.
- a tree T is a set of needs storing elements such that the nodes have a parent-child relationship
that satisfies the following:
▪ If T is not empty, T has a special tree called the root that has no parent.
▪ Each node v of T different than the root has a unique parent node w; each node with
parent w is a child of w.
• Binary Tree
- each node can have at most two children: a left child and a right child. The topmost node is
the root. The left subtree and right subtree attached to the root can be either empty or
smaller binary trees.
• Tree Terminologies
o Leaf node/external node – a node with no children.
o Internal node – a node which is not a leaf.
o Path - A sequence of nodes n1, n2, . . ., nk, such that ni is the parent of ni + 1 for i = 1, 2,. . ., k - 1.
The length of a path is 1 less than the number of nodes on the path. Thus, there is a path of
length zero from a node to itself.
o Siblings – the children of the same parent.
o Ancestor – If there is a path from node A to node B, then A is called an ancestor of B.
o Descendant - If there is a path from node A to node B, then B is called a descendant of A.
o Subtree – any node of a tree, with all its descendants.
o Level – distance from the root. The root has level 0. The maximum number of nodes at any
level is 2n.
o Height of the tree – maximum level in a tree.
o Height of the node - the length of the longest path from the node to a leaf.
o Depth - another term used to denote the height of the tree.
• Assigning Level Numbers and Numbering Nodes for A Binary Tree
o The nodes of a binary tree can be numbered in a natural way, level by level, left to right.
o The nodes of a complete binary tree can be numbered so that the root is assigned the
number 1, a left child is assigned twice the number assigned its parent, and a right child is
assigned one more than twice the number assigned its parent.
• Properties of Binary Trees
o If h = height of a binary tree, then
▪ 2h – maximum number of leaves
▪ (2h+1 – 1) – maximum number of nodes
o If a binary tree contains m nodes at level L, it contains a most (2*m) nodes at level (L + 1).
o The maximum number of nodes at any level L is 2^L.
o The total number of edges in a full binary tree with n node is n - 1.
• Types of Binary Trees
o Complete Binary Tree
▪ A binary tree in which all levels are fully filled except possibly the last, which is filled
from left to right. It contains the first n nodes when nodes are numbered from 1 to n from
top to bottom, left to right.
o Perfect Binary Tree
▪ A binary tree in which all internal nodes have two children, and all leaves are at same
level.
o Balanced Binary Tree
▪ A binary tree is balanced if height of the tree is O(log n) where n is number of nodes.
▪ A binary tree that for every node, the difference in height between its left and right
subtree is no more than 1. If this condition is true for all nodes, then the tree is balanced.
• Array Representation of Binary Tree
o Nodes are indexed from 0 (root) onwards.
o Indexing is done level by level, from left to right, top to bottom.
o Empty nodes are also indexed.
o Each node with index i is stored as the ith element in an array.
o For a node at index n, its left child is at (2n + 1) and right child
at (2n + 2).
o If a node doesn’t have a child, null is stored at the
corresponding array index.
• Binary Tree Traversals
- visiting each node in the tree exactly once.
o Inorder Traversal o Preorder Traversal
▪ Visit all the nodes in the left subtree. ▪ Visit root node.
▪ Visit the root node. ▪ Visit all the nodes in the left subtree.
▪ Visit all the nodes in the right ▪ Visit all the nodes in the right
subtree. subtree.
o Postorder Traversal
▪ Visit all the nodes in the left subtree.
▪ Visit all the nodes in the right subtree.
▪ Visit the root node.
• Binary Search Tree
- a node-based binary tree data structure.
• Properties of Binary Search Tree
o The left sub-tree of a node with keys less than the node’s key.
o The right sub-tree of a node contains only nodes with keys greater than the node’s key.
o The left and right sub-tree each must also be a binary search tree. There must be no
duplicate nodes.
• Searching a key
o To search a given key in Binary Search Tree, we first compare it with root, if the key is present
at root, we return root. If key is greater than root’s key, we recur for right sub-tree of root
node. Otherwise, we recur for left sub-tree.

You might also like