Day 1
Day 1
1
DAY-1
TABLE OF CONTENTS
2
Defining Problem
In programming, a problem refers to a task or challenge that needs to be solved using code. It could be anything from
writing a simple function to fix a bug in software to designing a complex algorithm.
Algorithm
An algorithm is a step-by-step procedure or a set of rules to solve a problem in a finite number of steps. In
programming, algorithms are used to process data, perform calculations, and automate reasoning tasks.
1. Start
3
2. Set a variable max to the first element of the list
Types of Algorithms:
1. Sorting Algorithms – Arrange elements in order (e.g., Bubble Sort, Quick Sort, Merge Sort).
2. Searching Algorithms – Find an element in a data structure (e.g., Binary Search, Linear Search).
3. Recursion-Based Algorithms – Solve problems by breaking them into smaller subproblems (e.g., Factorial
Calculation, Fibonacci Series).
4. Graph Algorithms – Used in networking and pathfinding (e.g., Dijkstra’s Algorithm, A* Algorithm).
5. Greedy Algorithms – Make the best choice at each step (e.g., Kruskal’s Algorithm).
6. Divide and Conquer Algorithms – Split the problem, solve parts, then merge results (e.g., Merge Sort).
7. Dynamic Programming – Solve problems efficiently by storing past results (e.g., Fibonacci with Memoization).
Steps:
1. Start
5. Stop
1. Start
4
3. Compare the numbers:
o Else if num2 is greater than num1 and num3, then max = num2
5. Stop
1. Start
5. Stop
Flowchart
A flowchart is a visual representation of an algorithm or process using symbols and arrows. It helps in understanding the
logical flow of a program or system.
5
Figure 1. Flowchart calculating addition of two numbers
6
Linear Search vs Binary Search – Understanding Problem Solving
Before understanding the differences between the linear and binary search, we should first know the linear search and
binary search separately.
Linear search (or sequential search) is a simple searching algorithm used to find an element in a list or array. It works by
checking each element one by one until the desired element is found or the list ends.
How It Works
5. If the end of the list is reached without finding the element, return -1 (or some indication that the element is not
present).
Time Complexity
Worst Case: O(n) (if the element is at the last position or not in the list).
Algorithm Steps
4. If the end of the list is reached without finding the element, return -1 (or any indication that the element is not
found).
7
Binary Search is an efficient algorithm used to find an element in a sorted list by repeatedly dividing the search space in
half.
4. Repeat steps 2-3 until the target is found or the search space is empty.
Time Complexity
Binary Search is significantly more efficient than Linear Search, especially for large datasets. Here’s why:
1. Time Complexity
8
Algorithm Best Case Worst Case Average Case
Linear Search: In the worst case, it has to check every element, taking O(n) time.
Binary Search: It divides the list in half at each step, reducing the number of comparisons, making it much faster
with O(log n) time.
For example: