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

ALGORITHM

Uploaded by

arul85700
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

ALGORITHM

Uploaded by

arul85700
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

EX:NO:

LIST ADT USING ARRAY & LIST ADT USING ARRAY


DATE:

AIM:
To Write a program to implement the List ADT using arrays and linked lists.

ALGORITHM:
i) Algorithm List ADT using arrays.

1. Initialize an empty array to store elements.


2. Implement the following operations:
a. is_empty(): Return True if the array is empty, False otherwise.
b. append(item): Add the item to the end of the array.
c. insert(index, item): Insert the item at the specified index.
d. remove(item): Remove the first occurrence of the item from the array.
e. index(item): Return the index of the first occurrence of the item.
f. count(item): Return the number of occurrences of the item in the array.
g. size(): Return the number of elements in the array.
h. get(index): Return the item at the specified index.
i. set(index, item): Set the item at the specified index to the new item.
j. clear(): Remove all elements from the array.

ii) Algorithm linked lists.

1. Define a Node class with attributes data and next.


2. Initialize an empty linked list with a head pointer pointing to None.
3. Implement the following operations:
a. is_empty(): Return True if the linked list is empty, False otherwise.
b. append(item): Add the item to the end of the linked list.
c. insert(index, item): Insert the item at the specified index.
d. remove(item): Remove the first occurrence of the item from the linked
list.
e. index(item): Return the index of the first occurrence of the item.
f. count(item): Return the number of occurrences of the item in the linked
list.
g. size(): Return the number of elements in the linked list.
h. get(index): Return the item at the specified index.
i. set(index, item): Set the item at the specified index to the new item.
j. clear(): Remove all elements from the linked list.
EX:NO:

STACK ADT & QUEUE ADT


DATE:

AIM:
To Write a programs to implement the following using a singly linked list. Stack ADT
Queue ADT.

ALGORITHM:
i)Stack
1. Define a Stack class with the following operations:
a. is_empty(): Return True if the stack is empty, False otherwise.
b. push(item): Add an item to the top of the stack.
c. pop(): Remove and return the item from the top of the stack.
d. peek(): Return the item at the top of the stack without removing it.
e. size(): Return the number of elements in the stack.
f. clear(): Remove all elements from the stack
ii)Queue
1. Define a Queue class with the following operations:
a. is_empty(): Return True if the queue is empty, False otherwise.
b. enqueue(item): Add an item to the rear of the queue.
c. dequeue(): Remove and return the item from the front of the queue.
d. front(): Return the item at the front of the queue without removing it.
e. rear(): Return the item at the rear of the queue without removing it.
f. size(): Return the number of elements in the queue.
g. clear(): Remove all elements from the queue.
EX:NO:

INFIX TO POSTFIX USING STACK


DATE:

AIM:
To Write a program that reads an infix expression, converts the expression to postfix
form and then evaluates the postfix expression (use stack ADT).

ALGORITHM:

1. Initialize an empty stack and an empty string for the output postfix expression.
2. Scan the infix expression from left to right.
3. For each character in the infix expression:
a. If it's an operand, append it to the output string.
b. If it's an operator:
i. Pop and append operators from the stack to the output until the stack is
empty, or the top of the stack has lower precedence than the current operator, or the
top of the stack is an opening parenthesis "(".
ii. Push the current operator onto the stack.
c. If it's an opening parenthesis "(", push it onto the stack.
d. If it's a closing parenthesis ")", pop operators from the stack and append them to
the output until an opening parenthesis "(" is encountered. Discard the opening
parenthesis.
4. After scanning the entire infix expression, pop any remaining operators from the
stack and append them to the output string.
5. The resulting output string is the postfix expression.
EX:NO:

PRIORITY OF QUEUE ADT


DATE:

AIM:
To Write a program to implement priority queue ADT.

ALGORITHM:
1. Define a Priority Queue class with the following operations:
is_empty()`: Check if the priority queue is empty.
enqueue(item, priority)`: Add an item with its associated priority to the priority
queue.
dequeue()`: Remove and return the item with the highest priority.
peek()`: Return the item with the highest priority without removing it.
size()`: Return the number of elements in the priority queue.
clear()`: Remove all elements from the priority queue.

2. Implement the priority queue using an appropriate data structure that efficiently
supports insertion and removal operations based on priority.

3. Ensure that elements with equal priority are dequeued in the order they were
enqueued.
EX:NO:

BINARY SEARCH TREE


DATE:

AIM:
To Write a program to perform the following operations: Insert an
element into a binary search tree. Delete an element from a binary search tree.
Search for a key element in a binary search tree.

ALGORITHM:

1. Define a Node class to represent each node in the binary search tree. Each node
should have data, a left child reference, and a right child reference.

2. Define a BinarySearchTree class with the following operations:


