Lab Report 2 2221081073

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Department of Computer Science and Engineering

Lab Report - 02

Course Name : DESIGN & ANALYSIS OF


ALGORITHM
Course Code : CSEC 312
Date of Submission : 6 October 2023

Submitted By: Submitted To:


Name: Badhon Sorkar Md. Harun -Ar- Rashid
ID: 2221081073 Lecturer
Batch: 56 B Dept. Of CSE, UU
Merge Sort

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`.

3. Recursively Sort Left Half:


- Recursively call the Merge Sort algorithm on the left subarray: `left_half = arr[:mid]`.

4. Recursively Sort Right Half:


- Recursively call the Merge Sort algorithm on the right subarray: `right_half = arr[mid:]`.

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.

9. Repeat Steps 6-8:


- Repeat steps 6-8 until either the left subarray or the right subarray is exhausted (i.e., the
pointer reaches the end of the subarray).

10. Append Remaining Elements:


- After step 9, if there are remaining elements in either the left subarray or the right subarray,
append them to the result array.

11. Return Sorted Array:


- Return the merged result array as the sorted array.
Python Code:
Jump Search

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).

3. Jump through Blocks:


- Repeat the following steps while `arr[curr] < target` and `curr < n`:
- Update `prev` to `curr`.
- Increment `curr` by the block size `step`.

4. Linear Search within Block:


- Perform a linear search between indices `prev` and `curr` to find the `target` element.
- Start from `prev` and increment the index until the `target` element is found or exceeded.

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.

3. Recursively Sort Subarrays:


- Recursively apply the Quick Sort algorithm to the left subarray (elements less than the pivot).
- Recursively apply the Quick Sort algorithm to the right subarray (elements greater than the
pivot).

4. Combine Results:
- As the subarrays are sorted in place, no additional combining step is necessary.

5. Return Sorted Array:


- The fully sorted array is achieved when the recursion reaches subarrays of size 1 or 0. The
algorithm returns the sorted array.
Python Code:
Insertion Sort

Insertion Sort is a simple and efficient comparison-based sorting algorithm. It is particularly


suitable for small datasets or partially sorted datasets. This algorithm works by building a sorted
portion of the array one element at a time. At each step, it takes an element from the unsorted
portion and inserts it into its correct position within the sorted portion.

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.

2. Iterate through the Unsorted Portion:


- Begin iterating from the second element (index 1) to the last element (index `n-1`) in the
unsorted portion of the array.

3. Insert into Sorted Portion:


- For each element in the unsorted portion, compare it with the elements in the sorted portion.
- Move elements in the sorted portion that are greater than the current element to the right to
create space for insertion.
- Insert the current element into its correct sorted position within the sorted portion.

4. Repeat Steps 2-3:


- Continue iterating through the unsorted portion, inserting elements into the sorted portion one
by one, until the entire array is sorted.

5. Return Sorted Array:


- The algorithm is complete when the entire array is sorted, and the sorted array is returned.
Python Code:
Maximum and Minimum

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.

2. Iterate Through the Dataset:


- Starting from the second element, iterate through the dataset.
- Compare each element with the current maximum value.
- If the element is greater than the current maximum value, update the maximum value to the
element.

3. Return Maximum Value:


- Once the entire dataset has been traversed, the maximum value is found and can be returned
as the result.
Minimum Value:

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.

2. Iterate Through the Dataset:


- Starting from the second element, iterate through the dataset.
- Compare each element with the current minimum value.
- If the element is smaller than the current minimum value, update the minimum value to the
element.

3. Return Minimum Value:


- Once the entire dataset has been traversed, the minimum value is found and can be returned
as the result.
Python Code:
GCD and LCM

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.

GCD Algorithm Steps:

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.

4. Return GCD: The GCD is the last non-zero remainder obtained.

LCM Algorithm steps:

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.

3. Return LCM: The LCM is the result of the formula.


Python Code:

You might also like