0% found this document useful (0 votes)
6 views25 pages

Lab 3

The document discusses various algorithmic strategies, focusing on brute force and divide and conquer methods, as well as searching and sorting algorithms. It highlights the pros and cons of brute force and divide and conquer approaches, and provides examples of linear and binary search algorithms, along with selection and merge sort techniques. The document concludes with practical exercises related to selection sort, merge sort, and binary search.

Uploaded by

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

Lab 3

The document discusses various algorithmic strategies, focusing on brute force and divide and conquer methods, as well as searching and sorting algorithms. It highlights the pros and cons of brute force and divide and conquer approaches, and provides examples of linear and binary search algorithms, along with selection and merge sort techniques. The document concludes with practical exercises related to selection sort, merge sort, and binary search.

Uploaded by

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

THEORY

OF
ALGORITHMS
LAB3
AGENDA
• Brute force vs Divide and conquer
• Linear Search vs Binary search
• Selection sort vs Merge sort

Theory of algorithms Lab3


BRUTE FORCE
A brute force algorithm is a
simple, comprehensive search
strategy that systematically
explores every option until a
problem’s answer is
discovered. It’s a generic
approach to problem-solving
that’s employed when the
issue is small enough to make
an in-depth investigation
possible. However, because of
their high temporal
complexity, brute force
techniques are inefficient for
Theory of algorithms Lab3
large-scale issues.
BRUTE FORCE
Pros-
•Easy to code and understand
•Solves a problem; generates a solution
•Stepping stone in the development of
better algorithms.

Cons-
•Fairly inefficient, in both time complexity
and space
•Not very suitable in real world
applications.

Theory of algorithms Lab3


DIVIDE AND CONQUER
Divide and Conquer Algorithm
involves breaking a larger problem
into smaller subproblems, solving
them independently, and then
combining their solutions to solve
the original problem. The basic idea
is to recursively divide the problem
into smaller subproblems until they
become simple enough to be solved
directly. Once the solutions to the
subproblems are obtained, they are
then combined to produce the
overall solution.

Theory of algorithms Lab3


DIVIDE AND CONQUER
1. Divide: 3. Merge:
Break down the original problem into smaller Combine the sub-problems to get the
subproblems. final solution of the whole problem.
Each subproblem should represent a part of Once the smaller subproblems are
the overall problem. solved, we recursively combine their
The goal is to divide the problem until no solutions to get the solution of larger
further division is possible. problem.
2. Conquer: The goal is to formulate a solution for
Solve each of the smaller subproblems the original problem by merging the
individually. results from the subproblems.
If a subproblem is small enough (often
referred to as the “base case”), we solve it
directly without further recursion.
The goal is to find solutions for these
subproblems independently.
Theory of algorithms Lab3
DIVIDE AND CONQUER
Pros Cons
• Efficiency: Reduces time • Recursion Overhead: High
complexity (e.g., Merge Sort: memory usage and risk of
O(nlog⁡n)O(nlogn)). stack overflow.
• Parallelism: Subproblems can • Complex Implementation:
be solved independently. Dividing and combining
• Simplifies Problems: Breaks results can be tricky.
complex problems into • Subproblem Overlap: May
smaller, manageable parts. lead to redundant
• Optimal Solutions: Often computations.
provides proven, optimal • Not Always Suitable: Some
results. problems can’t be easily
• Scalability: Handles large divided.
inputs effectively. Theory of algorithms Lab3
SEARCHING ALGORITHMS
• Linear Search
• Sentinel Linear Search
• Binary Search
• Meta Binary Search | One-Sided Binary Search
• Ternary Search
• Jump Search
• Interpolation Search
• Exponential Search
• Fibonacci Search
• The Ubiquitous Binary Search

Theory of algorithms Lab3


LINEAR SEARCH

Theory of algorithms Lab3


LINEAR SEARCH
#include <iostream> int main() {
#include <vector> int arr[] = {2, 3, 4, 10,
using namespace std; 40};
int x = 10;
int search(int arr[], int x) { int res = search(arr, x);
for (int i = 0; i < sizeof(arr); i+ if (res == -1)
+) cout << "Not Present";
if (arr[i] == x) else
return i; cout << "Present at Index
return -1; " << res;
} return 0;
}

Theory of algorithms Lab3


LINEAR SEARCH
Time Complexity of Linear Search Algorithm:
Time Complexity:
Best Case: In the best case, the key might be present at the first index.
So the best case complexity is O(1)
Worst Case: In the worst case, the key might be present at the last index
i.e., opposite to the end from which the search has started in the list. So
the worst-case complexity is O(N) where N is the size of the list.
Average Case: O(N)

Theory of algorithms Lab3


