SlideShare a Scribd company logo
1 of 93
Unit II
Searching and Sorting Algorithms
Data Structures
Data Structure
ā€¢ is a storage that is used to store and organize data. It is a way of arranging data on
a computer so that it can be accessed and updated efficiently.
ā€¢ also used for processing, retrieving, and storing data
Algorithm
ā€¢ a step-by-step process to solve a particular problem or accomplish a particular
task
ā€¢ is correct if, for every input, it gives correct/expected output
ā€¢ can represent an algorithm in simple English like sentences, as a computer
program or as a flowchart
Factors of an algorithm
Algorithm and Program
Algorithm
ā€¢ comprises simple English like steps
usually written on paper before we
write the program
ā€¢ machine-independent
ā€¢ language independent
ā€¢ write the algorithm during the
design time
Program
ā€¢ conversion of these simple
English like steps into the
instruction set in a particular
programming language
ā€¢ machine-independent
ā€¢ language independent
ā€¢ write the program during the
implementation time
ā€¢ implementation of the algorithm
in a particular programming
language
Algorithm for sum of n
Int findSum(int n)
{
return n*(n+1)/2;
}
Int findSum(int n)
{
int sum = 0;
for int(i=1; i<=n; i++)
{
sum = sum+ i;
}
return sum;
}
Aspects of algorithm
ā€¢ Designing
ā€¢ Analyzing
Design an algorithm
Design an algorithm -
To find the factorial of a number
ā€¢ Step1: START
ā€¢ Step2: Declare integer variables num, factorial, i
ā€¢ Step3: Initialize the variables as factorialā†1 and iā†1
ā€¢ Step4: Input the value of num from the user
ā€¢ Step5: Repeat the following steps until i=n
5.1 factorial ā† factorial * i
5.2 i ā† i + 1
ā€¢ Step6: print factorial
ā€¢ Step7: END
Analysis of an algorithm
ā€¢ is an important part of computational complexity theory.
ā€¢ predict the behavior of an algorithm without implementing.
ā€¢ by analyzing different algorithms gives comparison and determines the best one
for the purpose.
ā€¢ provides theoretical estimation for the required resources of an algorithm to
solve a specific computational problem.
ā€¢ determination of the amount of time and space resources required to execute it.
Analysis of an algorithm
ā€¢ To measure the efficiency of an algorithm
ā€¢ For this, following criteriaā€™s are used to measure the efficiency of the algorithm
ā€¢ Running time of the algorithm
ā€¢ Space/memory occupied by the algorithm
ā€¢ analysis is only an approximation; it is not perfect
Efficiency of an algorithm
ā€¢ Performance: How much time/memory is used when a program is run. This
depends on the machine, compiler, etc. as well as the code written.
ā€¢ Complexity: How do the resource requirements of a program or algorithm scale,
i.e. what happens as the size of the problem being solved by the code gets larger.
Time complexity
ā€¢ Amount taken by an algorithm to run
ā€¢ Always depend upon input size
ā€¢ Time taken by an algorithm to process the input determines the time complexity
ā€¢ accurate running time is not done, rather, approximations are considered - this is
known as asymptotic analysis
Space complexity
ā€¢ Amount of memory or space taken by an algorithm to run
ā€¢ The memory required to process the input by an algorithm determines the space
complexity
ā€¢ Space / complexity can be computed through two different approach ā€“
i. Using iterative approach
ii. Using recursive approach
Iterative algorithm
ā€¢ Repeating a certain number of steps continuously until a particular condition is
met successfully.
ā€¢ The repeated execution of some groups of code statements in a program is
called iteration.
ā€¢ Iterations can be an infinite number of times.
ā€¢ Loops are tools provided by programming languages to implement iteration.
Iterative approach
ā€¢ for loop
ā€¢ while loop
Recursive algorithm
ā€¢ calls itself with smaller input values and returns the result for the current input by
carrying out basic operations on the returned value for the smaller input.
ā€¢ the given problem statement is separated into two parts. The first one is the base
case, and the second one is the recursive step
ā€¢ Base Case: is the simplest instance of a problem, consisting of a condition that
terminates the recursive function. This base case evaluates the result when a
given condition is met.
ā€¢ Recursive Step: computes the result by making recursive calls to the same
function, but with the inputs decreased in size or complexity.
Recursive approach
ā€¢ Print sum of n natural numbers using recursion
ā€¢ F(n) = 1 + 2 + 3 + 4 + ā€¦ā€¦ā€¦ā€¦..+ (n-2) + (n-1) + n
Recursive algorithm
ā€¢ While performing recursion, we call the function again and again, which increases
the memory used by the program.
ā€¢ stack as well comes along with it because recursion uses the stack for
implementation.
ā€¢ Due to the usage of the stack in recursion, this program has space complexity of
the order ā€˜nā€™ i.e. linear order which is higher than constant order.
Difference between Recursion and Iteration
Types of Algorithm Analysis
1.Best case - Define the input for which algorithm takes less time or minimum
time. calculate the lower bound of an algorithm.
2.Worst case - Define the input for which algorithm takes a long time or maximum
time. In the worst calculate the upper bound of an algorithm.
3.Average case - take all random inputs and calculate the computation time for all
inputs. And then divide it by the total number of inputs.
Asymptotic Analysis
ā€¢ Evaluates the performance of an algorithm in terms of input size , the actual
running time is not calculated but predicted.
ā€¢ The time (or space) taken by an algorithm increases with the input size is
calculated.
ā€¢ To have a measure of the efficiency of algorithms that donā€™t depend on machine-
specific constants and donā€™t require algorithms to be implemented and time
taken by programs to be compared.
ā€¢ Mathematical tool to describe the running time of an algorithm in its input size.
Asymptotic notations
1.Omega Notation (Ī©-notation)
2.Big-O Notation (O-notation)
3.Theta Notation (Ī˜-notation)
Asymptotic Analysis
ā€¢ The running time of one operation is computed as f(n).
ā€¢ And may be for another operation it is computed as g(n2).
ā€¢ This means the first operation running time will increase linearly with the
increase in n and the running time of the second operation will increase
exponentially when n increases.
ā€¢ Similarly, the running time of both operations will be nearly the same if n is
significantly small.
Omega (Ī©) Notation
ā€¢ represents the lower bound of the running time of an algorithm.
ā€¢ The best amount of (less) time an algorithm takes to complete
ā€¢ The algorithm takes more time to complete than this but not less than this
ā€¢ It provides the information of minimum resources need to run the algorithm.
Big O (O) notation
ā€¢ represents the upper bound of the running time of an algorithm.
ā€¢ The longest amount of time an algorithm takes to complete.
ā€¢ The algorithm takes less time to complete than this but not more than this.
ā€¢ Provides the worst case analysis of an algorithm.
Theta (ʟ ) notation
ā€¢ represents both the upper and lower bound of the running time of an algorithm.
ā€¢ Determines average amount of time an algorithm would take to complete.
ā€¢ https://www.youtube.com/watch?v=xFRea6xyPgM
ā€¢ https://freevideolectures.com/course/5003/nptel-design-analysis-
algorithms/8
Searching methods
ā€¢ is an operation or a technique that helps finds the place of a given element or
value in the list.
ā€¢ are designed to check for an element or retrieve an element from any data
structure where it is stored.
ā€¢ is said to be successful or unsuccessful depending upon whether the element
that is being searched is found or not.
ā€¢ standard techniques
ā€¢ Sequential Search
ā€¢ Interval Search
Linear Search
ā€¢ simplest method for searching.
ā€¢ defined as a sequential search algorithm.
ā€¢ 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.
Linear Search
ā€¢ Given an array arr[] of N elements, the task is to write a function to search a
given element x in arr[].
ā€¢ Iterate from 0 to N-1 and compare the value of every index with x if they match
return index.
Steps for Linear Search
ā€¢ Start from the leftmost element of arr[] and one by one compare x with each
element of arr[].
ā€¢ If x matches with an element, return the index.
ā€¢ If x doesnā€™t match with any of the elements, return -1.
Linear Search implementation in C
Binary search
ā€¢ execution needs to compare the element to be searched with the present
element at the center of the list.
ā€¢ If it matches, then the search is successful otherwise the list is divided into two
halves:
ā€¢ one from the 0th element to the middle element which is the center element (first
half).
ā€¢ another from the center element to the last element (which is the 2nd half) where
all values are greater than the center element.
ā€¢ proceeds from either of the two halves depending upon whether the target
element is greater or smaller than the central element.
ā€¢ If the element is smaller than the central element, then searching is done in the
first half, otherwise searching is done in the second half.
Binary search
ā€¢ is a very fast and efficient searching technique.
ā€¢ 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).
Binary search
ā€¢ Begin with the mid element of the whole array as a search key.
ā€¢ If the value of the search key is equal to the item then return an index of the
search key.
ā€¢ Or if the value of the search key is less than the item in the middle of the interval,
narrow the interval to the lower half.
ā€¢ Otherwise, narrow it to the upper half.
ā€¢ Repeatedly check from the second point until the value is found or the interval is
empty.
Step-by-step Binary Search Algorithm
ā€¢ Compare x with the middle element.
ā€¢ If x matches with the middle element, we return the mid index.
ā€¢ Else If x is greater than the mid element, then x can only lie in the right half
subarray after the mid element. So we recur for the right half.
ā€¢ Else (x is smaller) recur for the left half.
Binary Search implementation in C
Binary Search
Binary Search implementation
#include <stdio.h>
// A recursive binary search function. It returns location of x in given array arr[l..r] is present, otherwise -1
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
// If the element is present at the middle itself
if (arr[mid] == x)
return mid;
// If element is smaller than mid, then it can only be present in left subarray
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
// Else the element can only be present in right subarray
return binarySearch(arr, mid + 1, r, x);
}
// We reach here when element is not present in array
return -1;
}
Binary Search implementation
// A iterative binary search function. It returns location of x in given array arr[l..r] if present, otherwise -1
int binarySearch(int arr[], int l, int r, int x)
{
while (l <= r) {
int m = l + (r - l) / 2;
// Check if x is present at mid
if (arr[m] == x)
return m;
// If x greater, ignore left half
if (arr[m] < x)
l = m + 1;
// If x is smaller, ignore right half
else
r = m - 1;
}
// if we reach here, then element was not present
return -1;
}
Example of Binary Search
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 10;
int result = binarySearch(arr, 0, n - 1, x);
(result == -1)
? printf("Element is not present in array")
: printf("Element is present at index %d", result);
return 0;
}
Linear Search and Binary Search
Linear Search and Binary Search
Time and Space complexity in Linear Search algorithm
ā€¢ The time complexity of linear search algorithm is divided into three main cases.
ā€¢ In the best-case scenario, the linear search algorithm performs O(1) operations.
ā€¢ In the worst-case scenario, the linear search algorithm performs O(n) operations.
ā€¢ When the element to be searched is in the middle of the array, the average case of the Linear
Search Algorithm is O(n).
ā€¢ The linear search algorithm takes up no extra space; its space complexity is O(n) for an array of n
elements.
Time and Space complexity of Binary Search algorithm
ā€¢ The time complexity of the binary search algorithm is O(log n).
ā€¢ The best-case time complexity would be O(1) when the central index would directly match the
desired value.
ā€¢ The worse case time complexity of the binary search T(n)= T(n/2)+1 is known as the recurrence
relation for binary search.
ā€¢ The space complexity of the binary search algorithm depends on the implementation of the
algorithm.
ā€¢ In the iterative method, the space complexity would be O(1).
ā€¢ While in the recursive method, the space complexity would be O(log n).
Fibonacci search algorithm
ā€¢ involves the Fibonacci Numbers.
ā€¢ just like the Binary search method,
ā€¢ a comparison-based searching algorithm
ā€¢ based on the divide and conquer technique.
ā€¢ works on an array that is sorted in the non-decreasing order.
Fibonacci Numbers
ā€¢ a sequence of numbers where any term is equal to the sum of the previous two
terms i.e.,
ā€¢ play a very important role in the information coding theory.
ā€¢ are heavily used in various security coding algorithms.
Fibonacci search algorithm
ā€¢ Find a Fibonacci number that is greater than or equal to the size of the given array in which key is
to be searched .
ā€¢ If the size of the array is n then we must find a Fibonacci number Fk such that Fk >=n .
ā€¢ Compute Fk-1, Fk-2, the of Fset , and the index value. The index is computed using the of Fset, n , and Fk-2.
ā€¢ Compare the key with the element at index in the array. The outcomes will be:
ā€¢ If the key and array element at index are equal then the key is at position index in the given
array, then return it and stop.
ā€¢ If the key is less than the array element at index , then search the key in the left sub-tree to Fk-2 .
ā€¢ If the given key is greater than the array element at index, then search the right sub-tree to Fk-2 .
ā€¢ If the key is not found, repeat above steps as long as Fk-2 >=0. The Fibonacci number is greater
than the length of the array .
After each iteration the size of array n is reduced either by 2/3 or by 1/3 of the array.
Fibonacci search algorithm - Example
10, 23, 34,45,67,89,97,101,110,145, 156, 169 find key 110.
Fibonacci search algorithm - Example
10, 23, 34,45,67,89,97,101,110,145, 156, 169 find key 110.
ā€¢ n=12, of Fset = -1
Fibonacci search algorithm - Example
10, 23, 34,45,67,89,97,101,110,145, 156, 169 find key 110.
ā€¢ n=12, of Fset = -1
ā€¢ Iteration 1-
ā€¢ Smallest fibonacci number >=12 is 13
Fibonacci search algorithm - Example
10, 23, 34,45,67,89,97,101,110,145, 156, 169 find key 110.
ā€¢ n=12, of Fset = -1
ā€¢ Iteration 1-
ā€¢ Smallest fibonacci number >=12 is 13
ā€¢ Fk =13, Fk-1 = 8, Fk-2 = 5
Fibonacci search algorithm - Example
10, 23, 34,45,67,89,97,101,110,145, 156, 169 find key 110.
ā€¢ n=12, of Fset = -1
ā€¢ Iteration 1-
ā€¢ Smallest fibonacci number >=12 is 13
ā€¢ Fk =13, Fk-1 = 8, Fk-2 = 5
ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((-1+5),11)=4
Fibonacci search algorithm - Example
10, 23, 34,45,67,89,97,101,110,145, 156, 169 find key 110.
ā€¢ n=12, of Fset = -1
ā€¢ Iteration 1-
ā€¢ Smallest fibonacci number >=12 is 13
ā€¢ Fk =13, Fk-1 = 8, Fk-2 = 5
ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((-1+5),11)=4
ā€¢ The element at index 4 in array is 67. Since 110> 47, we move one Fibonacci number down.
Fibonacci search algorithm - Example
10, 23, 34,45,67,89,97,101,110,145, 156, 169 find key 110.
ā€¢ n=12, of Fset = -1
ā€¢ Iteration 1-
ā€¢ Smallest fibonacci number >=12 is 13
ā€¢ Fk =13, Fk-1 = 8, Fk-2 = 5
ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((-1+5),11)=4
ā€¢ The element at index 4 in array is 67. Since 110> 47, we move one Fibonacci number down.
ā€¢ Iteration 2-
ā€¢
Fibonacci search algorithm - Example
10, 23, 34,45,67,89,97,101,110,145, 156, 169 find key 110.
ā€¢ n=12, of Fset = -1
ā€¢ Iteration 1-
ā€¢ Smallest fibonacci number >=12 is 13
ā€¢ Fk =13, Fk-1 = 8, Fk-2 = 5
ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((-1+5),11)=4
ā€¢ The element at index 4 in array is 67. Since 110> 47, we move one Fibonacci number down.
ā€¢ Iteration 2-
ā€¢ Fk =8, Fk-1 = 5, Fk-2 = 3
Fibonacci search algorithm - Example
10, 23, 34,45,67,89,97,101,110,145, 156, 169 find key 110.
ā€¢ n=12, of Fset = -1
ā€¢ Iteration 1-
ā€¢ Smallest fibonacci number >=12 is 13
ā€¢ Fk =13, Fk-1 = 8, Fk-2 = 5
ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((-1+5),11)=4
ā€¢ The element at index 4 in array is 67. Since 110> 47, we move one Fibonacci number down.
ā€¢ Iteration 2-
ā€¢ Fk =8, Fk-1 = 5, Fk-2 = 3
ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((4+3),11)=7
Fibonacci search algorithm - Example
10, 23, 34,45,67,89,97,101,110,145, 156, 169 find key 110.
ā€¢ n=12, of Fset = -1
ā€¢ Iteration 1-
ā€¢ Smallest fibonacci number >=12 is 13
ā€¢ Fk =13, Fk-1 = 8, Fk-2 = 5
ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((-1+5),11)=4
ā€¢ The element at index 4 in array is 67. Since 110> 47, we move one Fibonacci number down.
ā€¢ Iteration 2-
ā€¢ Fk =8, Fk-1 = 5, Fk-2 = 3
ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((4+3),11)=7
ā€¢ The element at index 7 in array is 97. Since 110> 97, we move one Fibonacci number down.
Fibonacci search algorithm - Example
10, 23, 34,45,67,89,97,101,110,145, 156, 169 find key 110.
ā€¢ n=12, of Fset = -1
ā€¢ Iteration 1-
ā€¢ Smallest fibonacci number >=12 is 13
ā€¢ Fk =13, Fk-1 = 8, Fk-2 = 5
ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((-1+5),11)=4
ā€¢ The element at index 4 in array is 67. Since 110> 47, we move one Fibonacci number down.
ā€¢ Iteration 2-
ā€¢ Fk =8, Fk-1 = 5, Fk-2 = 3
ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((4+3),11)=7
ā€¢ The element at index 7 in array is 97. Since 110> 97, we move one Fibonacci number down.
ā€¢ Iteration 3-
ā€¢
Fibonacci search algorithm - Example
10, 23, 34,45,67,89,97,101,110,145, 156, 169 find key 110.
ā€¢ n=12, of Fset = -1
ā€¢ Iteration 1-
ā€¢ Smallest fibonacci number >=12 is 13
ā€¢ Fk =13, Fk-1 = 8, Fk-2 = 5
ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((-1+5),11)=4
ā€¢ The element at index 4 in array is 67. Since 110> 47, we move one Fibonacci number down.
ā€¢ Iteration 2-
ā€¢ Fk =8, Fk-1 = 5, Fk-2 = 3
ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((4+3),11)=7
ā€¢ The element at index 7 in array is 97. Since 110> 97, we move one Fibonacci number down.
ā€¢ Iteration 3-
ā€¢ Fk =5, Fk-1 = 3 , Fk-2 = 2
Fibonacci search algorithm - Example
10, 23, 34,45,67,89,97,101,110,145, 156, 169 find key 110.
ā€¢ n=12, of Fset = -1
ā€¢ Iteration 1-
ā€¢ Smallest fibonacci number >=12 is 13
ā€¢ Fk =13, Fk-1 = 8, Fk-2 = 5
ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((-1+5),11)=4
ā€¢ The element at index 4 in array is 67. Since 110> 47, we move one Fibonacci number down.
ā€¢ Iteration 2-
ā€¢ Fk =8, Fk-1 = 5, Fk-2 = 3
ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((4+3),11)=7
ā€¢ The element at index 7 in array is 97. Since 110> 97, we move one Fibonacci number down.
ā€¢ Iteration 3-
ā€¢ Fk =5, Fk-1 = 3 , Fk-2 = 2
ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((7+2),11)=9
Fibonacci search algorithm - Example
10, 23, 34,45,67,89,97,101,110,145, 156, 169 find key 110.
ā€¢ n=12, of Fset = -1
ā€¢ Iteration 1-
ā€¢ Smallest fibonacci number >=12 is 13
ā€¢ Fk =13, Fk-1 = 8, Fk-2 = 5
ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((-1+5),11)=4
ā€¢ The element at index 4 in array is 67. Since 110> 47, we move one Fibonacci number down.
ā€¢ Iteration 2-
ā€¢ Fk =8, Fk-1 = 5, Fk-2 = 3
ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((4+3),11)=7
ā€¢ The element at index 7 in array is 97. Since 110> 97, we move one Fibonacci number down.
ā€¢ Iteration 3-
ā€¢ Fk =5, Fk-1 = 3 , Fk-2 = 2
ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((7+2),11)=9
ā€¢ The element at index 9 in array is 110, return index and stop
Space and Time Complexity of Fibonacci Search
ā€¢ The Fibonacci Search scheme shrinks the starting search space by
either two-thirds or one-third in every iteration depending upon
whether the key is smaller than Fk-1 or is greater than it.
ā€¢ On average, the search space is reduced by 1/3, so n/3k =1 or n=3k
ā€¢ k=log(n)
Space and Time Complexity of Fibonacci Search
ā€¢ Best Case: Time complexity of the the Fibonacci search is O(1). Weā€™ll
find this case when the search key is the first element we start
comparing.
ā€¢ Worst Case: for the Fibonacci search method occurs when the key is
always present in the larger subarray. Its worst-case time complexity
is O(log n).
ā€¢ Average Case: shrink the search space by 1/3, so the average case
time complexity of the Fibonacci search algorithm is O(log n).
ā€¢ Fibonacci search has O(1) space complexity because no extra space
other than temporary variables is used.
Sorting methods
ā€¢ Sorting - A method to rearrange a given array or list of elements in
according to a comparison operator on the elements.
ā€¢ An in-place sorting algorithm uses constant space for producing the
output.
ā€¢ It sorts the list only by modifying the order of the elements within the
list.
Sorting
ā€¢ Types of Sorting
ā€¢ Internal Sorting - all data is placed in the main
memory or internal memory
ā€¢ External Sorting - all data that needs to be sorted cannot be
placed in memory at a time
ā€¢ Sort Stability
ā€¢ Stable Sort - two same data appear in the same order in sorted
data
ā€¢ Unstable Sort - two same data appear in the different order in
sorted data
Bubble Sort
ā€¢ one of the simplest algorithms.
ā€¢ repeatedly iterates through the list.
ā€¢ compares the elements next to each other.
ā€¢ swaps according to the condition passed to them.
Bubble Sort
// An optimized version of Bubble Sort
void bubbleSort(int arr[], int n)
{
int i, j;
bool swapped;
for (i = 0; i < n-1; i++)
{
swapped = false;
for (j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
swap(&arr[j], &arr[j+1]);
swapped = true;
}
}
// IF no two elements were swapped by inner loop, then
break
if (swapped == false)
break;
}
}
/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("n");
}
// Driver program to test above functions
int main()
{
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: n");
printArray(arr, n);
return 0;
}
Bubble Sort
Bubble Sort
ā€¢ This algorithm is not suitable for large data sets as its average and
worst-case time complexity is quite high.
ā€¢ Time Complexity: O(N2)
ā€¢ Auxiliary Space: O(1)
Insertion Sort
ā€¢ a simple sorting algorithm.
ā€¢ an in-place comparison-based sorting algorithm.
ā€¢ a sub-list is maintained which is always sorted.
ā€¢ an element which is to be 'insert'ed in this sorted sub-list, has to find
its appropriate place and then it has to be inserted there.
ā€¢ the array is searched sequentially.
ā€¢ unsorted items are moved and inserted into the sorted sub-list (in the
same array).
Insertion Sort
Insertion Sort
ā€¢ not suitable for large data sets as its average and worst case
complexity are of ĪŸ(n2), where n is the number of items.
ā€¢ Auxiliary Space: O(1)
Selection sort algorithm
ā€¢ a simple sorting algorithm.
ā€¢ an in-place comparison-based algorithm in which the list is divided
into two parts,
ā€¢ the sorted part at the left end.
ā€¢ the unsorted part at the right end.
ā€¢ Initially, the sorted part is empty and the unsorted part is the entire
list.
ā€¢ The smallest element is selected from the unsorted array and
swapped with the leftmost element, and that element becomes a
part of the sorted array.
ā€¢ This process continues moving unsorted array boundary by one
element to the right.
Selection sort algorithm
Selection sort algorithm
ā€¢ This algorithm is not suitable for large data sets.
ā€¢ Its average and worst case complexities are of ĪŸ(n2), where n is the
number of items.
Insertion sort
Selection sort
Merge sort
ā€¢ a sorting algorithm that is based on the Divide and
Conquer paradigm.
ā€¢ the array is initially divided into two equal halves and then they are
combined in a sorted manner.
ā€¢ a recursive algorithm continuously splits the array in half until it
cannot be further divided.
Merge Sort algorithm
Merge Sort algorithm
ā€¢ with worst-case time complexity being ĪŸ(n log n), it is one of the
most respected algorithms.
ā€¢ requires an additional memory space of 0(n) for the temporary array.
Quick sort
ā€¢ a highly efficient sorting algorithm.
ā€¢ based on partitioning of array of data into smaller arrays.
ā€¢ A large array is partitioned into two arrays one of which holds values
smaller than the specified value, say pivot, based on which the
partition is made and another array holds values greater than the
pivot value.
Quick sort
ā€¢ This algorithm is quite efficient for large-sized data sets as its average
and worst-case complexity are O(n2), respectively.
ā€¢ has a space complexity of O(logn).
Pseudo code
ā€¢ Pseudocode is a way of expressing an algorithm without conforming
to specific syntax rules.
ā€¢ No broad standard for pseudocode syntax exists.
ā€¢ Pseudocode resembles skeleton programs, which can be compiled
without errors.
Bubble sort
Insertion sort
Selection sort
Merge sort
Quick sort
Comparison of Sorting algorithms
Thank you

