0% found this document useful (0 votes)
6 views

Algorithms

Uploaded by

iqra
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 views

Algorithms

Uploaded by

iqra
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/ 24

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.

Binary Search Algorithm


1.Start with a sorted list and set pointers to the start and end of the list.
2.Find the middle of the list.
3.Check if the middle element is the target:
1. If yes, you're done!
2. If no, check if the target is smaller or larger than the middle element:
1. If smaller, focus on the left half.
2. If larger, focus on the right half.
4.Keep cutting the list in half and checking the middle element until you
find the target or run out of elements.
Working of binary and Linear Search
What is Insertion Sort?
Insertion Sort works by dividing the list into two parts: a sorted part and an unsorted part.
It picks elements from the unsorted part and inserts them into the correct position in the sorted part.

Insertion Sort Algorithm


1. Start with the second element (since the first element is considered sorted).
2. Compare it to the elements before it (in the sorted part).
3. Shift the larger elements one position to the right to make space.
4. Insert the selected element into its correct position.
5. Repeat this for every element in the list until the entire list is sorted.
Example of Insertion Sort
Unsorted List:
5,2,9,1,5,65, 2, 9, 1, 5, 65,2,9,1,5,6
1.Start with the second element, 2.
•Compare with 5. Since 2 is smaller, shift 5 and insert 2.
•List becomes: 2,5,9,1,5,62, 5, 9, 1, 5, 62,5,9,1,5,6
2.Next element is 9. It is in the right place.
•List stays: 2,5,9,1,5,62, 5, 9, 1, 5, 62,5,9,1,5,6
3.Next element is 1.
•Compare with 9, 5, and 2. Shift them all and insert 1.
•List becomes: 1,2,5,9,5,61, 2, 5, 9, 5, 61,2,5,9,5,6
4.Next element is 5.
•Compare with 9 and 5. Shift 9 and insert 5.
•List becomes: 1,2,5,5,9,61, 2, 5, 5, 9, 61,2,5,5,9,6
5.Last element is 6.
•Compare with 9. Shift 9 and insert 6.
•List becomes: 1,2,5,5,6,91, 2, 5, 5, 6, 91,2,5,5,6,9
Now, the list is sorted.
What is Bubble Sort?
Bubble Sort works by repeatedly comparing adjacent elements and swapping them if they are in
the wrong order. This process "bubbles" the largest element to the end of the list, and then the
next largest, until the entire list is sorted.

Bubble Sort Algorithm

1. Start with the first element and compare it to the next one

2. If the first is bigger, swap them.

3.Move to the next pair of elements and do the same.

4.Keep doing this until the largest number is at the end.

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

1.Input: A sequence of numbers s1,s2,...Sn,


2.Start by assuming the first element s1s_1s1​is the largest.
3.Compare this element with the next element s2s_2s2​.
1. If s2s_2s2​is larger, update the largest to s2s_2s2​.
4.Continue comparing the current largest element with each
subsequent element in the sequence.
5.Keep updating the largest element if a larger one is found.
6.At the end, the largest element is the one that was never
replaced.
7.Output the largest element.
Algorithm to Find the Maximum Number in an Array of Integers

•Input: An array of integers.

•Assume the first number in the array is the largest.

•Compare the assumed largest number with the next number in the array.

•If the next number is larger, update the largest.

•Repeat this process for each number in the array.

•Once all numbers are checked, the largest number will be the result.

•Output the largest number.


Algorithm to Find the Sum of Two Digits of a Number

1.Input a two-digit number.

2.Extract the first digit by dividing the number by 10.

3.Extract the second digit by getting the remainder when

dividing the number by 10.

4.Add the two digits together.

5.Output the sum.


Algorithm to Check if a Number is Prime

•Input a number.

•If the number is less than 2, it's not prime.

•Check divisibility by numbers from 2 to the square root of the number:

•If the number is divisible by any of these, it's not prime.

•If it's not divisible by any of these, it's prime.

•Output whether the number is prime or not.


Algorithm to Find Factorial:

1.Start

2.Input: Get the value of n (the number you want the factorial of).

3.Initialize: Set a variable factorial = 1. This will store the result.

4.Loop: From i = 1 to n:

•Multiply factorial by i and store the result back in factorial.

5.End Loop when i reaches n.

6.Output: The value of factorial (this is the factorial of 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:

•Set a = 0 (the first term)

•Set b = 1 (the second term)

•If n is greater than 0, print a

•If n is greater than 1, print b

4.Loop: From i = 2 to n-1:

•Compute next_term = a + b

•Print next_term

•Update a to b (the previous term)

•Update b to next_term (the current term)

5.End Loop when i reaches n-1.

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

Definition: A growth function describes how the running time or space

requirements of an algorithm increase as the size of the input grows.

In Simple Terms: It shows how the resource needs of an algorithm change as you

give it more data to work with.

Time Complexity and Asymptotic Notations

Definition: Time complexity measures how the runtime of an algorithm grows as

the size of the input increases. Asymptotic notations are used to describe this

growth in a precise way.


Big O Notation (O)
Big O notation describes the upper bound of the time complexity of an algorithm.
It provides an upper limit on the growth rate,
ensuring that the algorithm will not grow faster than this limit as the input size increases.
In Simple Terms: It tells you the worst-case scenario for how long an algorithm will take.
Example: If an algorithm’s time complexity is O(n^2), it means that in the worst case, the time it takes to
run thealgorithm will grow proportional to the square of the input size n.
Formal Definition: f(n) = O(g(n)) means there are positive constants c and k, such that
0 ≤ f(n) ≤ cg(n) for all n ≥ k
. The values of c and k must be fixed for the function f and must not depend on n.
Big Omega Notation (Ω)
Definition: Big Omega notation describes the lower bound of the time complexity. It provides a guarantee that the
algorithm will take at least this much time.

Given two functions g(n) and f(n), we say that


f(n) = Ω(g(n)), if there exists constants c > 0 and n0 >= 0 such that
f(n) >= c*g(n) for all n >= n0
Big Theta Notation (Θ)
Definition: Big Theta notation provides both an upper and a lower bound on the time complexity. It describes the exact
asymptotic behavior of an algorithm.

f(n) : There exist positive constant c1, c2 and n0 such that


0 ≤ c1 g(n) ≤ f(n) ≤ c2 g(n), for all n ≥ n0

You might also like