Unit 1-Daa
Unit 1-Daa
Characteristics of an algorithm
Input: Zero / more quantities are externally supplied.
Output: At least one quantity is produced.
Definiteness: Each instruction is clear and unambiguous.
Finiteness: The algorithm must terminates after a finite number of steps.
Efficiency: Every instruction must be very basic and runs in short time.
Also, consider the speed and amount of memory the algorithm would take
for different situations.
The algorithm yields a required result for every legitimate input in a finite
amount of time.A common technique for proving correctness is to use
mathematical induction because an algorithm’s iterations provide a natural
sequence of steps needed for such proofs.For an approximation algorithm, we
usually would like to show that the error produced by the algorithm does not
exceed a predefined limit.
Analysing an Algorithm:
Coding an algorithm
L’Hôpital’s rule:
2 Basic efficiency classes
Selection Sort
We start selection sort by scanning the entire given list to find its smallest
element and exchange it with the first element, putting the smallest
element in its final position in the sorted list. Then we scan the list,
starting with the second element, to find the smallest among the last n − 1
elements and exchange it with thesecond element, putting the second
smallest element in its final position. After n − 1 passes, the list is sorted.
Bubble Sort
Another brute-force application to the sorting problem is to compare
adjacent elements of the list and exchange them if they are out of order.
By doing it repeatedly, we end up “bubbling up” the largest element to the
last position
on the list. The next pass bubbles up the second largest element, and so
on,
until after n − 1 passes the list is sorted.
Sequential search
ALGORITHM SequentialSearch2(A[0..n], K)
//Implements sequential search with a search key as a sentinel
//Input: An array A of n elements and a search key K
//Output: The index of the first element in A[0..n − 1] whose value is
equal to K or −1 if no such element is found
A[n] ← K
i←0
while A[i] not equal to K do
i←i+1
ifi< n return i
else return −1
Exhaustive Search
Exhaustive search is simply a brute-force approach to
combinatorial problems.
Find the shortest tour through a given set of n nn cities that visits
each city exactly once before returning to the city where it started.
Assignment Problem