insert(data)`: Recursively insert a new node with the given data into the binary
search tree.
search(data)`: Recursively search for a node with the given data in the binary
search tree.
delete(data)`: Recursively delete a node with the given data from the binary search
tree.

3. Implement the operations using recursive algorithms:


- Insert: If the tree is empty, create a new node with the given data. Otherwise,
recursively insert into the left subtree if the data is less than the current node's data,
or into the right subtree if it's greater.
- Search: If the tree is empty or the current node's data matches the search data,
return True. Otherwise, recursively search the left subtree if the search data is less
than the current node's data, or the right subtree if it's greater.
- Delete: Find the node to delete, and handle cases where it has zero, one, or two
children recursively.
EX:NO:

AVL TREE
DATE:

4. Optionally, implement traversal methods like preorder, inorder, and postorder


recursively to traverse the tree.

AIM:
To Write a program to perform the following operations Insertion into an
AVL-tree, Deletion from an AVL-tree.

ALGORITHM:

i) Insertion Algorithm for AVL Tree:


1. Perform standard BST insertion.
2. After insertion, update the height of the current node.
3. Traverse back from the current node towards the root, updating heights and
checking balance factor of each node.
4. If the balance factor of any node is violated (-2 or 2), perform rotations to balance
the tree.
ii) Deletion Algorithm for AVL Tree:
1.Perform standard BST deletion.
2.After deletion, update the height of the current node.
3.Traverse back from the current node towards the root, updating heights and
checking balance factor of each node.
4.If the balance factor of any node is violated (-2 or 2), perform rotations to balance
the tree.
EX:NO:

BFS & DFS


DATE:

AIM:
To Write a programs for the implementation of BFS and DFS for a given
graph.

ALGORITHM:
i)BFS

Step 1: SET STATUS = 1 (ready state) for each node in G

Step 2: Enqueue the starting node A and set its STATUS = 2 (waiting state)

Step 3: Repeat Steps 4 and 5 until QUEUE is empty

Step 4: Dequeue a node N. Process it and set its STATUS = 3 (processed state).

Step 5: Enqueue all the neighbours of N that are in the ready state (whose STATUS
= 1) and set

their STATUS = 2

[END OF LOOP]

Step 6: EXIT
ii) DFS

Step 1: SET STATUS = 1 (ready state) for each node in G

Step 2: Push the starting node A on the stack and set its STATUS = 2 (waiting state)

Step 3: Repeat Steps 4 and 5 until STACK is empty

Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed state)

Step 5: Push on the stack all the neighbors of N that are in the ready state (whose
STATUS = 1) and set their STATUS = 2 (waiting state)

[END OF LOOP]

Step 6: EXIT
EX:NO:

LINEAR SEARCH & BINARY SEARCH


DATE:

AIM:
To Write a programs for implementing the following searching methods: Linear
search Binary search.
ALGORITHM:
i) Linear Search
Step 1: Set i to 1.
Step 2: if i > n, then jump to step 7.
Step 3: if A[i] = x then jump to step 6.
Step 4: Set i to i + 1.
Step 5: Go to step 2.
Step 6: Print element x found at index i and jump to step 8.
Step 7: Print element not found.
Step 8: Exit.
ii) Binary Search

Step 1: set beg = lower_bound, end = upper_bound, pos = - 1


Step 2: repeat steps 3 and 4 while beg <=end
Step 3: set mid = (beg + end)/2
Step 4: if a[mid] = val
set pos = mid
print pos
go to step 6
else if a[mid] > val
set end = mid - 1
else
set beg = mid + 1
[end of if]
[end of loop]
Step 5: if pos = -1
print "value is not present in the array"
[end of if]
Step 6: exit.
EX:NO:

BUBBLE SORT, SELECTION SORT, INSERTION SORT &


DATE: RADIX SORT

AIM:

To Write a programs for implementing the following sorting methods: Bubble


sort, Selection sort, Insertion sort, Radix sort.

ALGORITHM:
i) Bubblle sort
1. The largest element is placed in its correct position, i.e., the end of the array.
2. Place the second largest element at correct position.

3. Place the remaining two elements at their correct positions.

4. Total no. of passes: n-1


Total no. of comparisons: n*(n-1)/2.
ii) Selection sort

Step 1: Repeat Steps 2 and 3 for i = 0 to n-1


Step 2: CALL SMALLEST(arr, i, n, pos)
Step 3: SWAP arr[i] with arr[pos]
[END OF LOOP]
Step 4: EXIT

iii) Insertion sort

Step 1 - If the element is the first element, assume that it is already sorted.
Return

Step 2 - Pick the next element, and store it separately in a key.

Step 3 - Now, compare the key with all elements in the sorted array.

Step 4 - If the element in the sorted array is smaller than the current element,
then move to the next element. Else, shift greater elements in the array towards
the right.

Step 5 - Insert the value.


Step 6 - Repeat until the array is sorted.

iv) Radix sort

Step 1: Find the largest element in the array.


Step 2: Sort the elements based on the unit place digits.
Step 3: Sort the elements based on the tens place digits.
Step 4: Sort the elements based on the hundreds place digits.
Step 5: The array is now sorted in ascending order.

You might also like