More Related Content

Similar to Unit II_Searching and Sorting Algorithms.ppt

Slides [DAA] Unit 2 Ch 2.pdf
Slides [DAA] Unit 2 Ch 2.pdfSlides [DAA] Unit 2 Ch 2.pdf
Slides [DAA] Unit 2 Ch 2.pdfVijayraj799513
Ā 
Introduction to Data Structures Sorting and searching
Introduction to Data Structures Sorting and searchingIntroduction to Data Structures Sorting and searching
Introduction to Data Structures Sorting and searchingMvenkatarao
Ā 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdfNayanChandak1
Ā 
Lecture 2 role of algorithms in computing
Lecture 2   role of algorithms in computingLecture 2   role of algorithms in computing
Lecture 2 role of algorithms in computingjayavignesh86
Ā 
Intro to Data Structure & Algorithms
Intro to Data Structure & AlgorithmsIntro to Data Structure & Algorithms
Intro to Data Structure & AlgorithmsAkhil Kaushik
Ā 
Chap5 slides
Chap5 slidesChap5 slides
Chap5 slidesBaliThorat1
Ā 
jn;lm;lkm';m';;lmppt of data structure.pdf
jn;lm;lkm';m';;lmppt of data structure.pdfjn;lm;lkm';m';;lmppt of data structure.pdf
jn;lm;lkm';m';;lmppt of data structure.pdfVinayNassa3
Ā 
RAJAT PROJECT.pptx
RAJAT PROJECT.pptxRAJAT PROJECT.pptx
RAJAT PROJECT.pptxSayedMohdAsim2
Ā 
Analysis and Algorithms: basic Introduction
Analysis and Algorithms: basic IntroductionAnalysis and Algorithms: basic Introduction
Analysis and Algorithms: basic Introductionssuseraf8b2f
Ā 
Data structures and algorithms Module-1.pdf
Data structures and algorithms Module-1.pdfData structures and algorithms Module-1.pdf
Data structures and algorithms Module-1.pdfDukeCalvin
Ā 
Analysis of Algorithm full version 2024.pptx
Analysis of Algorithm  full version  2024.pptxAnalysis of Algorithm  full version  2024.pptx
Analysis of Algorithm full version 2024.pptxrajesshs31r
Ā 
Design Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptxDesign Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptxrajesshs31r
Ā 
Algorithm and C code related to data structure
Algorithm and C code related to data structureAlgorithm and C code related to data structure
Algorithm and C code related to data structureSelf-Employed
Ā 
Analysis and Design of Algorithms
Analysis and Design of AlgorithmsAnalysis and Design of Algorithms
Analysis and Design of AlgorithmsBulbul Agrawal
Ā 

