You are on page 1of 32

Algorithms

What is Algorithm
An algorithm is a procedure used for solving a problem or performing a
computation.

Algorithms act as an exact list of instructions that conduct specified


actions step by step in either hardware- or software-based routines
Which sequence will give the correct picture of
an owl?
Which sequence will give the correct
algorithm for boiling water?
1. Heat the pot until water boils
2. Turn the stove on
3. Take an empty pot
4. Put the water-filled pot on the fire
5. Pour water in empty pot
What will be the first step of addition
algorithm
•Adding digits of any column
•Adding digits of ones column
•Placing digits vertically in columns
•Carry over digits of a column
EXAMPLE 1

Describe an algorithm for finding the maximum (largest) value in a


finite sequence of integers.
Solution 2 6 7 9 10 3
We perform the following steps.
1. Set the temporary maximum equal to the first integer in the
sequence. (The temporary maximum will be the largest integer
examined at any stage of the procedure.)
2. Compare the next integer in the sequence to the temporary
maximum, and if it is largerthan the temporary maximum, set the
temporary maximum equal to this integer.
3. Repeat the previous step if there are more integers in the sequence.
4. Stop when there are no integers left in the sequence. The temporary
maximum at this point is the largest integer in the sequence.
pseudocode
is a plain language description of the steps in an algorithm or another
system.

-often uses structural conventions of a normal programming language,


but is intended for human reading rather than machine reading.

It typically omits details that are essential for machine understanding of


the algorithm, such as variable declarations and language-specific code
A pseudocode description of the algorithm for finding the maximum element in a
finite sequence follows.

This algorithm first assigns the initial term of the sequence, a1, to the variable
max. The “for”loop is used to successively examine terms of the sequence. If a
term is greater than the current value of max, it is assigned to be the new value of
max.
PROPERTIES OF ALGORITHMS

Input. An algorithm has input values from a specified set.

Output. From each set of input values an algorithm


produces output values from a specified set. The output
values are the solution to the problem.

Definiteness. The steps of an algorithm must be defined


precisely.

Correctness. An algorithm should produce the correct


output values for each set of input values.
Finiteness. An algorithm should produce the desired output
after a finite (but perhaps large) number of steps for any input in
the set.

Effectiveness. It must be possible to perform each step of an


algorithm exactly and in a finite amount of time.

Generality. The procedure should be applicable for all problems


of the desired form, not just for a particular set of input values.
EXAMPLE 2

Show that Algorithm 1 for finding the maximum element in a finite


sequence of integers has all the properties listed.
Solution:

The input to Algorithm 1 is a sequence of integers.

The output is the largest integer in the sequence.

Each step of the algorithm is precisely defined, because only assignments, a


finite loop, and conditional statements occur.

To show that the algorithm is correct, we must show that when the
algorithm terminates, the value of the variable max equals the maximum of
the terms of the sequence.
Searching Algorithms
A search algorithm is the step-by-step procedure used to locate specific
data among a collection of data.

It is considered a fundamental procedure in computing.

when searching for data, the difference between a fast application and
a slower one often lies in the use of the proper search algorithm
Linear or Sequential Search
Consider
arr = [2, 12, 15, 11, 7, 19, 45]
0 1 2 3 4 5 6
Suppose the target element we want to search is 7.

Approach for Linear or Sequential Search

Start with index 0 and compare each element with the target
If the target is found to be equal to the element, return its index
If the target is not found, return -1
The procedures for implementing linear search are as follows:

Step 1: First, read the search element (Target element) in the array.

Step 2: In the second step compare the search element with the first element in the array.

Step 3: If both are matched, display "Target element is found" and terminate the Linear Search function.

Step 4: If both are not matched, compare the search element with the next element in the array.

Step 5: In this step, repeat steps 3 and 4 until the search (Target) element is compared with the last
element of the array.

Step 6 - If the last element in the list does not match, the Linear Search Function will be terminated, and
the message "Element is not found" will be displayed.
Time Complexity Analysis

The Best Case occurs when the target element is the first element of the array.
The number of comparisons, in this case, is 1. So, the time complexity is O(1).

The Average Case: On average, the target element will be somewhere in the
middle of the array. The number of comparisons, in this case, will be N/2. So,
the time complexity will be O(N) (the constant being ignored).

The Worst Case occurs when the target element is the last element in the array
or not in the array. In this case, we have to traverse the entire array, and so the
number of comparisons will be N. So, the time complexity will be O(N)
Binary Search
- is a searching algorithm used in a sorted array by repeatedly dividing
the search interval in half.
- works on the principle of divide and conquer and it is considered the
best searching algorithm because it's faster to run.

-if the terms are numbers, they are listed from smallest to largest;
if they are words, they are listed in lexicographic, or alphabetic, order).
The binary search works in the following manner:

The search process initiates by locating the middle element of the sorted array
of data

After that, the key value is compared with the element

If the key value is smaller than the middle element, then searches analyses the
lower values to the middle element for comparison and matching

In case the key value is greater than the middle element then searches analyses
the upper values to the middle element for comparison and matching
Example
You have an array of sorted values ranging from 2 to 20 and need to locate
18.

The average of the lower and upper limits is (l + r) / 2 = 4. The value being
searched is greater than the mid which is 4.

The array values less than the mid are dropped from search and values
greater than the mid-value 4 are searched.

This is a recurrent dividing process until the actual item to be searched is


found.
Activity
• Use the linear and binary searching algorithms to locate n=22 in set A
• 0 1 2 3 4 5 6 7 8 9
A= { 0, 2, 4, 8, 9, 11, 14, 22, 27, 30}

Linear:

Binary:
Sorting
-refers to ordering data in an increasing or decreasing manner
according to some linear relationship among the data items.

ordering: arranging items in a sequence ordered by some criterion;

categorizing: grouping items with similar properties.


Bubble sort
is the simplest sorting algorithm that works by repeatedly swapping the
adjacent elements if they are in the wrong order. This algorithm is not
suitable for large data sets as its average and worst-case time
complexity is quite high.
How does Bubble Sort Work?
Input: arr[] = {5, 1, 4, 2, 8}

First Pass:

Bubble sort starts with very first two elements, comparing them to check which
one is greater.
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and
swaps since 5 > 1.
( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5),
algorithm does not swap them.
Second pass:

Now, during second iteration it should look like this:


( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Third Pass:

Now, the array is already sorted, but our algorithm does not know if it is
completed.

The algorithm needs one whole pass without any swap to know it is sorted.

( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
EXAMPLE 4
Use the bubble sort to put 3, 2, 4, 1, 5 into increasing order.

You might also like