















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
















An algorithm can be defined as a set of procedure in solving a problem. Importance
of dividing the search interval in half, it estimates the position of the target element based on its value and the values of the first and last elements in the search interval. Algorithm: ● Calculate the position of the target element based on its value and the values of the first and last elements in the search interval. ● If the target element matches the element at the estimated position, return its index. ● If the target element is smaller than the element at the estimated position, repeat the search on the lower half of the list. ● If the target element is larger than the element at the estimated position, repeat the search on the upper half of the list. ● If the target element is not found, return -1.
● Start from the first element of the list. ● Compare the target element with the current element. ● If the target element is found, return its index. ● If the end of the list is reached and the element is not found, return -1. Time Complexity: The worst-case time complexity of the linear search algorithm is O(n), where n is the number of elements in the list. This is because in the worst-case scenario, the target element will not be found until the last element is checked. The best-case time complexity is O(1), which occurs when the target element is found at the first position. Merits: ● The linear search algorithm is simple to implement and understand. ● It works with any type of list or array, regardless of how it is sorted or ordered. ● It is useful for small lists or arrays, as the overhead of sorting or other optimization techniques may not be worth it. Demerits: ● The worst-case time complexity of the linear search algorithm is O(n), which can be slow for large lists or arrays. ● It is not efficient for searching large datasets or databases. ● It can take a long time to search through unsorted lists or arrays. II. The binary search algorithm is a search algorithm used to find an element in a sorted list or array by repeatedly dividing the search interval in half. It is a very efficient algorithm for searching through large datasets. Algorithm: ● Begin with the middle element of the sorted list. ● If the target element matches the middle element, return its index. ● If the target element is smaller than the middle element, repeat the search on the lower half of the list. ● If the target element is larger than the middle element, repeat the search on the upper half of the list. ● If the target element is not found, return -1. Time Complexity: The time complexity of the binary search algorithm is O(log n), where n is the number of elements in the list. This is because in each iteration, the search interval is divided in half, resulting in a logarithmic reduction in the search space.
● The interpolated search algorithm may perform worse than the binary search algorithm for datasets with unevenly distributed values. ● It requires that the list be sorted. ● It is not suitable for dynamic datasets, where elements are frequently added or removed from the list IV. The exponential search algorithm is a search algorithm used to find an element in a sorted list or array by first finding an appropriate range of values using exponential growth, and then performing a binary search on that range. It is an efficient algorithm for searching through large datasets. Algorithm: ● Determine an appropriate range of values by doubling an index starting from 1 until the value at that index is greater than or equal to the target value. ● Perform a binary search on the range determined in step 1 to find the target element. ● If the target element is found, return its index. Otherwise, return -1. Time Complexity: The time complexity of the exponential search algorithm is O(log n), where n is the number of elements in the list. This is because the algorithm first determines an appropriate range of values using exponential growth, which takes O(log n) time. The binary search on that range takes O(log m) time, where m is the size of the range. Merits: ● The exponential search algorithm is efficient for searching through large datasets, where the time complexity can be reduced to O(log n). ● It requires only one binary search, which is faster than multiple binary searches. ● It works best for sorted lists or arrays. Demerits: ● The exponential search algorithm requires the list to be sorted. ● It is not suitable for dynamic datasets, where elements are frequently added or removed from the list. ● It may be slower than other algorithms, such as the binary search algorithm, for datasets with a small number of elements. QUESTION 6 A sorting algorithm is a method for arranging the elements of a list or array in a particular order, such as numerical or alphabetical. Sorting algorithms are commonly used in computer science and many applications, including data analysis, database management, and web searching. Common sorting algorithms and their applications:
O(n). This is because the algorithm only needs to make n-1 comparisons in the best case, as each element is already in its proper place. Merits : Insertion sort is a simple algorithm that is easy to understand and implement. It is also efficient for small datasets and has a better performance than bubble sort. It also sorts in place, meaning it does not require additional memory beyond the original list or array. Demerits : Insertion sort is not very efficient for large datasets, as its time complexity is O(n^2), which can make it slow and inefficient. It also has poor performance when the list or array is almost sorted, as it still needs to make the same number of comparisons even if the list is almost sorted. Overall, insertion sort is useful for small datasets or where simplicity is important, but it is not recommended for large datasets or real-time applications.
Time Complexity : The time complexity of quick sort is O(n log n) in the average case, where n is the number of elements in the list or array. However, in the worst case scenario where the pivot is consistently chosen poorly, the time complexity can be O(n^2). In practice, the worst case scenario is rare and quick sort is generally considered one of the fastest sorting algorithms. Merits : Quick sort is highly efficient and well-suited for large datasets. It also has a smaller memory footprint than merge sort, as it sorts the list or array in place without the need for temporary arrays. It is also a highly adaptable algorithm, as it can be optimized for different data distributions and can be easily parallelized. Demerits : Quick sort can be unstable, meaning that the order of equal elements in the list may not be preserved. It can also be sensitive to the choice of pivot element, and poorly chosen pivot elements can lead to worst case scenarios and poor performance. Additionally, quick sort is a relatively complex algorithm to understand and implement. Overall, quick sort is a highly efficient sorting algorithm that is well-suited for large datasets and can be optimized for various scenarios. However, it may not be the best choice in situations where stability is important or where the choice of pivot element is uncertain. QUESTION 8 An array is a data structure that stores a fixed-size sequence of elements of the same data type. Each element in an array is accessed using an index or a subscript, which represents its position in the sequence. An ordered array is an array where the elements are stored in a particular order, such as ascending or descending order based on their values. Examples of ordered arrays include: ● A list of temperatures recorded over a period of time, sorted in chronological order ● A list of student names sorted alphabetically ● A list of stock prices sorted by increasing value An unordered array is an array where the elements are not stored in any particular order. Examples of unordered arrays include: ● A list of customer names and addresses ● A list of emails received in a mailbox ● A list of items in a grocery cart Applications: The choice between using an ordered or unordered array depends on the particular application requirements. Differences Ordered arrays are useful when the data needs to be accessed in a particular order, such as for searching or sorting purposes. For example, if we want to find the highest or lowest temperature recorded in a period of time, an ordered array would make it easier to perform the search. Unordered arrays are useful when the data does not need to be accessed in any particular order, such as for storing a collection of items. For example, a grocery cart can contain a collection of items with no particular ordering requirement.
A circular linked list is a special type of linked list in which the last node is linked to the first node, forming a circular sequence. This means that the next pointer of the last node points to the first node, instead of null. Circular linked lists can be either singly linked or doubly linked. The main difference between a doubly linked list and a circular linked list is the way they are linked together. In a doubly linked list, the last node points to null, while in a circular linked list, the last node points to the first node, creating a loop. This means that traversing a circular linked list can be done indefinitely in either direction, while traversing a doubly linked list can only be done until the last node is reached. QUESTION 11 A binary tree is a tree-like data structure in which each node has at most two children, referred to as the left child and the right child. Each node in a binary tree can have either zero, one, or two children. Properties of a binary tree: i. Root: The topmost node of a binary tree is called the root node. It has no parent. ii. Parent and child: Each node in a binary tree can have at most two children, referred to as the left child and the right child. Each child node has a parent node, except for the root node which has no parent. iii. Leaf: A node with no children is called a leaf node or a terminal node. iv. Internal node: A node with at least one child is called an internal node. v. Depth: The depth of a node in a binary tree is the number of edges from the root node to that node. vi. Height: The height of a binary tree is the maximum depth of any node in the tree. vii. Binary search tree: A binary tree can be a binary search tree if it satisfies the following property: for every node, all the nodes in its left subtree have values less than its own value, and all the nodes in its right subtree have values greater than its own value. viii. Balanced tree: A binary tree is balanced if the height of its left and right subtrees differ by at most one. ix. Complete binary tree: A binary tree is complete if all levels of the tree are completely filled, except possibly the last level, which is filled from left to right. x. Full binary tree: A binary tree is full if every node has either zero or two children. xi. QUESTION 12 A binary search tree is a binary tree that satisfies the binary search tree property, which means that for each node in the tree, all the nodes in its left subtree have values less than its own value, and all the nodes in its right subtree have values greater than its own value. Properties of a binary search tree:
Breadth-first search (BFS) and depth-first search (DFS) are two common algorithms used to traverse or search a graph or a tree. Breadth-first search (BFS): BFS is an algorithm used to traverse or search a graph or a tree data structure. The algorithm starts at the root node and explores all the nodes at the current depth level before moving on to the next depth level. This process is repeated until all nodes have been explored or the desired node has been found. The algorithm uses a queue data structure to keep track of the nodes to be visited. Algorithm for BFS:
the source code in a way that minimizes the total length of the encoded message. The tree is constructed by starting with the two least frequent symbols and repeatedly combining the two nodes with the lowest frequency until a single root node is created.
g) 25, 24, 16, 18, 21, 25, 15, 12, 33, 36, 19, 22, 31, 11, 10 h) 25, 24, 16, 18, 21, 25, 15, 12, 33, 36, 19, 22, 31, 11, 10 i) 25, 24, 16, 18, 21, 25, 15, 33, 12, 36, 19, 22, 31, 11, 10 j) 25, 24, 16, 18, 21, 25, 15, 33, 36, 12, 19, 22, 31, 11, 10 k) 25, 24, 16, 18, 21, 25, 15, 33, 36, 19, 12, 22, 31, 11, 10 l) 25, 24, 16, 18, 21, 25, 15, 33, 36, 19, 22, 12, 31, 11, 10 m) 25, 24, 16, 18, 21, 25, 15, 33, 36, 19, 22, 31, 12, 11, 10 n) 25, 24, 16, 18, 21, 25, 15, 33, 36, 19, 22, 31, 12, 11, 10 o) 25, 24, 16, 18, 21, 25, 15, 33, 36, 19, 22, 31, 12, 11, 10 Phase 4 a) 25, 24, 16, 18, 21, 25, 15, 33, 36, 19, 22, 31, 12, 11, 10 b) 25, 24, 16, 18, 21, 25, 15, 33, 36, 19, 22, 31, 12, 11, 10 c) 25, 24, 16, 18, 21, 25, 15, 33, 36, 19, 22, 31, 12, 11, 10 d) 25, 24, 18, 16, 21, 25, 15, 33, 36, 19, 22, 31, 12, 11, 10 e) 25, 24, 18, 21, 16, 25, 15, 33, 36, 19, 22, 31, 12, 11, 10 f) 25, 24, 18, 21, 25, 16, 15, 33, 36, 19, 22, 31, 12, 11, 10 g) 25, 24, 18, 21, 25, 16, 15, 33, 36, 19, 22, 31, 12, 11, 10 h) 25, 24, 18, 21, 25, 16, 33, 15, 36, 19, 22, 31, 12, 11, 10 i) 25, 24, 18, 21, 25, 16, 33, 36, 15, 19, 22, 31, 12, 11, 10 j) 25, 24, 18, 21, 25, 16, 33, 36, 19, 15, 22, 31, 12, 11, 10 k) 25, 24, 18, 21, 25, 16, 33, 36, 19, 22, 15, 31, 12, 11, 10 l) 25, 24, 18, 21, 25, 16, 33, 36, 19, 22, 31, 15, 12, 11, 10 m) 25, 24, 18, 21, 25, 16, 33, 36, 19, 22, 31, 15, 12, 11, 10 n) 25, 24, 18, 21, 25, 16, 33, 36, 19, 22, 31, 15, 12, 11, 10 o) 25, 24, 18, 21, 25, 16, 33, 36, 19, 22, 31, 15, 12, 11, 10 Phase 5 a) 25, 24, 18, 21, 25, 16, 33, 36, 19, 22, 31, 15, 12, 11, 10 b) 25, 24, 18, 21, 25, 16, 33, 36, 19, 22, 31, 15, 12, 11, 10 c) 25, 24, 21, 18, 25, 16, 33, 36, 19, 22, 31, 15, 12, 11, 10 d) 25, 24, 21, 25, 18, 16, 33, 36, 19, 22, 31, 15, 12, 11, 10 e) 25, 24, 21, 25, 18, 16, 33, 36, 19, 22, 31, 15, 12, 11, 10 f) 25, 24, 21, 25, 18, 33, 16, 36, 19, 22, 31, 15, 12, 11, 10 g) 25, 24, 21, 25, 18, 33, 36, 16, 19, 22, 31, 15, 12, 11, 10 h) 25, 24, 21, 25, 18, 33, 36, 19, 16, 22, 31, 15, 12, 11, 10 i) 25, 24, 21, 25, 18, 33, 36, 19, 22, 16, 31, 15, 12, 11, 10 j) 25, 24, 21, 25, 18, 33, 36, 19, 22, 31, 16, 15, 12, 11, 10 k) 25, 24, 21, 25, 18, 33, 36, 19, 22, 31, 16, 15, 12, 11, 10 l) 25, 24, 21, 25, 18, 33, 36, 19, 22, 31, 16, 15, 12, 11, 10 m) 25, 24, 21, 25, 18, 33, 36, 19, 22, 31, 16, 15, 12, 11, 10 n) 25, 24, 21, 25, 18, 33, 36, 19, 22, 31, 16, 15, 12, 11, 10 o) 25, 24, 21, 25, 18, 33, 36, 19, 22, 31, 16, 15, 12, 11, 10 Phase 6 a) 25, 24, 21, 25, 18, 33, 36, 19, 22, 31, 16, 15, 12, 11, 10 b) 25, 24, 21, 25, 18, 33, 36, 19, 22, 31, 16, 15, 12, 11, 10 c) 25, 24, 21, 25, 18, 33, 36, 19, 22, 31, 16, 15, 12, 11, 10 d) 25, 24, 25, 21, 18, 33, 36, 19, 22, 31, 16, 15, 12, 11, 10 e) 25, 24, 25, 21, 18, 33, 36, 19, 22, 31, 16, 15, 12, 11, 10
h) 25, 33, 36, 25, 24, 31, 22, 21, 19, 18, 16, 15, 12, 11, 10 i) 25, 33, 36, 25, 24, 31, 22, 21, 19, 18, 16, 15, 12, 11, 10 j) 25, 33, 36, 25, 24, 31, 22, 21, 19, 18, 16, 15, 12, 11, 10 k) 25, 33, 36, 25, 24, 31, 22, 21, 19, 18, 16, 15, 12, 11, 10 l) 25, 33, 36, 25, 24, 31, 22, 21, 19, 18, 16, 15, 12, 11, 10 m) 25, 33, 36, 25, 24, 31, 22, 21, 19, 18, 16, 15, 12, 11, 10 n) 25, 33, 36, 25, 24, 31, 22, 21, 19, 18, 16, 15, 12, 11, 10 o) 25, 33, 36, 25, 24, 31, 22, 21, 19, 18, 16, 15, 12, 11, 10 Phase 10 a) 25, 33, 36, 25, 24, 31, 22, 21, 19, 18, 16, 15, 12, 11, 10 b) 33, 25, 36, 25, 24, 31, 22, 21, 19, 18, 16, 15, 12, 11, 10 c) 33, 36, 25, 25, 24, 31, 22, 21, 19, 18, 16, 15, 12, 11, 10 d) 33, 36, 25, 25, 24, 31, 22, 21, 19, 18, 16, 15, 12, 11, 10 e) 33, 36, 25, 25, 24, 31, 22, 21, 19, 18, 16, 15, 12, 11, 10 f) 33, 36, 25, 25, 24, 31, 22, 21, 19, 18, 16, 15, 12, 11, 10 g) 33, 36, 25, 25, 31, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 h) 33, 36, 25, 25, 31, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 i) 33, 36, 25, 25, 31, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 j) 33, 36, 25, 25, 31, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 k) 33, 36, 25, 25, 31, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 l) 33, 36, 25, 25, 31, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 m) 33, 36, 25, 25, 31, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 n) 33, 36, 25, 25, 31, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 o) 33, 36, 25, 25, 31, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 Phase 11 a) 33, 36, 25, 25, 31, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 b) 36, 33, 25, 25, 31, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 c) 36, 33, 25, 25, 31, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 d) 36, 33, 25, 31, 25, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 e) 36, 33, 25, 31, 25, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 f) 36, 33, 25, 31, 25, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 g) 36, 33, 25, 31, 25, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 h) 36, 33, 25, 31, 25, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 i) 36, 33, 25, 31, 25, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 j) 36, 33, 25, 31, 25, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 k) 36, 33, 25, 31, 25, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 l) 36, 33, 25, 31, 25, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 m) 36, 33, 25, 31, 25, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 n) 36, 33, 25, 31, 25, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 o) 36, 33, 25, 31, 25, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 Phase 12 a) 36, 33, 25, 31, 25, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 b) 36, 33, 25, 31, 25, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 c) 36, 33, 25, 31, 25, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 d) 36, 33, 31, 25, 25, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 e) 36, 33, 31, 25, 25, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 f) 36, 33, 31, 25, 25, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 g) 36, 33, 31, 25, 25, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10
h) 36, 33, 31, 25, 25, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 i) 36, 33, 31, 25, 25, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 j) 36, 33, 31, 25, 25, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 k) 36, 33, 31, 25, 25, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 l) 36, 33, 31, 25, 25, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 m) 36, 33, 31, 25, 25, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 n) 36, 33, 31, 25, 25, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 o) 36, 33, 31, 25, 25, 24, 22, 21, 19, 18, 16, 15, 12, 11, 10 Therefore, the sorted list in descending order using bubble sort is : 36,33,31,25,25,24,22,21,19,18,16,15,12,11,10. Binary Tree Max-Heap QUESTION 19 (i) Depth First Search 45, 41, 37, 26, 25, 20, 35, 38, 32, 29 (ii) Breadth First Search 45, 41, 38, 37, 35, 32, 29, 26, 25, 20 QUESTION 20 A heap is a specialized tree-based data structure that satisfies the heap property. The heap property can either be the max- heap property or the min-heap property, which specifies the relationship between parent and child nodes. In a max-heap, the value of each parent node is greater than or equal to the values of its children nodes, and in a min-heap, the value of each parent node is less than or equal to the values of its children nodes. 10 (^1011) 11 (^15 12 15 ) (^21 19 181621 ) (^25 25 242225 ) 33 31 33 31 (^3636)