Similar to Unit II_Searching and Sorting Algorithms.ppt (20)

Slides [DAA] Unit 2 Ch 2.pdf
Slides [DAA] Unit 2 Ch 2.pdfSlides [DAA] Unit 2 Ch 2.pdf
Slides [DAA] Unit 2 Ch 2.pdf
Ā 
Algorithm.pptx
Algorithm.pptxAlgorithm.pptx
Algorithm.pptx
Ā 
Algorithm.pptx
Algorithm.pptxAlgorithm.pptx
Algorithm.pptx
Ā 
Python algorithm
Python algorithmPython algorithm
Python algorithm
Ā 
Cs 331 Data Structures
Cs 331 Data StructuresCs 331 Data Structures
Cs 331 Data Structures
Ā 
Introduction to Data Structures Sorting and searching
Introduction to Data Structures Sorting and searchingIntroduction to Data Structures Sorting and searching
Introduction to Data Structures Sorting and searching
Ā 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdf
Ā 
Lecture 2 role of algorithms in computing
Lecture 2   role of algorithms in computingLecture 2   role of algorithms in computing
Lecture 2 role of algorithms in computing
Ā 
Intro to Data Structure & Algorithms
Intro to Data Structure & AlgorithmsIntro to Data Structure & Algorithms
Intro to Data Structure & Algorithms
Ā 
Chap5 slides
Chap5 slidesChap5 slides
Chap5 slides
Ā 
Iare ds ppt_3
Iare ds ppt_3Iare ds ppt_3
Iare ds ppt_3
Ā 
jn;lm;lkm';m';;lmppt of data structure.pdf
jn;lm;lkm';m';;lmppt of data structure.pdfjn;lm;lkm';m';;lmppt of data structure.pdf
jn;lm;lkm';m';;lmppt of data structure.pdf
Ā 
RAJAT PROJECT.pptx
RAJAT PROJECT.pptxRAJAT PROJECT.pptx
RAJAT PROJECT.pptx
Ā 
Analysis and Algorithms: basic Introduction
Analysis and Algorithms: basic IntroductionAnalysis and Algorithms: basic Introduction
Analysis and Algorithms: basic Introduction
Ā 
Data structures and algorithms Module-1.pdf
Data structures and algorithms Module-1.pdfData structures and algorithms Module-1.pdf
Data structures and algorithms Module-1.pdf
Ā 
U nit i data structure-converted
U nit   i data structure-convertedU nit   i data structure-converted
U nit i data structure-converted
Ā 
Analysis of Algorithm full version 2024.pptx
Analysis of Algorithm  full version  2024.pptxAnalysis of Algorithm  full version  2024.pptx
Analysis of Algorithm full version 2024.pptx
Ā 
Design Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptxDesign Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptx
Ā 
Algorithm and C code related to data structure
Algorithm and C code related to data structureAlgorithm and C code related to data structure
Algorithm and C code related to data structure
Ā 
Analysis and Design of Algorithms
Analysis and Design of AlgorithmsAnalysis and Design of Algorithms
Analysis and Design of Algorithms
Ā 

