Practical Programming -L02
Practical Programming -L02
Greedy Algorithm Chooses the best option at each step without considering Finding the shortest path in a
the overall result. graph.
Brute Force Tries all possible solutions to find the best one. Cracking a password by trying
all combinations.
Recursive Algorithm Solves a problem by breaking it into smaller instances of Calculating factorial or
the same problem. Fibonacci numbers.
Divide and Conquer Breaks a problem into smaller sub-problems, solves Merge Sort, Quick Sort.
them, and combines results.
Backtracking Tries different solutions and abandons them if they don’t Solving Sudoku or the N-
work. Queens problem.
Hashing Algorithm Maps data of any size to a fixed-size value (hash). Password storage, data
retrieval.
Searching Algorithm Finds an element in a data structure. Binary Search, Linear Search.
8. Searching and Sorting Algorithms
Searching Algorithms
• Linear Search: Sequentially checks each element in a list.
• Time Complexity: O(n).
• Best for: Small datasets.
• Binary Search: Efficient for sorted lists.
• Time Complexity: O(log n).
• Best for: Large, sorted datasets.
8. Searching and Sorting Algorithms
Sorting Algorithms
• Bubble Sort: Simple but inefficient for large datasets.
• Time Complexity: O(n²).
• Merge Sort: Efficient and stable.
• Time Complexity: O(n log n).
• Quick Sort: Fast and in-place.
• Time Complexity: O(n log n) on average.
9. Summary
• Key Takeaways:
• Algorithms are step-by-step procedures to solve
problems.
• They must be clear, finite, and produce the correct
output.
• Algorithms can be expressed in natural language,
flowcharts, or pseudo-code.
• Next Steps:
• Practice creating algorithms for different problems.
• Learn how to implement algorithms in C++ or other
programming languages.
10. Q&A and Discussion
11. Practical Exercises
1. Find the Maximum Number in a List:
• Write an algorithm to find the largest number in a list of integers.
• Test it with the following list: [3, 7, 2, 9, 4].
2. Calculate the Average of Numbers:
• Write an algorithm to calculate the average of a list of numbers.
• Test it with the following list: [10, 20, 30, 40, 50].
3. Sort a List of Numbers:
• Write an algorithm to sort a list of numbers in ascending order.
• Test it with the following list: [5, 3, 8, 1, 2].
Interactive Example 1
Problem: Find the maximum number in a list.
Algorithm:
1. Start
2. Read the list of numbers.
3. Set the first number as the maximum.
4. Compare each number with the current maximum.
5. If a number is greater, update the maximum.
6. Repeat until the end of the list.
7. Print the maximum number.
8. Stop.
Interactive Example 2
Problem: Calculate the Average of Numbers.
Algorithm:
1. Input: A list of numbers, numbers = [n1, n2, n3, ……,nk].
2. Steps:
• Initialize a variable total_sum to 0.
• Initialize a variable count to 0.
• Iterate through each number in the list:
• Add the current number to total_sum.
• Increment count by 1.
• Calculate the average by dividing total_sum by count
• Return the average.
3. Output: The average of the numbers in the list.
Interactive Example 3
Problem: Write an algorithm to sort a list of numbers in ascending order.
Algorithm:
a. Input: A list of numbers, numbers = [n1, n2, n3, ……,nk].
b. Steps:
1. Start with the first element of the list.
2. Compare the current element with the next element.
3. If the current element is greater than the next element, swap them.
4. Move to the next element and repeat step 2-3 until the end of list.
5. Repeat the entire process for the list until no swaps are needed.
6. Return the sorted list
c. Output: Sorted list.