0% found this document useful (0 votes)
19 views2 pages

dsa

Uploaded by

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

dsa

Uploaded by

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

Q.

5 Merge Sort Algorithm int factorial_recursive(int n) {


Merge Sort is a divide-and-conquer algorithm if (n == 0 || n == 1)
that splits an array into halves, recursively sorts return 1;
each half, and then merges the sorted halves. return n * factorial_recursive(n - 1);
Merge Sort Algorithm: }
1. Divide: Split the array into two halves. int main() {
2. Conquer: Recursively sort the two halves. int num;
3. Combine: Merge the two sorted halves printf("Enter a number: ");
to produce the sorted array. scanf("%d", &num);
Here's the algorithm in pseudocode: printf("Factorial (Recursive): %d\n", factorial_recursive(num));
1. MergeSort(array[], left, right) return 0;
o If left < right: }
1. Calculate mid = left + (right - left) / 2 b) Non-Recursive Function:
2. Call MergeSort(array, left, mid)
3. Call MergeSort(array, mid + 1, right) #include <stdio.h>
4. Call Merge(array, left, mid, right)
2. Merge(array[], left, mid, right) int factorial_non_recursive(int n) {
int result = 1;
o Create temporary arrays L[] and R[] for (int i = 1; i <= n; i++)
o Copy data to temp arrays L[] and R[] result *= i;
o Merge the temp arrays back into array[left..right] return result;
}
o Copy the remaining elements of L[] and R[] if any
int main() {
Applying Merge Sort on the Given Elements int num;
Let's apply Merge Sort on the array [45, 32, 65, 76, 23, 12, 54, 67, 22, 87, 5]. printf("Enter a number: ");
1. Initial Array: [45, 32, 65, 76, 23, 12, 54, 67, 22, 87, 5] scanf("%d", &num);
2. Divide Step: printf("Factorial (Non-Recursive): %d\n", factorial_non_recursive(num));
o Split into [45, 32, 65, 76, 23] and [12, 54, 67, 22, 87, 5] return 0;
o Further split each half until each sub-array has one element: }
 [45, 32, 65] -> [45, 32] -> [45] and [32]
 [76, 23] -> [76] and [23] #include <stdio.h>
 [12, 54, 67] -> [12, 54] -> [12] and [54] int binarySearch(int arr[], int low, int high, int key) {
if (low > high)

[22, 87, 5] -> [22, 87] -> [22] and [87]
return -1; // Element not found
3. Conquer Step: int mid = low + (high - low) / 2;
o Sort and merge sub-arrays: if (arr[mid] == key)
 [45] and [32] -> [32, 45] return mid;
 [32, 45] and [65] -> [32, 45, 65] else if (arr[mid] > key)
return binarySearch(arr, low, mid - 1, key);
 [76] and [23] -> [23, 76] else
 [32, 45, 65] and [23, 76] -> [23, 32, 45, 65, 76] return binarySearch(arr, mid + 1, high, key);
 [12] and [54] -> [12, 54] }
 [12, 54] and [67] -> [12, 54, 67] int main() {
int arr[] = {2, 4, 6, 8, 10, 12, 14};
 [22] and [87] -> [22, 87]
int n = sizeof(arr) / sizeof(arr[0]);
 [22, 87] and [5] -> [5, 22, 87] int key = 10;

[12, 54, 67] and [5, 22, 87] -> [5, 12, 22, 54, 67, 87] int result = binarySearch(arr, 0, n - 1, key);
4. Combine Step: if (result != -1)
o Merge [23, 32, 45, 65, 76] and [5, 12, 22, 54, 67, 87] printf("Element found at index %d\n", result);
else
o Resulting in the sorted array: [5, 12, 22, 23, 32, 45, 54, 65, 67, printf("Element not found.\n");
76, 87] return 0;
}
Algorithm InsertNodeDLL(head, position, value): 1.An array is a grouping of data elements of equivalent data type
1. Create a new node with data = value. .A linked list is a group of entities called a node.
2. If position = 1: The node includes two segments: data and address.
a. Set newNode->next = head 2.It stores the data elements in a contiguous memory zone.
b. If head is not NULL, set head->prev = newNode It stores elements randomly, or we can say anywhere in the memory zone.
c. Set head = newNode 3.In the case of an array, memory size is fixed, and it is not possible to change it during the
3. Else: run time.
a. Traverse the list to the (position-1)th node. In the linked list, the placement of elements is allocated during the run time.
b. Set newNode->next = current->next 4.The elements are not dependent on each other.
c. If current->next is not NULL, set current->next->prev = newNode The data elements are dependent on each other.
d. Set current->next = newNode 5.The memory is assigned at compile time.
e. Set newNode->prev = current The memory is assigned at run time.
4. End. 6.It is easier and faster to access the element in an array.
In a linked list, the process of accessing elements takes more time.
7.In the case of an array, memory utilization is ineffective.
Breadth-First Search (BFS) In the case of the linked list, memory utilization is effective.
8When it comes to executing any operation like insertion, deletion, array takes more time.
Overview: BFS starts at a root node and explores all its neighbors before moving
to the next depth level, utilizing a queue. When it comes to executing any operation like insertion, deletion, the linked list takes less
time.
Steps: Initialize a queue with the starting node, then dequeue, process, and
enqueue unvisited neighbors until the queue is empty. Complexity: Time: (O(V + E)), Space: (O(V)).

Complexity: Time: (O(V + E)), Space: (O(V)). Use Cases: Topological sorting, connected components, pathfinding.

Use Cases: Shortest path in unweighted graphs, level-order traversal in trees, Advantages: Generally lower memory usage, recursive implementation can
network broadcasting. simplify code.

Advantages: Guarantees shortest path, easy to implement. Disadvantages: Does not guarantee the shortest path, risk of stack overflow in
deep recursions.
Disadvantages: Higher memory requirements for dense graphs.
Key Differences
Depth-First Search (DFS)
Traversal Method: BFS is level-based; DFS is depth-based.
Overview: DFS explores as far down each branch as possible before
backtracking, using a stack or recursion. Data Structures: BFS uses a queue; DFS uses a stack.

Steps: Start at a node, visit unvisited neighbors recursively, and backtrack if no Shortest Path: BFS guarantees the shortest path; DFS does not.
neighbors remain. Memory Use: BFS can be more memory-intensive.

You might also like