More from HODElex

Unit 4_Network Layer_Part II.pptx
Unit 4_Network Layer_Part II.pptxUnit 4_Network Layer_Part II.pptx
Unit 4_Network Layer_Part II.pptxHODElex
Ā 
Unit VI - Graphs.ppt
Unit VI - Graphs.pptUnit VI - Graphs.ppt
Unit VI - Graphs.pptHODElex
Ā 
unit II_2_i.pptx
unit II_2_i.pptxunit II_2_i.pptx
unit II_2_i.pptxHODElex
Ā 
IEEE and Lower Level LAN Protocols.ppt
IEEE and Lower Level LAN Protocols.pptIEEE and Lower Level LAN Protocols.ppt
IEEE and Lower Level LAN Protocols.pptHODElex
Ā 
I & E Tutorials.pdf
I & E Tutorials.pdfI & E Tutorials.pdf
I & E Tutorials.pdfHODElex
Ā 
wireless lan presentation.ppt
wireless lan presentation.pptwireless lan presentation.ppt
wireless lan presentation.pptHODElex
Ā 
Unit 3_Network Layer_Part II.pptx
Unit 3_Network Layer_Part II.pptxUnit 3_Network Layer_Part II.pptx
Unit 3_Network Layer_Part II.pptxHODElex
Ā 

