1.
merge sort algorithm
Algorithm MERGE_SORT(A, low, high)
1. If low < high
2. mid = (low + high) / 2
3. MERGE_SORT(A, low, mid)
4. MERGE_SORT(A, mid + 1, high)
5. MERGE(A, low, mid, high)
Algorithm MERGE(A, low, mid, high)
1. Create two temporary arrays:
Left[low…mid]
Right[mid+1…high]
2. Copy elements into Left and Right
3. i = 0, j = 0, k = low
4. While i < size of Left AND j < size of Right
If Left[i] <= Right[j]
A[k] = Left[i]
i=i+1
Else
A[k] = Right[j]
j=j+1
k=k+1
5. Copy remaining elements of Left (if any)
6. Copy remaining elements of Right (if any)
2. Bubble Sort algorithm.
Algorithm BUBBLE_SORT(A, n)
1. For i = 0 to n-2
2. For j = 0 to n-2-i
3. If A[j] > A[j+1]
4. Swap A[j] and A[j+1]
5. End For
6. End For
7. Return A
3. quick sort algorithm
Algorithm QUICK_SORT(A, low, high)
1. If low < high
2. loc = PARTITION(A, low, high)
3. QUICK_SORT(A, low, loc - 1)
4. QUICK_SORT(A, loc + 1, high)
Algorithm PARTITION(A, low, high)
1. pivot = A[low]
2. left = low + 1
3. right = high
4. While left <= right
5. While left <= high AND A[left] <= pivot
6. left = left + 1
7. While A[right] > pivot
8. right = right - 1
9. If left < right
10. Swap A[left] and A[right]
11. Swap A[low] and A[right]
12. Return right
4. insertion sort algorithm.
1. Start
2. For i = 1 to n-1
3. key = A[i]
4. j=i-1
5. While j >= 0 AND A[j] > key
6. A[j + 1] = A[j]
7. j=j-1
8. End While
9. A[j + 1] = key
10. End For
11. Stop
5. Write algorithm of PUSH and POP operation on stack
Algorithm PUSH(STACK, TOP, MAX, ITEM)
1. If TOP == MAX - 1
2. Print "Stack Overflow"
3. Return
4. Else
5. TOP = TOP + 1
6. STACK[TOP] = ITEM
7. Return
Algorithm POP(STACK, TOP)
1. If TOP == -1
2. Print "Stack Underflow"
3. Return
4. Else
5. ITEM = STACK[TOP]
6. TOP = TOP - 1
7. Return ITEM
4. ans - 1. For i = 1 to n-1
2. key = A[i]
3. j=i-1
4. While j >= 0 AND A[j] > key
5. A[j + 1] = A[j]
6. j=j-1
7. A[j + 1] = key
6. Write the algorithm for searching an element in a given list using linear search
Algorithm LINEAR_SEARCH(A, n, key)
1. For i = 0 to n-1
2. If A[i] == key
3. Print "Element found at position", i
4. Return i
5. End For
6. Print "Element not found"
7. Return -1
7. Write an algorithm for traversing a singly linked list and printing all its elements.
Algorithm TRAVERSE_SLL(head)
1. If head == NULL
Print "List is empty"
Return
2. temp = head
3. While temp != NULL
Print temp->data
temp = temp->next
4. End
8. Describe the algorithm for inserting a new node at the beginning of a singly linked list
Algorithm INSERT_BEGIN(head, item)
1. Create a new node N
2. If N == NULL
Print "Overflow"
Return
3. N->data = item
4. N->next = head
5. head = N
6. Return head
9. Write an algorithm to delete a node from end of the singly linked list.
Algorithm DELETE_END(head)
1. If head == NULL
Print "Underflow"
Return head
2. If head->next == NULL
Free head
head = NULL
Return head
3. temp = head
4. While temp->next->next != NULL
temp = temp->next
5. last = temp->next
6. temp->next = NULL
7. Free last
8. Return head
10. Describe the algorithm to insert a new node at the beginning of a doubly linked list
Algorithm INSERT_BEGIN_DLL(head, item)
1. Create a new node N
2. If N == NULL
Print "Overflow"
Return head
3. N->data = item
4. N->prev = NULL
5. N->next = head
6. If head != NULL
head->prev = N
7. head = N
8. Return head
11. Write the algorithm for deleting a node from end of a doubly linked list.
Algorithm DELETE_END_DLL(head)
1. If head == NULL
Print "Underflow"
Return head
2. If head->next == NULL
Free head
head = NULL
Return head
3. temp = head
4. While temp->next != NULL
temp = temp->next
5. temp->prev->next = NULL
6. Free temp
7. Return head
12. Describe the algorithm for inserting an element at the beginning of a circular doubly linked list
Algorithm INSERT_BEGIN_CDLL(head, item)
1. Create a new node N
2. If N == NULL
Print "Overflow"
Return head
3. N->data = item
4. If head == NULL
N->next = N
N->prev = N
head = N
Return head
5. last = head->prev
6. N->next = head
7. N->prev = last
8. last->next = N
9. head->prev = N
10. head = N
11. Return head
13. Write the algorithm for enqueuing and dequeuing in a linked-list-based queue
Algorithm ENQUEUE(front, rear, item)
1. Create a new node N
2. If N == NULL
Print "Overflow"
Return
3. N->data = item
4. N->next = NULL
5. If front == NULL
front = rear = N
Else
rear->next = N
rear = N
6. Return front, rear
Algorithm DEQUEUE(front, rear)
1. If front == NULL
Print "Underflow"
Return
2. temp = front
3. front = front->next
4. If front == NULL
rear = NULL
5. Free temp
6. Return front, rear
14. Express an Algorithm to add two polynomials having m and n terms respectively using linked list
with an example.
Algorithm ADD_POLY(P, Q)
1. Create an empty list R
2. Set p = P, q = Q
3. While p != NULL and q != NULL
If p->exp > q->exp
Insert (p->coeff, p->exp) into R
p = p->next
Else if p->exp < q->exp
Insert (q->coeff, q->exp) into R
q = q->next
Else
sum = p->coeff + q->coeff
If sum != 0
Insert (sum, p->exp) into R
p = p->next
q = q->next
4. While p != NULL
Insert remaining terms of p into R
p = p->next
5. While q != NULL
Insert remaining terms of q into R
q = q->next
6. Return R
15. Dijkstra’s Algorithm
1. For each vertex v in G
2. distance[v] = ∞
3. visited[v] = false
4. distance[source] = 0
5. Repeat n times:
6. Select unvisited vertex u with minimum distance
7. Mark u as visited
8. For each neighbor v of u
9. If distance[u] + weight(u, v) < distance[v]
10. distance[v] = distance[u] + weight(u, v)
16. Write the algorithm for deleting a node from end of a doubly linked list.
1. If head == NULL
2. Print "List is empty (Underflow)"
3. Return
4. If head->next == NULL // Only one node
5. Free head
6. head = NULL
7. Return
8. temp = head
9. While temp->next != NULL // Traverse to last node
10. temp = temp->next
11. temp->prev->next = NULL // Remove last node link
12. Free temp
13. Return head
[Link] a program which implements routines for the following operations:- i) Create an array at
runtime ii) Find the maximum element in an array iii) Delete duplicate elements from an array.
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, j, k, max;
// (i) Create array at runtime
printf("Enter number of elements: ");
scanf("%d", &n);
int *arr = (int*)malloc(n * sizeof(int));
if(arr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
printf("Enter elements:\n");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// (ii) Find maximum element
max = arr[0];
for(i = 1; i < n; i++) {
if(arr[i] > max) {
max = arr[i];
}
}
printf("Maximum element = %d\n", max);
// (iii) Delete duplicate elements
for(i = 0; i < n; i++) {
for(j = i + 1; j < n; j++) {
if(arr[i] == arr[j]) {
for(k = j; k < n - 1; k++) {
arr[k] = arr[k + 1];
}
n--;
j--;
}
}
}
printf("Array after deleting duplicates:\n");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
free(arr);
return 0;
}