0% found this document useful (0 votes)
15 views

Practicle File Dsa 1

practicle in dsa

Uploaded by

sweta.23bce11754
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Practicle File Dsa 1

practicle in dsa

Uploaded by

sweta.23bce11754
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

PRACTICAL FILE

CSE2002- DATA STRUCTURES AND ALGORITHMS


Class No. BL2024250100286
Slot: B14+C14+D14+E14+F14

Course Instructor
Dr Antima Jain
Assistant Professor Senior Grade 1
School of Computing Science and Engineering
VIT Bhopal University, Sehore
Madhya Pradesh

Name :SWETA KUMARI


Registration Number :23BCE11754
Date of Submission :8-8-2024
Index page no.
1 Implementation of Sorting Algorithms (Bubble Sort, 1
Merge sort and Quick sort)
2 Implementation of Searching Algorithms (Linear and Binary) 7
3 Implementation of Single Linked List. 12
4 Implementation Double Linked List. 16
5 Implementation Stack using Array and Linked List. 18
6 Implementation of Queue using Array and Linked List. 26
7 Implementation of Binary Tree Traversals. 30
8 Implementation of Binary Search Tree. 34
9 Implementation of BFS and DFS. 37
10 Implementation of Dijkstra Shortest Path Algorithms. 40
11 Implementation of Prims and Kruskal Algorithms. 43
12 Implementation Hashing Techniques 48
Aim-Implementation of Sorting Algorithms (Bubble Sort, Merge sort
and Quick sort)
Algorithm-
Bubble Sort
1. i = 0
2. while i < n-1
• j=0
• while j < n-i-1
• if arr[j] > arr[j+1]
• swap arr[j] and arr[j+1]
• j = j+1
• i = i+1
Merge Sort
1. if left < right
• mid = left + (right-left)/2
• merge_sort (arr, left, mid)
• merge_sort (arr, mid+1, right)
• merge (arr, left, mid, right)
Quick Sort
1. if low < high
• pivot = partition(arr, low, high)
• quick_sort(arr, low, pivot-1)
• quick_sort(arr, pivot+1, high)
Input-
Output-
Bubble sort
Aim- Implementation of Searching Algorithms (Linear and Binary)
Algorithm-
Linear search
1. Initialize index to -1
2. For i from 0 to n-1: a. If A[i] is equal to x:
i. Set index to i ii. Break out of the loop
3. Return index
Binary search
1. Initialize low to 0
2. Initialize high to n-1
3. While low is less than or equal to high: a. Calculate mid as (low
+ high) / 2 b. If A[mid] is equal to x: i. Return mid c. Else
if A[mid] is less than x: i. Set low to mid + 1 d. Else:
i. Set high to mid - 1
4. Return -1 (target value not found)
Input-
Linear search
Binary search-
Output-
Linear search output
Aim- Implementation of Single Linked List.
Algorithm-
1.
2. Create a new node:
• newNode ← createNode(data)
• newNode.data ← data
• newNode.next ← NULL
3. Update the head of the list:
• head ← newNode
• newNode.next ← head
4. Return the updated head:
Input-
Output-
Aim-Implementation Double Linked List
Algorithm-
1. Create a new node:
• newNode ← createNode(data)
• newNode.data ← data
• newNode.next ← NULL
• newNode.prev ← NULL
2. Check if the list is empty:
• if head == NULL then
• head ← newNode
• else
• newNode.next ← head
• head.prev ← newNode
• head ← newNode
3. Return the updated head:
• return head
Input-
Output-
Aim-Implementation Stack using Array and Linked List.
Algorithm-
Array- Here are the formal step-by-step algorithms for implementing
a stack using an array and a linked list:
Array Stack Algorithm
1. Initialize an empty array stack of size n.
2. Set top to -1.
3. Push(x):
• If top == n-1, throw an error.
• Increment top.
• Set stack[top] = x.
4. Pop():
• If top == -1, throw an error.
• Decrement top.
• Return stack[top+1].
5. IsEmpty():
• Return top == -1.
Linked List Stack Algorithm
1. Initialize an empty linked list stack.
2. Set head to null.
3. Push(x):
• Create a new node n with value = x.
• Set n.next = head.
• Set head = n.
4. Pop():
• If head == null, throw an error.
• Set temp = head.
• Set head = head.next.
• Return temp.value.
5. IsEmpty():
• Return head == null.
Output-
Aim-Implementation of Queue using Array and Linked List.
Algorithm-
Queue using Array
1. Initialize front and rear to 0.
2. Enqueue: rear++, store element at rear index.
3. Dequeue: front++, return element at front index.
4. Check empty: front == rear.
5. Check full: rear == MAX_SIZE - 1.
Queue using Linked List
1. Initialize front and rear to NULL.
2. Enqueue: create new node, set rear->next to new node,
update rear.
3. Dequeue: store front node's data, update front to front->next,
free dequeued node.
4. Check empty: front == NULL.

