Algorithms
Algorithms
PROPERTIES OF ALGORITHMS
There are several properties that algorithms generally share. They are useful to keep in mind
when algorithms are described. These properties are:
i. Input. An algorithm has input values from a specified set.
ii. 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.
iii. Definiteness. The steps of an algorithm must be defined precisely.
iv. Correctness. An algorithm should produce the correct output values for each set of input
values.
v. Finiteness. An algorithm should produce the desired output after a finite (but perhaps
large) number of steps for any input in the set.
vi. Effectiveness. It must be possible to perform each step of an algorithm exactly and in a
finite amount of time.
vii. Generality. The procedure should be applicable for all problems of the desired form, not
just for a particular set of input values.
Searching Algorithms
Linear Search
The first algorithm that we will present is called the linear search, or sequential search.
Linear Search is defined as a sequential search algorithm that starts at one end and goes through
each element of a list until the desired element is found, otherwise the search continues till the
end of the data set.
Algorithm
Linear Search ( Array A, Value x)
Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit
Pseudocode
Binary Search
Binary Search is defined as a searching algorithm used in a sorted array by repeatedly
dividing the search interval in half.
The idea of binary search is to use the information that the array is sorted and reduce the
time complexity to O(log N).
Pseudocode
The pseudocode of binary search algorithms should look like this –
Procedure binary_search
A ← sorted array
n ← size of array
x ← value to be searched
Set lowerBound = 1
Set upperBound = n
while x not found
if upperBound < lowerBound
EXIT: x does not exists.
set midPoint = lowerBound + ( upperBound - lowerBound ) / 2
if A[midPoint] < x
set lowerBound = midPoint + 1
if A[midPoint] > x
set upperBound = midPoint - 1
if A[midPoint] = x
EXIT: x found at location midPoint
end while
end procedure
Sorting Algorithm
Bubble Sort
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent
elements if they are in the wrong order.
• traverse from left and compare adjacent elements and the higher one is placed at right
side.
• In this way, the largest element is moved to the rightmost end at first.
• This process is then continued to find the second largest and place it and so on until the
data is sorted.
Algorithm
begin BubbleSort(list)
Euclidean Algorithm
The Euclidean Algorithm is a technique for quickly finding the GCD of two integers.
Algorithm
The Euclidean Algorithm for finding GCD(A,B) is as follows:
Step 1: Let a, b be the two numbers
Step 2: a mod b = R
Step 3: Let a = b and b = R
Step 4: Repeat Steps 2 and 3 until a mod b is greater than 0
Step 5: GCD = b
Step 6: Finish
Exercise:
Find the greatest common divisor of 414 and 662 using the Euclidean algorithm.
Let,
A = 662, B = 414, Q = A\B, R = A%B
Q A B R
1 662 414 248
1 414 248 166
1 248 166 82
2 166 82 2
41 82 2 0
2 0
GCD(662,414) = 2