0% found this document useful (0 votes)
12 views9 pages

Day 1

Uploaded by

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

Day 1

Uploaded by

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

ADVANCED CREDIT PROGRAM-2025

COURSE NAME: CODING

1
DAY-1

TABLE OF CONTENTS

SR. NO NAME OF THE TOPIC PAGE NO


1. Defining Problem 3
2. Algorithm 3
3. Types of Algorithms 4
4. Flowchart 5
5. Linear Search vs Binary Search 7
6. Frequently Asked Questions 9
7. Reading Material 9

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.

Solving Programming Problems:

 Understand the problem: Read and analyse the requirements carefully.

 Break it down: Divide it into smaller sub-problems.

 Plan a solution: Use algorithms, data structures, or logic to solve it.

 Write the code: Implement the solution in a programming language.

 Test the code: Debug and fix any issues.

 Optimize: Improve performance and efficiency.

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.

Characteristics of a Good Algorithm:

1. Definiteness – Each step must be clear and unambiguous.

2. Finiteness – It must complete in a finite number of steps.

3. Input – It should take zero or more inputs.

4. Output – It should produce at least one output.

5. Effectiveness – Each step must be simple and feasible to execute.

Example of a Simple Algorithm:

Finding the Largest Number in a List

1. Start

3
2. Set a variable max to the first element of the list

3. Loop through the list:

o If the current element is greater than max, update max

4. After the loop, max contains the largest number

5. Print max and stop

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

Algorithm to Find the Sum of Two Numbers

Steps:

1. Start

2. Input two numbers (num1 and num2)

3. Calculate the sum: sum = num1 + num2

4. Output the sum

5. Stop

Algorithm to Find the Maximum of Three Numbers

1. Start

2. Input three numbers (num1, num2, num3)

4
3. Compare the numbers:

o If num1 is greater than num2 and num3, then max = num1

o Else if num2 is greater than num1 and num3, then max = num2

o Else, max = num3

4. Output the maximum number

5. Stop

Algorithm to Print All Even Numbers from 1 to 10

1. Start

2. Initialize a variable num = 1

3. Loop from num = 1 to 10:

o If num is even (num % 2 == 0), print num

4. Repeat the loop until num reaches 10

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.

Why Use Flowcharts?

✅ Easy to Understand – Helps visualize the logic of a program.


✅ Error Detection – Helps identify mistakes in logic before implementation.
✅ Better Documentation – Acts as a reference for future development.
✅ Improved Communication – Helps programmers, developers, and stakeholders understand the process.

5
Figure 1. Flowchart calculating addition of two numbers

Figure 2. Flowchart calculating maximum 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

1. Start from the first element of the array.

2. Compare it with the target element.

3. If they match, return the index of the element.

4. If not, move to the next element and repeat.

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

 Best Case: O(1) (if the element is at the first position).

 Worst Case: O(n) (if the element is at the last position or not in the list).

 Average Case: O(n).

Algorithm Steps

1. Start from the first element of the list.

2. Compare each element with the target value.

3. If a match is found, return the index of the element.

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.

How Binary Search Works

1. Start with a sorted list and define two pointers:

o left at the beginning of the list

o right at the end of the list

2. Find the middle element of the current search range.

3. Compare it with the target:

o If the middle element matches the target, return its index.

o If the target is smaller, search in the left half.

o If the target is greater, search in the right half.

4. Repeat steps 2-3 until the target is found or the search space is empty.

Time Complexity

 Best Case: O(1) (if the middle element is the target).

 Worst Case: O(logn) (because the list is halved at each step).

 Average Case: O(logn)

Why is Binary Search Better than Linear Search?

Binary Search is significantly more efficient than Linear Search, especially for large datasets. Here’s why:

1. Time Complexity

Algorithm Best Case Worst Case Average Case

Linear Search O(1) O(n) O(n)

Binary Search O(1) O(logn) O(logn)

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:

 A list of 1,000,000 elements:

o Linear Search: Up to 1,000,000 comparisons.

o Binary Search: At most 20 comparisons

FREQUENTLY ASKED QUESTIONS


 What is time complexity in algorithms?
 What is space complexity?
 What are the characteristics of a good algorithm?
 How does a linear search work in data structures?
 Can you give me some examples of where linear searches are used?
 When should you use linear search instead of binary search?
 What’s the average and worst-case time complexity of Binary search?

SUGGESTED READING MATERIAL:


 Algorithms, https://brilliant.org/wiki/algorithm/
 Algorithm Fundamentals, https://brilliant.org/courses/computer-science-algorithms/
 Types of Algorithms, https://www.educba.com/types-of-algorithms/

You might also like