Input-
Array implementation-

-
Linked list implementation of queue-
Output-
Aim-Implementation of Binary Tree Traversals.
Algorithm- preorder-
PreOrderTraversal(root)
2 if root == NULL then
3 return
4 end if
5 Visit(root)
6 PreOrderTraversal(root->left)
7 PreOrderTraversal(root->right)
8end PreOrderTraversal
Inorder-
InOrderTraversal(root)
2 if root == NULL then
3 return
4 end if
5 InOrderTraversal(root->left)
6 Visit(root)
7 InOrderTraversal(root->right)
8end InOrderTraversal
Postorder-
PostOrderTraversal(root)
2 if root == NULL then
3 return
4 end if
5 PostOrderTraversal(root->left)
6 PostOrderTraversal(root->right)
7 Visit(root)
8end PostOrderTraversal
Input-

-
Output-
Aim- Implementation of Binary Search Tree.
Algorithm-
1. Insert Node
• If root is NULL, create a new node with data and return it.
• If data is less than root's value, recursively call InsertNode on
the left subtree.
• If data is greater than root's value, recursively
call InsertNode on the right subtree.
• If data is equal to root's value, return root (no duplicates
allowed).
2. Search Node
• If root is NULL, return FALSE (node not found).
• If data is equal to root's value, return TRUE (node found).
• If data is less than root's value, recursively call SearchNode on
the left subtree.
• If data is greater than root's value, recursively
call SearchNode on the right subtree.
3. Delete Node
• If root is NULL, return root (node not found).
• If data is less than root's value, recursively call DeleteNode on
the left subtree.
• If data is greater than root's value, recursively
call DeleteNode on the right subtree.
• If data is equal to root's value, delete the node and return the
updated root node.
4. Traversal
• In-Order Traversal: Recursively traverse the left subtree, visit
the current node, and recursively traverse the right subtree.
• Pre-Order Traversal: Visit the current node, recursively traverse
the left subtree, and recursively traverse the right subtree.
• Post-Order Traversal: Recursively traverse the left subtree,
recursively traverse the right subtree, and visit the current node
Input-
Output-
Aim- Implementation of BFS and DFS
Algorithm-
Breadth first search
1. Create an empty queue Q
2. Initialize Q with the root node R as the first element
3. While Q is not empty a. Dequeue a
node N from Q b. Process N (e.g., print its value) c. Enqueue all
children of N into Q (if any) i. If N has a left child, enqueue it
into Q ii. If N has a right child, enqueue it into Q
4. Repeat step 3 until Q is empty
Depth first search-
1. Create a recursive function DFS
2. Call DFS with the root node R as the argument
3. In the DFS function a. Process the current node N (e.g., print its
value) b. Recursively call DFS on the left subtree of N (if any)
c. Recursively call DFS on the right subtree of N (if any)
4. Return from the DFS function
Input-
output-
Aim- Implementation of Dijkstra Shortest Path Algorithms.
Algorithm:
1. Create a distance array dist and a visited array visited of size V.
2. Initialize the distance array with infinity (INT_MAX) and the
visited array with 0.
3. Set the distance to the source vertex to 0.
4. Repeat the following steps until all vertices are visited: a. Find
the vertex u with the minimum distance value that has not
been visited. b. Mark u as visited. c. Update the distance values
of all adjacent vertices v of u if the distance to v through u is
less than the current distance value of v.
5. Print the shortest distance array.
Output-
Aim- Implementation of Prims and Kruskal Algorithms.
Algorithm-
Prims algorithm-
1. Choose an arbitrary vertex s in V as the starting vertex.
2. Initialize an empty tree T and a set U of vertices that have been
included in T.
3. Initialize a priority queue Q with all edges incident on s, where
the priority of each edge is its weight.
4. While Q is not empty: a. Extract the edge e with the minimum
priority (i.e., the edge with the minimum weight) from Q. b.
If e connects a vertex u in U to a vertex v not in U, then: i.
Add e to T. ii. Add v to U. iii. Add all edges incident on v to Q.
5. Return T as the minimum spanning tree of G.
Kruskal algorithm-
1. Sort all edges in E in non-decreasing order of their weights.
2. Initialize an empty tree T and a disjoint set D of vertices.
3. For each edge e in the sorted order: a. Find the roots u and v of
the trees in D that contain the endpoints of e. b. If u and v are
in different trees, then: i. Add e to T. ii. Merge the trees
containing u and v into a single tree in D.
4. Return T as the minimum spanning tree of G.
Input-
Prim’s algorithm
Kruskal's Algorithm
1.prims Output-

