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

Algorithms

An algorithm is a precise sequence of instructions to perform a computation or solve a problem. The document discusses several types of algorithms including linear search, binary search, bubble sort, and the Euclidean algorithm. Linear search sequentially checks each element until the target is found, having time complexity of O(N). Binary search repeatedly halves the search space, having time complexity of O(log N) for sorted data. Bubble sort iterates through adjacent elements and swaps them if out of order, having time complexity of O(N^2). The Euclidean algorithm efficiently finds the greatest common divisor of two numbers by repeatedly dividing the larger by the remainder.

Uploaded by

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

Algorithms

An algorithm is a precise sequence of instructions to perform a computation or solve a problem. The document discusses several types of algorithms including linear search, binary search, bubble sort, and the Euclidean algorithm. Linear search sequentially checks each element until the target is found, having time complexity of O(N). Binary search repeatedly halves the search space, having time complexity of O(log N) for sorted data. Bubble sort iterates through adjacent elements and swaps them if out of order, having time complexity of O(N^2). The Euclidean algorithm efficiently finds the greatest common divisor of two numbers by repeatedly dividing the larger by the remainder.

Uploaded by

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

Algorithms

An algorithm is a finite sequence of precise instructions for performing a computation or for


solving a problem.
ALGORITHM 1 Finding the Maximum Element in a Finite Sequence.
procedure max(a1, a2,...,an: integers)
max := a1
for i := 2 to n
if max < ai then max := ai
return max{max is the largest element}

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.

How Does Linear Search Algorithm Work?


In Linear Search Algorithm,
i. Every element is considered as a potential match for the key and checked for the
same.
ii. If any element is found equal to the key, the search is successful and the index of that
element is returned.
iii. If no element is found equal to the key, the search yields “No match found”.

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

procedure linear_search (list, value)

for each item in the list


if match item == value
return the item's location
end if
end for
end procedure

Complexity Analysis of Linear Search:


Time Complexity:
Best Case: In the best case, the key might be present at the first index. So the best case
complexity is O(1)
Worst Case: In the worst case, the key might be present at the last index i.e., opposite to
the end from which the search has started in the list. So the worst-case complexity is O(N)
where N is the size of the list.
Average Case: O(N)

Advantages of Linear Search:


i. Linear search can be used irrespective of whether the array is sorted or
not. It can be used on arrays of any data type.
ii. Does not require any additional memory.
iii. It is a well-suited algorithm for small datasets.

Drawbacks of Linear Search:


Linear search has a time complexity of O(N), which in turn makes it slow for large datasets.
Not suitable for large arrays.

When to use Linear Search?


When we are dealing with a small dataset.
When you are searching for a dataset stored in contiguous memory.

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).

Conditions for when to apply Binary Search in a Data Structure:


To apply Binary Search algorithm:
i. The data structure must be sorted.
ii. Access to any element of the data structure takes constant time.
Algorithm
Binary_Search(a, lower_bound, upper_bound, val)
Step 1: set beg = lower_bound, end = upper_bound, pos = - 1
Step 2: repeat steps 3 and 4 while beg <=end
Step 3: set mid = (beg + end)/2
Step 4: if a[mid] = val
set pos = mid
print pos
go to step 6
else if a[mid] > val
set end = mid - 1
else
set beg = mid + 1
[end of if]
[end of loop]
Step 5: if pos = -1
print "value is not present in the array"
[end of if]
Step 6: exit

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

Complexity Analysis of Binary Search:


Time Complexity:
Best Case: O(1)
Average Case: O(log N)
Worst Case: O(log N)

Advantages of Binary Search:


i. Binary search is faster than linear search, especially for large arrays.
ii. More efficient than other searching algorithms with a similar time complexity, such
as interpolation search or exponential search.
iii. Binary search is well-suited for searching large datasets that are stored in external
memory, such as on a hard drive or in the cloud.

Drawbacks of Binary Search:


i. The array should be sorted.
ii. Binary search requires that the data structure being searched be stored in
contiguous memory locations.
iii. Binary search requires that the elements of the array be comparable, meaning that
they must be able to be ordered.
Applications of Binary Search:
i. Binary search can be used as a building block for more complex algorithms used
in machine learning, such as algorithms for training neural networks or finding the
optimal hyperparameters for a model.
ii. It can be used for searching in computer graphics such as algorithms for ray tracing
or texture mapping.
iii. It can be used for searching a database.

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.

Bubble Sort Algorithm


In this algorithm,

• 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)

for all elements of list


if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for
return list
end BubbleSort
Complexity Analysis of Bubble Sort:
Time Complexity:
Best Case: O(n)
Average Case: O(n2)
Worst Case: O(n2)

Advantages of Bubble Sort:


i. Bubble sort is easy to understand and implement.
ii. It does not require any additional memory space.
iii. It is a stable sorting algorithm, meaning that elements with the same key value
maintain their relative order in the sorted output.
Disadvantages of Bubble Sort:
i. Bubble sort has a time complexity of O(N2) which makes it very slow for large data
sets.
ii. Bubble sort is a comparison-based sorting algorithm, which means that it requires
a comparison operator to determine the relative order of elements in the input data
set. It can limit the efficiency of the algorithm in certain cases.

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

You might also like