More from HODElex (7)

Unit 4_Network Layer_Part II.pptx
Unit 4_Network Layer_Part II.pptxUnit 4_Network Layer_Part II.pptx
Unit 4_Network Layer_Part II.pptx
Ā 
Unit VI - Graphs.ppt
Unit VI - Graphs.pptUnit VI - Graphs.ppt
Unit VI - Graphs.ppt
Ā 
unit II_2_i.pptx
unit II_2_i.pptxunit II_2_i.pptx
unit II_2_i.pptx
Ā 
IEEE and Lower Level LAN Protocols.ppt
IEEE and Lower Level LAN Protocols.pptIEEE and Lower Level LAN Protocols.ppt
IEEE and Lower Level LAN Protocols.ppt
Ā 
I & E Tutorials.pdf
I & E Tutorials.pdfI & E Tutorials.pdf
I & E Tutorials.pdf
Ā 
wireless lan presentation.ppt
wireless lan presentation.pptwireless lan presentation.ppt
wireless lan presentation.ppt
Ā 
Unit 3_Network Layer_Part II.pptx
Unit 3_Network Layer_Part II.pptxUnit 3_Network Layer_Part II.pptx
Unit 3_Network Layer_Part II.pptx
Ā 

Recently uploaded

Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
Ā 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
Ā 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
Ā 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
Ā 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
Ā 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEslot gacor bisa pakai pulsa
Ā 
Model Call Girl in Narela Delhi reach out to us at šŸ”8264348440šŸ”
Model Call Girl in Narela Delhi reach out to us at šŸ”8264348440šŸ”Model Call Girl in Narela Delhi reach out to us at šŸ”8264348440šŸ”
Model Call Girl in Narela Delhi reach out to us at šŸ”8264348440šŸ”soniya singh
Ā 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
Ā 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
Ā 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
Ā 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
Ā 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
Ā 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
Ā 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
Ā 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
Ā 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
Ā 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoĆ£o Esperancinha
Ā 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
Ā 

Recently uploaded (20)

Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
Ā 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
Ā 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
Ā 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Ā 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
Ā 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
Ā 
Model Call Girl in Narela Delhi reach out to us at šŸ”8264348440šŸ”
Model Call Girl in Narela Delhi reach out to us at šŸ”8264348440šŸ”Model Call Girl in Narela Delhi reach out to us at šŸ”8264348440šŸ”
Model Call Girl in Narela Delhi reach out to us at šŸ”8264348440šŸ”
Ā 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
Ā 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
Ā 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
Ā 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
Ā 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
Ā 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
Ā 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Ā 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
Ā 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
Ā 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
Ā 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Ā 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
Ā 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
Ā 