2.kruskal
Aim- Implementation Hashing Techniques
Chaining (Separate Chaining) Algorithm:
Hash Table Insertion Algorithm:
1. Input: key and value to be inserted into the hash table.
2. Hash Function: Calculate the hash value h of the key using a
hash function: h = hash_function(key).
3. Index Calculation: Calculate the index i of the hash table where
the key will be stored: i = h % TABLE_SIZE.
4. Collision Resolution: If the bucket at index i is empty, create a
new node with the key and value and store it in the bucket.
Otherwise, traverse the linked list at the bucket and append a
new node with the key and value to the end of the list.
5. Return: Return successfully inserted.
Hash Table Search Algorithm:
1. Input: key to be searched in the hash table.
2. Hash Function: Calculate the hash value h of the key using a
hash function: h = hash_function(key).
3. Index Calculation: Calculate the index i of the hash table where
the key might be stored: i = h % TABLE_SIZE.
4. Collision Resolution: Traverse the linked list at the bucket and
search for a node with the matching key. If found, return the
associated value. If not found, return a special value indicating
not found (e.g., -1).
5. Return: Return the search result.
Open Addressing (Linear Probing) Algorithm:
Hash Table Insertion Algorithm:
1. Input: key and value to be inserted into the hash table.
2. Hash Function: Calculate the hash value h of the key using a
hash function: h = hash_function(key).
3. Index Calculation: Calculate the initial index i of the hash table
where the key will be stored: i = h % TABLE_SIZE.
4. Collision Resolution: If the bucket at index i is empty, store
the key and value in the bucket. Otherwise, probe other
buckets in a linear sequence (e.g., i = (i + 1) % TABLE_SIZE) until
an empty bucket is found. Store the key and value in the empty
bucket.
5. Return: Return successfully inserted.
Hash Table Search Algorithm:
1. Input: key to be searched in the hash table.
2. Hash Function: Calculate the hash value h of the key using a
hash function: h = hash_function(key).
3. Index Calculation: Calculate the initial index i of the hash table
where the key might be stored: i = h % TABLE_SIZE.
4. Collision Resolution: Probe other buckets in a linear sequence
(e.g., i = (i + 1) % TABLE_SIZE) until a bucket with a
matching key is found or an empty bucket is found. If a
matching key is found, return the associated value. If an empty
bucket is found, return a special value indicating not found
(e.g., -1).
5. Return: Return the search result.
Input- Chaining (Separate Chaining)
Open Addressing (Linear Probing)
Output-

You might also like