Lab-1 Manual Upload
Lab-1 Manual Upload
LAB MANUAL 01
CSC2211 Algorithms
Fall
2024-2025
TITLE
PREREQUISITE
OBJECTIVE
THEORY
Algorithm
An algorithm is a step by step method of solving a problem. It is commonly used for data
processing, calculation and other related computer and mathematical operations. An algorithm
is also used to manipulate data in various ways, such as inserting a new data item, searching for
a particular item or sorting an item.
Technically, computers use algorithms to list the detailed instructions for carrying out an
operation. For example, to compute an employee’s paycheck, the computer uses an algorithm.
To accomplish this task, appropriate data must be entered into the system. In terms of
efficiency, various algorithms are able to accomplish operations or problem solving easily and
quickly.
In an algorithm, each instruction is identified and the order in which they should be carried out
is planned. Algorithms are often used as a starting point for creating a computer program, and
they are sometimes written as a flowchart or in pseudocode.
Searching Algorithms
Searching Algorithms are designed to check for an element or retrieve an element from any
data structure where it is stored. Based on the type of search operation, these algorithms are
generally classified into two categories:
Sequential Search: In this, the list or array is traversed sequentially and every element is
checked. For example: Linear Search.
Interval Search: These algorithms are specifically designed for searching in sorted data-
structures. This type of searching algorithms are much more efficient than Linear Search as they
repeatedly target the center of the search structure and divide the search space in half. For
Example: Binary Search.
Linear Search
A linear search scans one item at a time, without jumping to any item.
The worst case complexity is O(n), sometimes known an O(n) search
Time taken to search elements keeps increasing as the numbers of elements are
increased.
Example:
Input: arr[] = {10, 20, 80, 30, 60, 50,110, 100, 130, 170}
x = 110;
Output: 6
Element x is present at index 6
Input: arr[] = {10, 20, 80, 30, 60, 50,110, 100, 130, 170}
x = 175;
Output: -1
Element x is not present in arr[].
Example:
Input: 7, 6, 1, 5, 3, 4, 9, 5
Output: 1, 3, 4, 5, 5, 6, 7, 9
Bubble Sort:
Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based algorithm in which
each pair of adjacent elements is compared and the elements are swapped if they are not in order. This
algorithm is not suitable for large data sets as its average and worst case complexity are of Ο(n2) where
n is the number of items.
return list
end BubbleSort
Selection Sort
The Selection sort algorithm is based on the idea of finding the minimum or maximum element in an
unsorted array and then putting it in its correct position in a sorted array.
SELECTION-SORT(A)
for j ← 1 to n-1
smallest ← j
for i ← j + 1 to n
if A[ i ] < A[ smallest ]
smallest ← i
Exchange A[ j ] ↔ A[ smallest ]
Insertion Sort
Insertion sort is based on the idea that one element from the input elements is consumed in each
iteration to find its correct position i.e, the position to which it belongs in a sorted array.
It iterates the input elements by growing the sorted array at each iteration. It compares the current
element with the largest value in the sorted array. If the current element is greater, then it leaves the
element in its place and moves on to the next element else it finds its correct position in the sorted
array and moves it to that position. This is done by shifting all the elements, which are larger than the
current element, in the sorted array to one position ahead.
INSERTION-SORT(A)
for j = 2 to n
key ← A [j]
j←i–1
A[i+1] ← A[i]
i←i–1
A[j+1] ← key
Counting sort is a sorting technique based on keys between a specific range. It works by
counting the number of objects having distinct key values (kind of hashing). Then doing some
arithmetic to calculate the position of each object in the output sequence.
2) Modify the count array such that each element at each index
stores the sum of previous counts.
Index: 0 1 2 3 4 5 6 7 8 9
Count: 0 2 4 4 5 6 6 7 7 7
CountingSort(A)
for i = 0 to k do
c[i] = 0
for j = 0 to n do
c[A[j]] = c[A[j]] + 1
for i = 1 to k do
c[i] = c[i] + c[i-1]