Algorithms
Algorithms
I Q R A M U S H TA Q
LECTURER
C S & I T D E PA RT M E N T
IQRA GIRLS COLLEGE
Linear Search Algorithm
•S ta r t a t th e f ir s t n u m b e r in th e lis t.
•C h e c k if it m a tc h e s th e ta rg e t n u m b e r.
•I f y e s , s to p . I f n o , m o v e to th e n e x t n u m b e r.
•K e e p d o in g th is u n til y o u f in d th e ta rg e t o r r e a c h th e
e n d o f th e lis t .
What is Binary Search?
Binary search is a more efficient way to search a sorted list by repeatedly dividing the
search space in half. It compares the target number to the middle element of the list, then
eliminates half of the list based on whether the target is smaller or larger than the middle
element.
1. Start with the first element and compare it to the next one
5.Repeat the process until no more swaps are needed, and the
list is sorted.
Example of Bubble Sort
Unsorted List:
5,2,9,1,5,65, 2, 9, 1, 5, 65,2,9,1,5,6
1.First pass:
1. Compare 5 and 2. Swap.
2. Compare 5 and 9. No swap.
3. Compare 9 and 1. Swap.
4. Compare 9 and 5. Swap.
5. Compare 9 and 6. Swap.
6. List becomes: 2,5,1,5,6,92, 5, 1, 5, 6, 92,5,1,5,6,9
2.Second pass:
1. Compare 2 and 5. No swap.
2. Compare 5 and 1. Swap.
3. Compare 5 and 5. No swap.
4. Compare 5 and 6. No swap.
5. List becomes: 2,1,5,5,6,92, 1, 5, 5, 6, 92,1,5,5,6,9
3.Third pass:
1. Compare 2 and 1. Swap.
2. Compare 2 and 5. No swap.
3. List becomes: 1,2,5,5,6,91, 2, 5, 5, 6, 91,2,5,5,6,9
Algorithm to Find the Largest Element in a Sequence
•Compare the assumed largest number with the next number in the array.
•Once all numbers are checked, the largest number will be the result.
•Input a number.
1.Start
2.Input: Get the value of n (the number you want the factorial of).
4.Loop: From i = 1 to n:
7.End
Fibonacci Series Algorithm:
2.Input: Get the value of n (the number of terms you want in the Fibonacci series).
3.Start
3.Initialize:
•Compute next_term = a + b
•Print next_term
6.End
Greedy Algorithm: A greedy algorithm is a simple approach
Halting Problem: The halting problem
to solving problems by making the most optimal choice at
each step, with the hope that these local choices will lead to a is a famous problem in computer science
globally optimal solution.
that asks whether a given computer
Example:
program will eventually stop (halt) or run
Making Change: If you need to make change for a dollar
using coins of 25, 10, 5, and 1 cents, a greedy algorithm forever when given a specific input.
would always pick the largest coin possible until the amount
is made.
Growth Function
In Simple Terms: It shows how the resource needs of an algorithm change as you
the size of the input increases. Asymptotic notations are used to describe this