Lab Report 2 2221081073
Lab Report 2 2221081073
Lab Report 2 2221081073
Lab Report - 02
Merge Sort is a highly efficient comparison-based sorting algorithm known for its stability and
time complexity of O(n log n). It operates by dividing the input array into two halves, recursively
sorting these halves, and then merging them into a single sorted array. The algorithm's divide-
and-conquer approach ensures reliable sorting performance, even for large datasets.
Algorithm Steps:
1. Base Case:
-If `n` (the length of the array) is 1 or less, return `arr` as it is already sorted.
2. Divide:
-Calculate the middle index of `arr`: `mid = n // 2`.
5. Merge Subarrays:
- Create an empty result array.
- Initialize two pointers, `i` and `j`, for the left and right subarrays, both initially set to 0.
6. Compare Elements:
- Compare `left_half[i]` and `right_half[j]`.
7. Append Smaller Element:
- Append the smaller of the two elements to the result array.
8. Increment Pointers:
- Increment the pointer (`i` or `j`) corresponding to the element that was appended.
Jump Search is an efficient searching algorithm used to find an element in a sorted array. It
combines the strengths of linear search and binary search to reduce the number of comparisons
required, particularly for large datasets. Jump Search is suitable for scenarios where data is
sorted and random access to elements is possible.
Algorithm Steps:
1. Define Block Size: Determine the optimal block size `step` for jumping through the array. A
common choice is the square root of `n`.
2. Initialize Variables:
- Initialize a variable `prev` to 0 (representing the starting index of the current block).
- Initialize a variable `curr` to `step` (representing the ending index of the current block).
5. Return Result:
- If the `target` element is found, return its index.
- If the `target` element is not found within the array, return `-1`.
Python Code:
Quick Sort
Quick Sort is a highly efficient and widely used comparison-based sorting algorithm. Known for
its average-case time complexity of O(n log n), it is often the preferred choice for sorting large
datasets. Quick Sort follows the divide-and-conquer strategy to sort an array by partitioning it
into smaller subarrays, sorting those subarrays, and combining them to achieve a fully sorted
array.
Algorithm Steps:
1. Choose a Pivot: Select a pivot element from the array. Common choices include the first
element, last element, or a randomly chosen element.
2. Partition the Array: Rearrange the elements in the array such that all elements less than the
pivot are on the left side, and all elements greater than the pivot are on the right side. The pivot
element is now in its final sorted position.
4. Combine Results:
- As the subarrays are sorted in place, no additional combining step is necessary.
Algorithm Steps:
1. Initialize Sorting:
- Start with the second element in the array (index 1). The first element (index 0) is considered
the initial sorted portion.
In data analysis and statistics, it is often necessary to identify the extreme values within a dataset,
namely the maximum (or max) and minimum (or min) values. The maximum value represents
the largest element in the dataset, while the minimum value represents the smallest element. This
lab report outlines methods and algorithms to efficiently find both the maximum and minimum
values in a dataset.
Maximum Value:
The maximum value in a dataset is the greatest element among all the elements in the dataset. To
find the maximum value, we follow these steps:
Algorithm Steps:
1. Initialize Variables:
- Set the initial maximum value as the first element in the dataset.
The minimum value in a dataset is the smallest element among all the elements in the dataset. To
find the minimum value, we follow these steps:
Algorithm Steps:
1. Initialize Variables:
- Set the initial minimum value as the first element in the dataset.
The GCD and LCM algorithms play essential roles in number theory and various computational
tasks. The GCD algorithm finds the largest positive integer that divides two or more numbers
without leaving a remainder. The LCM algorithm calculates the smallest multiple that is divisible
by two or more numbers. Both algorithms have applications in various fields, including
cryptography, number theory, and computer science.
1. Initialization: Start with two positive integers, `a` and `b`, for which you want to find the
GCD.
2. Calculate Remainder: Use the Euclidean algorithm to calculate the remainder of dividing the
larger number by the smaller number.
3. Repeat: Continue to calculate the remainder of dividing the smaller number by the remainder
from the previous step until the remainder is 0.
1. Initialization: Start with two positive integers, `a` and `b`, for which you want to find the
LCM.
2. Calculate LCM: Use the formula: LCM(a, b) = (a * b) / GCD(a, b), where `GCD(a, b)` is the
greatest common divisor calculated using the GCD algorithm.