Lecture#05algorithm (Lecture)
Lecture#05algorithm (Lecture)
Communication
Technology(IICT)
Algorithm and its Complexity
Algorith
m
• An algorithm is a set of instructions designed to perform a
specific task.
• An algorithm is a step by step procedure to solve logical and
mathematical problems.
• An algorithm is a finite step-by-step procedure to achieve a required
result .
• A recipe is a good example of an algorithm because it says what must
be done, step by step. It takes inputs (ingredients) and produces an
output (the completed dish).
Pseudocode
• Pseudocode is a simple way of writing programming code in English.
• Pseudocode is an informal high-level representation of the actual
code that shows how an algorithm or a computer program works in
plain English.
• pseudocode is the way of expressing a program or code so that it
could be easily understood by programmers of every programming
languages.
• No standard for pseudocode syntax exists, as a program
in pseudocode is not an executable program.
Pseudocode
Examples of Pseudocode
• An example of pseudocode to create a program to add 2 numbers
together and then display the result.
Start Program
Enter two numbers, A, B
Add the numbers together
Print Sum
End Program
Algorithm : Example:
write an algorithm to find the sum ,product and average of five numbers
Step1: start
Let the five numbers be A=2,B=5,C=8,D=4 and E=12.
Step2: Find sum (SUM)
SUM=A+B+C+D+E
Step 3: FIND the product (PROD) PROD=A*B*C*D*E
Step 4: FIND the average (AVG) AVG=SUM/5
Step 5: Output SUM, PROD, AVG
Step 6: Stop
Algorithm: Find the largest of three unequal
numbers
• Step 1: Start
Let the three numbers be A=10, B=20 and C=30
• Step 2: Check if A is the largest number
IF A>B and A>C THEN LARGEST=A otherwise GOTO Step 4
• Step 3: GOTO Step 7
Step 4:check if B is the largest number
if B>A and B>C THEN LARGEST=B otherwise GOTO step5
Step 5: GOTO Step 7
Step 6: Largest=C
Step 7:OUTPUT LARGEST
Step 8: Stop
Complexity of Algorithm
• It generalizes the concept of “lower bound” () in the same way as Big Oh
generalizes the concept of “upper bound” (≤).
• Let’s take an example: searching in array. To find the searched key in
the best case we will have luck and we will find the element at first position.
• In the best case we have a constant complexity O(1), because we make only
one step and directly find the element.
Average case :Big-Theta
• In the average case we can expect to check half the elements in the
array until we find the one we are looking for.
A traffic lights example
Repetition
Searching and Sorting
Information retrieval is one of the most important applications of
computers. Searching is basically finding element in the data.
Basics Searching Techniques
1. Linear search
2. Binary Search
Searching
12 11 13 5 6
First Pass:
Initially, the first two elements of the array are compared in insertion sort.
12 11 13 5 6
Here, 12 is greater than 11 hence they are not in the ascending order and 12 is not
at its correct position. Thus, swap 11 and 12.
So, for now 11 is stored in a sorted sub-array.
11 12 13 5 6
Second Pass:
Now, two elements are present in the sorted sub-array which are 11 and 12
Moving forward to the next two elements which are 13 and 5
11 12 13 5 6
Both 5 and 13 are not present at their correct place so swap them
11 12 5 13 6
After swapping, elements 12 and 5 are not sorted, thus swap again
11 5 12 13 6
Here, again 11 and 5 are not sorted, hence swap again
5 11 12 13 6
here, it is at its correct position
Fourth Pass:
Now, the elements which are present in the sorted sub-array are 5, 11 and 12
Moving to the next two elements 13 and 6
5 11 12 13 6
Clearly, they are not sorted, thus perform swap between both
5 11 12 6 13
Now, 6 is smaller than 12, hence, swap again
5 11 6 12 13
Here, also swapping makes 11 and 6 unsorted hence, swap again
5 6 11 12 13
Finally, the array is completely sorted.