Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
31 views

Lecture#05algorithm (Lecture)

The document discusses algorithms and their complexity. It defines an algorithm as a step-by-step procedure to solve a problem and provides examples of algorithms to find the sum, product, and average of numbers and to find the largest of three numbers. It explains that complexity measures the resources required by an algorithm and is commonly expressed using Big O notation. Common sorting algorithms like insertion sort and bubble sort are discussed along with their quadratic time complexity. Searching algorithms like linear search and binary search are also mentioned.

Uploaded by

muhabdull30
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Lecture#05algorithm (Lecture)

The document discusses algorithms and their complexity. It defines an algorithm as a step-by-step procedure to solve a problem and provides examples of algorithms to find the sum, product, and average of numbers and to find the largest of three numbers. It explains that complexity measures the resources required by an algorithm and is commonly expressed using Big O notation. Common sorting algorithms like insertion sort and bubble sort are discussed along with their quadratic time complexity. Searching algorithms like linear search and binary search are also mentioned.

Uploaded by

muhabdull30
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Introduction to Info and

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

• Complexity is a rough approximation of the number of


steps necessary to execute an algorithm.
• It is used to measure the efficiency of algorithm.
• The complexity of an algorithm computes the amount of time and
spaces required by an algorithm for an input of size (n).
Algorithm Complexity

The performance of the algorithm can be measured in two factors:


• Time complexity: The time complexity of an algorithm is the amount
of time required to complete the execution.
• The time complexity of an algorithm is denoted by the big O notation.
Here, big O notation is the asymptotic notation to represent the time
complexity.
• Space complexity: An algorithm's space complexity is the amount of
space required to solve a problem and produce an output. Similar to
the time complexity, space complexity is also expressed in big O
notation.
Time complexity: Best, Worst and Average
Case
Worst case( Big-Oh)
• Complexity of algorithms is usually evaluated in the worst case (most
unfavorable scenario).
• “Big-Oh” specifies an upper bound of complexity .
• Let’s take an example: searching in array. To find the searched key in
the worst case, we have to check all the elements in the array.
• Or it may happened that we check ‘n’ element but the element is not
in the List.
Best case: (Omega)

Best case: (Omega)


• Big Omega is just opposite to Big Oh.

• 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

• What is the maximum number of entries that must be interrogated


when applying the binary search to a list of 200 entries? What about a
list of 100,000 entries?
Sorting Algorithms
• Sorting refers to arranging data in a particular format. Sorting
algorithm specifies the way to arrange data in a particular order.
Ascending or Descending order.
• Two Algorithms
- Bubble Sort
- Insertion Sort
Complexity of insertion sort and Bubble sort is O(n2)
Insertion Sort
Insertion Sort
Working of Insertion Sort algorithm:
Consider an example: Data : {12, 11, 13, 5, 6}

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, move to the next two elements and compare them


11 12 13 5 6
Here, 13 is greater than 12, thus both elements seems to be in ascending order,
hence, no swapping will occur. 12 also stored in a sorted sub-array along with 11
Third 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.

You might also like