BINARY SEARCH
Binary search is a search algorithm used to find the position of a target value within a sorted array. It works by
repeatedly dividing the search interval in half until the target value is found or the interval is empty. The search
interval is halved by comparing the target element with the middle value of the search space.
The Binary Search Algorithm can be implemented in the following two ways:
• Iterative Binary Search Algorithm
• Recursive Binary Search Algorithm

Theory of algorithms Lab3


BINARY SEARCH

Theory of algorithms Lab3


BINARY SEARCH
#include <iostream> // Driver code
using namespace std; int main(void)
// An iterative binary search function. {
int binarySearch(int arr[], int low, int high, int x) int arr[] = { 2, 3, 4, 10, 40 };
{ int x = 10;
while (low <= high) {
int n = sizeof(arr) / sizeof(arr[0]);
int mid = low + (high - low) / 2;
int result = binarySearch(arr, 0, n - 1, x);
// Check if x is present at mid if (result == -1) cout << "Element is not present in
if (arr[mid] == x) array";
return mid; else cout << "Element is present at index " <<
result;
// If x greater, ignore left half return 0;
if (arr[mid] < x) }Outp
low = mid + 1;
ut:
// If x is smaller, ignore right half
else

}
high = mid - 1;
Time Complexity:
// If we reach here, then element was not present
return -1;
O(log N)
} Theory of algorithms Lab3
BINARY SEARCH(WITH RECURSION)
#include <iostream> int main() {
using namespace std; int arr[] = {1, 3, 5, 7, 9, 11, 13, 15};
int size = sizeof(arr) / sizeof(arr[0]);
int binarySearch(int arr[], int low, int high, int target) { int target = 7;
// Base case: target not found int result = binarySearch(arr, 0, size - 1,
if (low > high) target);
return -1;
if (result != -1)
int mid = (low + high) / 2; cout << "Element found at index " << result
<< endl;
// Check if the middle element is the target else
if (arr[mid] == target) cout << "Element not found" << endl;
return mid; return 0;
Outp
// If target is smaller than mid, search in the left half }
else if (arr[mid] > target)
return binarySearch(arr, low, mid - 1, target); ut:
// If target is larger than mid, search in the right half
else
return binarySearch(arr, mid + 1, high, target);
}

Theory of algorithms Lab3


SORTING ALGORITHMS
• Selection
Sort
• Bubble Sort
• Insertion
Sort
• Merge Sort
• Quick Sort
• Heap Sort
• Cycle Sort
• 3-way
• Merge
Sort Theory of algorithms Lab3
SELECTION SORT
Selection Sort is a comparison-
based sorting algorithm. It sorts an
array by repeatedly selecting the
smallest (or largest) element from
the unsorted portion and swapping
it with the first unsorted element.
This process continues until the
entire array is sorted.
1.First we find the smallest
element and swap it with the
first element. This way we get
the smallest element at its
correct position.
2.Then we find the smallest
among remaining elements (or
second smallest) and swap it
with the second element.
Theory of algorithms Lab3
We keep doing this until we get all
SELECTION SORT
#include <iostream> void printArray(int arr[], int n) {
using namespace std; for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
void selectionSort(int arr[], int n) { }
for (int i = 0; i < n - 1; i++) { cout << endl;
// Find the minimum element in the unsorted }
part of the array int main() {
int minIndex = i; int arr[] = {64, 25, 12, 22, 11};
for (int j = i + 1; j < n; j++) { int n = sizeof(arr) / sizeof(arr[0]);
if (arr[j] < arr[minIndex]) { cout << "Original array: ";
minIndex = j; printArray(arr, n);
} selectionSort(arr, n);
} cout << "Sorted array: ";
printArray(arr, n);
// Swap the found minimum element with the return 0;
first element }
if (minIndex != i) {
swap(arr[i], arr[minIndex]);
}
Theory of algorithms Lab3
}
SELECTION SORT
Time Complexity:
Best Case: O(n²) (even if the array is already sorted, the algorithm still
performs all comparisons).
Average Case: O(n²).
Worst Case: O(n²).

Theory of algorithms Lab3


MERGE SORT
Merge sort is a sorting algorithm that
follows the divide-and-conquer
approach. It works by recursively
dividing the input array into smaller
subarrays and sorting those subarrays
then merging them back together to
obtain the sorted array.

Theory of algorithms Lab3


MERGE SORT

Theory of algorithms Lab3


MERGE SORT

Theory of algorithms Lab3


MERGE SORT

Theory of algorithms Lab3


SHEET 3
Question 1: Given the following array elements:
A = [3, 41, 52, 26, 8, 57, 9, 49]
a) Rewrite the array after each step in the selection sorting algorithm.
b) Illustrate the operation of the merge sorting algorithm on A.
c) State the steps of searching for key = 9 using the binary Search algorithm. At each step show the
low, high, and mid values.

Theory of algorithms Lab3


THANK
YOU
Theory of algorithms Lab3

You might also like