Unit II_Searching and Sorting Algorithms.ppt

  • 1. Unit II Searching and Sorting Algorithms Data Structures
  • 2. Data Structure ā€¢ is a storage that is used to store and organize data. It is a way of arranging data on a computer so that it can be accessed and updated efficiently. ā€¢ also used for processing, retrieving, and storing data
  • 3. Algorithm ā€¢ a step-by-step process to solve a particular problem or accomplish a particular task ā€¢ is correct if, for every input, it gives correct/expected output ā€¢ can represent an algorithm in simple English like sentences, as a computer program or as a flowchart
  • 4. Factors of an algorithm
  • 5. Algorithm and Program Algorithm ā€¢ comprises simple English like steps usually written on paper before we write the program ā€¢ machine-independent ā€¢ language independent ā€¢ write the algorithm during the design time Program ā€¢ conversion of these simple English like steps into the instruction set in a particular programming language ā€¢ machine-independent ā€¢ language independent ā€¢ write the program during the implementation time ā€¢ implementation of the algorithm in a particular programming language
  • 6. Algorithm for sum of n Int findSum(int n) { return n*(n+1)/2; } Int findSum(int n) { int sum = 0; for int(i=1; i<=n; i++) { sum = sum+ i; } return sum; }
  • 7. Aspects of algorithm ā€¢ Designing ā€¢ Analyzing
  • 9. Design an algorithm - To find the factorial of a number ā€¢ Step1: START ā€¢ Step2: Declare integer variables num, factorial, i ā€¢ Step3: Initialize the variables as factorialā†1 and iā†1 ā€¢ Step4: Input the value of num from the user ā€¢ Step5: Repeat the following steps until i=n 5.1 factorial ā† factorial * i 5.2 i ā† i + 1 ā€¢ Step6: print factorial ā€¢ Step7: END
  • 10. Analysis of an algorithm ā€¢ is an important part of computational complexity theory. ā€¢ predict the behavior of an algorithm without implementing. ā€¢ by analyzing different algorithms gives comparison and determines the best one for the purpose. ā€¢ provides theoretical estimation for the required resources of an algorithm to solve a specific computational problem. ā€¢ determination of the amount of time and space resources required to execute it.
  • 11. Analysis of an algorithm ā€¢ To measure the efficiency of an algorithm ā€¢ For this, following criteriaā€™s are used to measure the efficiency of the algorithm ā€¢ Running time of the algorithm ā€¢ Space/memory occupied by the algorithm ā€¢ analysis is only an approximation; it is not perfect
  • 12. Efficiency of an algorithm ā€¢ Performance: How much time/memory is used when a program is run. This depends on the machine, compiler, etc. as well as the code written. ā€¢ Complexity: How do the resource requirements of a program or algorithm scale, i.e. what happens as the size of the problem being solved by the code gets larger.
  • 13. Time complexity ā€¢ Amount taken by an algorithm to run ā€¢ Always depend upon input size ā€¢ Time taken by an algorithm to process the input determines the time complexity ā€¢ accurate running time is not done, rather, approximations are considered - this is known as asymptotic analysis
  • 14. Space complexity ā€¢ Amount of memory or space taken by an algorithm to run ā€¢ The memory required to process the input by an algorithm determines the space complexity ā€¢ Space / complexity can be computed through two different approach ā€“ i. Using iterative approach ii. Using recursive approach
  • 15. Iterative algorithm ā€¢ Repeating a certain number of steps continuously until a particular condition is met successfully. ā€¢ The repeated execution of some groups of code statements in a program is called iteration. ā€¢ Iterations can be an infinite number of times. ā€¢ Loops are tools provided by programming languages to implement iteration.
  • 16. Iterative approach ā€¢ for loop ā€¢ while loop
  • 17. Recursive algorithm ā€¢ calls itself with smaller input values and returns the result for the current input by carrying out basic operations on the returned value for the smaller input. ā€¢ the given problem statement is separated into two parts. The first one is the base case, and the second one is the recursive step ā€¢ Base Case: is the simplest instance of a problem, consisting of a condition that terminates the recursive function. This base case evaluates the result when a given condition is met. ā€¢ Recursive Step: computes the result by making recursive calls to the same function, but with the inputs decreased in size or complexity.
  • 18. Recursive approach ā€¢ Print sum of n natural numbers using recursion ā€¢ F(n) = 1 + 2 + 3 + 4 + ā€¦ā€¦ā€¦ā€¦..+ (n-2) + (n-1) + n
  • 19. Recursive algorithm ā€¢ While performing recursion, we call the function again and again, which increases the memory used by the program. ā€¢ stack as well comes along with it because recursion uses the stack for implementation. ā€¢ Due to the usage of the stack in recursion, this program has space complexity of the order ā€˜nā€™ i.e. linear order which is higher than constant order.
  • 21. Types of Algorithm Analysis 1.Best case - Define the input for which algorithm takes less time or minimum time. calculate the lower bound of an algorithm. 2.Worst case - Define the input for which algorithm takes a long time or maximum time. In the worst calculate the upper bound of an algorithm. 3.Average case - take all random inputs and calculate the computation time for all inputs. And then divide it by the total number of inputs.
  • 22. Asymptotic Analysis ā€¢ Evaluates the performance of an algorithm in terms of input size , the actual running time is not calculated but predicted. ā€¢ The time (or space) taken by an algorithm increases with the input size is calculated. ā€¢ To have a measure of the efficiency of algorithms that donā€™t depend on machine- specific constants and donā€™t require algorithms to be implemented and time taken by programs to be compared. ā€¢ Mathematical tool to describe the running time of an algorithm in its input size.
  • 23. Asymptotic notations 1.Omega Notation (Ī©-notation) 2.Big-O Notation (O-notation) 3.Theta Notation (Ī˜-notation)
  • 24. Asymptotic Analysis ā€¢ The running time of one operation is computed as f(n). ā€¢ And may be for another operation it is computed as g(n2). ā€¢ This means the first operation running time will increase linearly with the increase in n and the running time of the second operation will increase exponentially when n increases. ā€¢ Similarly, the running time of both operations will be nearly the same if n is significantly small.
  • 25. Omega (Ī©) Notation ā€¢ represents the lower bound of the running time of an algorithm. ā€¢ The best amount of (less) time an algorithm takes to complete ā€¢ The algorithm takes more time to complete than this but not less than this ā€¢ It provides the information of minimum resources need to run the algorithm.
  • 26. Big O (O) notation ā€¢ represents the upper bound of the running time of an algorithm. ā€¢ The longest amount of time an algorithm takes to complete. ā€¢ The algorithm takes less time to complete than this but not more than this. ā€¢ Provides the worst case analysis of an algorithm.
  • 27. Theta (ʟ ) notation ā€¢ represents both the upper and lower bound of the running time of an algorithm. ā€¢ Determines average amount of time an algorithm would take to complete.
  • 29. Searching methods ā€¢ is an operation or a technique that helps finds the place of a given element or value in the list. ā€¢ are designed to check for an element or retrieve an element from any data structure where it is stored. ā€¢ is said to be successful or unsuccessful depending upon whether the element that is being searched is found or not. ā€¢ standard techniques ā€¢ Sequential Search ā€¢ Interval Search
  • 30. Linear Search ā€¢ simplest method for searching. ā€¢ defined as a sequential search algorithm. ā€¢ 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.
  • 31. Linear Search ā€¢ Given an array arr[] of N elements, the task is to write a function to search a given element x in arr[]. ā€¢ Iterate from 0 to N-1 and compare the value of every index with x if they match return index.
  • 32. Steps for Linear Search ā€¢ Start from the leftmost element of arr[] and one by one compare x with each element of arr[]. ā€¢ If x matches with an element, return the index. ā€¢ If x doesnā€™t match with any of the elements, return -1.
  • 34. Binary search ā€¢ execution needs to compare the element to be searched with the present element at the center of the list. ā€¢ If it matches, then the search is successful otherwise the list is divided into two halves: ā€¢ one from the 0th element to the middle element which is the center element (first half). ā€¢ another from the center element to the last element (which is the 2nd half) where all values are greater than the center element. ā€¢ proceeds from either of the two halves depending upon whether the target element is greater or smaller than the central element. ā€¢ If the element is smaller than the central element, then searching is done in the first half, otherwise searching is done in the second half.
  • 35. Binary search ā€¢ is a very fast and efficient searching technique. ā€¢ 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).
  • 36. Binary search ā€¢ Begin with the mid element of the whole array as a search key. ā€¢ If the value of the search key is equal to the item then return an index of the search key. ā€¢ Or if the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. ā€¢ Otherwise, narrow it to the upper half. ā€¢ Repeatedly check from the second point until the value is found or the interval is empty.
  • 37. Step-by-step Binary Search Algorithm ā€¢ Compare x with the middle element. ā€¢ If x matches with the middle element, we return the mid index. ā€¢ Else If x is greater than the mid element, then x can only lie in the right half subarray after the mid element. So we recur for the right half. ā€¢ Else (x is smaller) recur for the left half.
  • 40. Binary Search implementation #include <stdio.h> // A recursive binary search function. It returns location of x in given array arr[l..r] is present, otherwise -1 int binarySearch(int arr[], int l, int r, int x) { if (r >= l) { int mid = l + (r - l) / 2; // If the element is present at the middle itself if (arr[mid] == x) return mid; // If element is smaller than mid, then it can only be present in left subarray if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x); // Else the element can only be present in right subarray return binarySearch(arr, mid + 1, r, x); } // We reach here when element is not present in array return -1; }
  • 41. Binary Search implementation // A iterative binary search function. It returns location of x in given array arr[l..r] if present, otherwise -1 int binarySearch(int arr[], int l, int r, int x) { while (l <= r) { int m = l + (r - l) / 2; // Check if x is present at mid if (arr[m] == x) return m; // If x greater, ignore left half if (arr[m] < x) l = m + 1; // If x is smaller, ignore right half else r = m - 1; } // if we reach here, then element was not present return -1; }
  • 42. Example of Binary Search int main(void) { int arr[] = { 2, 3, 4, 10, 40 }; int n = sizeof(arr) / sizeof(arr[0]); int x = 10; int result = binarySearch(arr, 0, n - 1, x); (result == -1) ? printf("Element is not present in array") : printf("Element is present at index %d", result); return 0; }
  • 43. Linear Search and Binary Search
  • 44. Linear Search and Binary Search
  • 45. Time and Space complexity in Linear Search algorithm ā€¢ The time complexity of linear search algorithm is divided into three main cases. ā€¢ In the best-case scenario, the linear search algorithm performs O(1) operations. ā€¢ In the worst-case scenario, the linear search algorithm performs O(n) operations. ā€¢ When the element to be searched is in the middle of the array, the average case of the Linear Search Algorithm is O(n). ā€¢ The linear search algorithm takes up no extra space; its space complexity is O(n) for an array of n elements.
  • 46. Time and Space complexity of Binary Search algorithm ā€¢ The time complexity of the binary search algorithm is O(log n). ā€¢ The best-case time complexity would be O(1) when the central index would directly match the desired value. ā€¢ The worse case time complexity of the binary search T(n)= T(n/2)+1 is known as the recurrence relation for binary search. ā€¢ The space complexity of the binary search algorithm depends on the implementation of the algorithm. ā€¢ In the iterative method, the space complexity would be O(1). ā€¢ While in the recursive method, the space complexity would be O(log n).
  • 47. Fibonacci search algorithm ā€¢ involves the Fibonacci Numbers. ā€¢ just like the Binary search method, ā€¢ a comparison-based searching algorithm ā€¢ based on the divide and conquer technique. ā€¢ works on an array that is sorted in the non-decreasing order.
  • 48. Fibonacci Numbers ā€¢ a sequence of numbers where any term is equal to the sum of the previous two terms i.e., ā€¢ play a very important role in the information coding theory. ā€¢ are heavily used in various security coding algorithms.
  • 49. Fibonacci search algorithm ā€¢ Find a Fibonacci number that is greater than or equal to the size of the given array in which key is to be searched . ā€¢ If the size of the array is n then we must find a Fibonacci number Fk such that Fk >=n . ā€¢ Compute Fk-1, Fk-2, the of Fset , and the index value. The index is computed using the of Fset, n , and Fk-2. ā€¢ Compare the key with the element at index in the array. The outcomes will be: ā€¢ If the key and array element at index are equal then the key is at position index in the given array, then return it and stop. ā€¢ If the key is less than the array element at index , then search the key in the left sub-tree to Fk-2 . ā€¢ If the given key is greater than the array element at index, then search the right sub-tree to Fk-2 . ā€¢ If the key is not found, repeat above steps as long as Fk-2 >=0. The Fibonacci number is greater than the length of the array . After each iteration the size of array n is reduced either by 2/3 or by 1/3 of the array.
  • 50. Fibonacci search algorithm - Example 10, 23, 34,45,67,89,97,101,110,145, 156, 169 find key 110.
  • 51. Fibonacci search algorithm - Example 10, 23, 34,45,67,89,97,101,110,145, 156, 169 find key 110. ā€¢ n=12, of Fset = -1
  • 52. Fibonacci search algorithm - Example 10, 23, 34,45,67,89,97,101,110,145, 156, 169 find key 110. ā€¢ n=12, of Fset = -1 ā€¢ Iteration 1- ā€¢ Smallest fibonacci number >=12 is 13
  • 53. Fibonacci search algorithm - Example 10, 23, 34,45,67,89,97,101,110,145, 156, 169 find key 110. ā€¢ n=12, of Fset = -1 ā€¢ Iteration 1- ā€¢ Smallest fibonacci number >=12 is 13 ā€¢ Fk =13, Fk-1 = 8, Fk-2 = 5
  • 54. Fibonacci search algorithm - Example 10, 23, 34,45,67,89,97,101,110,145, 156, 169 find key 110. ā€¢ n=12, of Fset = -1 ā€¢ Iteration 1- ā€¢ Smallest fibonacci number >=12 is 13 ā€¢ Fk =13, Fk-1 = 8, Fk-2 = 5 ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((-1+5),11)=4
  • 55. Fibonacci search algorithm - Example 10, 23, 34,45,67,89,97,101,110,145, 156, 169 find key 110. ā€¢ n=12, of Fset = -1 ā€¢ Iteration 1- ā€¢ Smallest fibonacci number >=12 is 13 ā€¢ Fk =13, Fk-1 = 8, Fk-2 = 5 ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((-1+5),11)=4 ā€¢ The element at index 4 in array is 67. Since 110> 47, we move one Fibonacci number down.
  • 56. Fibonacci search algorithm - Example 10, 23, 34,45,67,89,97,101,110,145, 156, 169 find key 110. ā€¢ n=12, of Fset = -1 ā€¢ Iteration 1- ā€¢ Smallest fibonacci number >=12 is 13 ā€¢ Fk =13, Fk-1 = 8, Fk-2 = 5 ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((-1+5),11)=4 ā€¢ The element at index 4 in array is 67. Since 110> 47, we move one Fibonacci number down. ā€¢ Iteration 2- ā€¢
  • 57. Fibonacci search algorithm - Example 10, 23, 34,45,67,89,97,101,110,145, 156, 169 find key 110. ā€¢ n=12, of Fset = -1 ā€¢ Iteration 1- ā€¢ Smallest fibonacci number >=12 is 13 ā€¢ Fk =13, Fk-1 = 8, Fk-2 = 5 ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((-1+5),11)=4 ā€¢ The element at index 4 in array is 67. Since 110> 47, we move one Fibonacci number down. ā€¢ Iteration 2- ā€¢ Fk =8, Fk-1 = 5, Fk-2 = 3
  • 58. Fibonacci search algorithm - Example 10, 23, 34,45,67,89,97,101,110,145, 156, 169 find key 110. ā€¢ n=12, of Fset = -1 ā€¢ Iteration 1- ā€¢ Smallest fibonacci number >=12 is 13 ā€¢ Fk =13, Fk-1 = 8, Fk-2 = 5 ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((-1+5),11)=4 ā€¢ The element at index 4 in array is 67. Since 110> 47, we move one Fibonacci number down. ā€¢ Iteration 2- ā€¢ Fk =8, Fk-1 = 5, Fk-2 = 3 ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((4+3),11)=7
  • 59. Fibonacci search algorithm - Example 10, 23, 34,45,67,89,97,101,110,145, 156, 169 find key 110. ā€¢ n=12, of Fset = -1 ā€¢ Iteration 1- ā€¢ Smallest fibonacci number >=12 is 13 ā€¢ Fk =13, Fk-1 = 8, Fk-2 = 5 ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((-1+5),11)=4 ā€¢ The element at index 4 in array is 67. Since 110> 47, we move one Fibonacci number down. ā€¢ Iteration 2- ā€¢ Fk =8, Fk-1 = 5, Fk-2 = 3 ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((4+3),11)=7 ā€¢ The element at index 7 in array is 97. Since 110> 97, we move one Fibonacci number down.
  • 60. Fibonacci search algorithm - Example 10, 23, 34,45,67,89,97,101,110,145, 156, 169 find key 110. ā€¢ n=12, of Fset = -1 ā€¢ Iteration 1- ā€¢ Smallest fibonacci number >=12 is 13 ā€¢ Fk =13, Fk-1 = 8, Fk-2 = 5 ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((-1+5),11)=4 ā€¢ The element at index 4 in array is 67. Since 110> 47, we move one Fibonacci number down. ā€¢ Iteration 2- ā€¢ Fk =8, Fk-1 = 5, Fk-2 = 3 ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((4+3),11)=7 ā€¢ The element at index 7 in array is 97. Since 110> 97, we move one Fibonacci number down. ā€¢ Iteration 3- ā€¢
  • 61. Fibonacci search algorithm - Example 10, 23, 34,45,67,89,97,101,110,145, 156, 169 find key 110. ā€¢ n=12, of Fset = -1 ā€¢ Iteration 1- ā€¢ Smallest fibonacci number >=12 is 13 ā€¢ Fk =13, Fk-1 = 8, Fk-2 = 5 ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((-1+5),11)=4 ā€¢ The element at index 4 in array is 67. Since 110> 47, we move one Fibonacci number down. ā€¢ Iteration 2- ā€¢ Fk =8, Fk-1 = 5, Fk-2 = 3 ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((4+3),11)=7 ā€¢ The element at index 7 in array is 97. Since 110> 97, we move one Fibonacci number down. ā€¢ Iteration 3- ā€¢ Fk =5, Fk-1 = 3 , Fk-2 = 2
  • 62. Fibonacci search algorithm - Example 10, 23, 34,45,67,89,97,101,110,145, 156, 169 find key 110. ā€¢ n=12, of Fset = -1 ā€¢ Iteration 1- ā€¢ Smallest fibonacci number >=12 is 13 ā€¢ Fk =13, Fk-1 = 8, Fk-2 = 5 ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((-1+5),11)=4 ā€¢ The element at index 4 in array is 67. Since 110> 47, we move one Fibonacci number down. ā€¢ Iteration 2- ā€¢ Fk =8, Fk-1 = 5, Fk-2 = 3 ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((4+3),11)=7 ā€¢ The element at index 7 in array is 97. Since 110> 97, we move one Fibonacci number down. ā€¢ Iteration 3- ā€¢ Fk =5, Fk-1 = 3 , Fk-2 = 2 ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((7+2),11)=9
  • 63. Fibonacci search algorithm - Example 10, 23, 34,45,67,89,97,101,110,145, 156, 169 find key 110. ā€¢ n=12, of Fset = -1 ā€¢ Iteration 1- ā€¢ Smallest fibonacci number >=12 is 13 ā€¢ Fk =13, Fk-1 = 8, Fk-2 = 5 ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((-1+5),11)=4 ā€¢ The element at index 4 in array is 67. Since 110> 47, we move one Fibonacci number down. ā€¢ Iteration 2- ā€¢ Fk =8, Fk-1 = 5, Fk-2 = 3 ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((4+3),11)=7 ā€¢ The element at index 7 in array is 97. Since 110> 97, we move one Fibonacci number down. ā€¢ Iteration 3- ā€¢ Fk =5, Fk-1 = 3 , Fk-2 = 2 ā€¢ Index = min((of Fset + Fk-2 ),n-1) = min((7+2),11)=9 ā€¢ The element at index 9 in array is 110, return index and stop
  • 64.
  • 65. Space and Time Complexity of Fibonacci Search ā€¢ The Fibonacci Search scheme shrinks the starting search space by either two-thirds or one-third in every iteration depending upon whether the key is smaller than Fk-1 or is greater than it. ā€¢ On average, the search space is reduced by 1/3, so n/3k =1 or n=3k ā€¢ k=log(n)
  • 66. Space and Time Complexity of Fibonacci Search ā€¢ Best Case: Time complexity of the the Fibonacci search is O(1). Weā€™ll find this case when the search key is the first element we start comparing. ā€¢ Worst Case: for the Fibonacci search method occurs when the key is always present in the larger subarray. Its worst-case time complexity is O(log n). ā€¢ Average Case: shrink the search space by 1/3, so the average case time complexity of the Fibonacci search algorithm is O(log n). ā€¢ Fibonacci search has O(1) space complexity because no extra space other than temporary variables is used.
  • 67. Sorting methods ā€¢ Sorting - A method to rearrange a given array or list of elements in according to a comparison operator on the elements. ā€¢ An in-place sorting algorithm uses constant space for producing the output. ā€¢ It sorts the list only by modifying the order of the elements within the list.
  • 68. Sorting ā€¢ Types of Sorting ā€¢ Internal Sorting - all data is placed in the main memory or internal memory ā€¢ External Sorting - all data that needs to be sorted cannot be placed in memory at a time ā€¢ Sort Stability ā€¢ Stable Sort - two same data appear in the same order in sorted data ā€¢ Unstable Sort - two same data appear in the different order in sorted data
  • 69. Bubble Sort ā€¢ one of the simplest algorithms. ā€¢ repeatedly iterates through the list. ā€¢ compares the elements next to each other. ā€¢ swaps according to the condition passed to them.
  • 70. Bubble Sort // An optimized version of Bubble Sort void bubbleSort(int arr[], int n) { int i, j; bool swapped; for (i = 0; i < n-1; i++) { swapped = false; for (j = 0; j < n-i-1; j++) { if (arr[j] > arr[j+1]) { swap(&arr[j], &arr[j+1]); swapped = true; } } // IF no two elements were swapped by inner loop, then break if (swapped == false) break; } } /* Function to print an array */ void printArray(int arr[], int size) { int i; for (i=0; i < size; i++) printf("%d ", arr[i]); printf("n"); } // Driver program to test above functions int main() { int arr[] = {64, 34, 25, 12, 22, 11, 90}; int n = sizeof(arr)/sizeof(arr[0]); bubbleSort(arr, n); printf("Sorted array: n"); printArray(arr, n); return 0; }
  • 72. Bubble Sort ā€¢ This algorithm is not suitable for large data sets as its average and worst-case time complexity is quite high. ā€¢ Time Complexity: O(N2) ā€¢ Auxiliary Space: O(1)
  • 73. Insertion Sort ā€¢ a simple sorting algorithm. ā€¢ an in-place comparison-based sorting algorithm. ā€¢ a sub-list is maintained which is always sorted. ā€¢ an element which is to be 'insert'ed in this sorted sub-list, has to find its appropriate place and then it has to be inserted there. ā€¢ the array is searched sequentially. ā€¢ unsorted items are moved and inserted into the sorted sub-list (in the same array).
  • 75. Insertion Sort ā€¢ not suitable for large data sets as its average and worst case complexity are of ĪŸ(n2), where n is the number of items. ā€¢ Auxiliary Space: O(1)
  • 76. Selection sort algorithm ā€¢ a simple sorting algorithm. ā€¢ an in-place comparison-based algorithm in which the list is divided into two parts, ā€¢ the sorted part at the left end. ā€¢ the unsorted part at the right end. ā€¢ Initially, the sorted part is empty and the unsorted part is the entire list. ā€¢ The smallest element is selected from the unsorted array and swapped with the leftmost element, and that element becomes a part of the sorted array. ā€¢ This process continues moving unsorted array boundary by one element to the right.
  • 78. Selection sort algorithm ā€¢ This algorithm is not suitable for large data sets. ā€¢ Its average and worst case complexities are of ĪŸ(n2), where n is the number of items.
  • 81. Merge sort ā€¢ a sorting algorithm that is based on the Divide and Conquer paradigm. ā€¢ the array is initially divided into two equal halves and then they are combined in a sorted manner. ā€¢ a recursive algorithm continuously splits the array in half until it cannot be further divided.
  • 83. Merge Sort algorithm ā€¢ with worst-case time complexity being ĪŸ(n log n), it is one of the most respected algorithms. ā€¢ requires an additional memory space of 0(n) for the temporary array.
  • 84. Quick sort ā€¢ a highly efficient sorting algorithm. ā€¢ based on partitioning of array of data into smaller arrays. ā€¢ A large array is partitioned into two arrays one of which holds values smaller than the specified value, say pivot, based on which the partition is made and another array holds values greater than the pivot value.
  • 85. Quick sort ā€¢ This algorithm is quite efficient for large-sized data sets as its average and worst-case complexity are O(n2), respectively. ā€¢ has a space complexity of O(logn).
  • 86. Pseudo code ā€¢ Pseudocode is a way of expressing an algorithm without conforming to specific syntax rules. ā€¢ No broad standard for pseudocode syntax exists. ā€¢ Pseudocode resembles skeleton programs, which can be compiled without errors.
  • 92. Comparison of Sorting algorithms