What Worst Case Analysuis
What Worst Case Analysuis
90% Refund @Courses DSA Data Structure Analysis of Algorithms Sorting Searching Greedy Recurs
1. Big-O Notation
2. Omega Notation
3. Theta Notation
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Got It !
Policy
https://www.geeksforgeeks.org/worst-average-and-best-case-analysis-of-algorithms/?ref=lbp 1/20
13/02/2024, 07:27 Worst, Average and Best Case Analysis of Algorithms - GeeksforGeeks
We useIncookies
the best-case
to ensure youanalysis,
have the bestwe calculate
browsing theonlower
experience bound
our website. on the running time
By using
our site,
of you
an acknowledge
algorithm.that youmust
We have read and understood
know the case ourthat
Cookie Policy & Privacy
causes a minimum number of
Policy
https://www.geeksforgeeks.org/worst-average-and-best-case-analysis-of-algorithms/?ref=lbp 2/20
13/02/2024, 07:27 Worst, Average and Best Case Analysis of Algorithms - GeeksforGeeks
operations to be executed. In the linear search problem, the best case occurs
when x is present at the first location. The number of operations in the best
case is constant (not dependent on n). So time complexity in the best case
would be ?(1)
In average case analysis, we take all possible inputs and calculate the
computing time for all of the inputs. Sum all the calculated values and divide
the sum by the total number of inputs. We must know (or predict) the
distribution of cases. For the linear search problem, let us assume that all
cases are uniformly distributed (including the case of x not being present in
the array). So we sum all the cases and divide the sum by (n+1). Following is
the value of average-case time complexity.
The average case analysis is not easy to do in most practical cases and it is
rarely done. In the average case analysis, we must know (or predict) the
mathematical distribution of all possible inputs.
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
3. Best Case Analysis Policy
https://www.geeksforgeeks.org/worst-average-and-best-case-analysis-of-algorithms/?ref=lbp 3/20
13/02/2024, 07:27 Worst, Average and Best Case Analysis of Algorithms - GeeksforGeeks
A) For some algorithms, all the cases (worst, best, average) are
asymptotically the same. i.e., there are no worst and best cases.
B) Where as most of the other sorting algorithms have worst and best
cases.
C++
return i;
}
return -1;
}
// Driver's Code
int main()
{
int arr[] = { 1, 10, 30, 15 };
int x = 30;
int n = sizeof(arr) / sizeof(arr[0]);
// Function call
cout << x << " is present at index "
<< search(arr, n, x);
return 0;
}
/* Driver's code*/
int main()
{
int arr[] = { 1, 10, 30, 15 };
int x = 30;
int n = sizeof(arr) / sizeof(arr[0]);
// Function call
printf("%d is present at index %d", x,
search(arr, n, x));
We use cookies to ensure you have the best browsing experience on our website. By using
getchar();
our site, youreturn
acknowledge
0; that you have read and understood our Cookie Policy & Privacy
} Policy
https://www.geeksforgeeks.org/worst-average-and-best-case-analysis-of-algorithms/?ref=lbp 5/20
13/02/2024, 07:27 Worst, Average and Best Case Analysis of Algorithms - GeeksforGeeks
Java
/* Driver's code*/
public static void main(String[] args)
{
int arr[] = { 1, 10, 30, 15 };
int x = 30;
int n = arr.length;
// Function call
System.out.printf("%d is present at index %d", x,
search(arr, n, x));
}
}
Python3
We use cookies to ensure you have the best browsing experience on our website. By using
our site,
# you acknowledge
Driver's Codethat you have read and understood our Cookie Policy & Privacy
if __name__ == '__main__': Policy
https://www.geeksforgeeks.org/worst-average-and-best-case-analysis-of-algorithms/?ref=lbp 6/20
13/02/2024, 07:27 Worst, Average and Best Case Analysis of Algorithms - GeeksforGeeks
# Function call
print(x, "is present at index",
search(arr, x))
C#
/* Driver's code*/
public static void Main()
{
int[] arr = { 1, 10, 30, 15 };
int x = 30;
int n = arr.Length;
// Function call
Console.WriteLine(x + " is present at index "
+ search(arr, n, x));
}
}
Javascript
https://www.geeksforgeeks.org/worst-average-and-best-case-analysis-of-algorithms/?ref=lbp 7/20
13/02/2024, 07:27 Worst, Average and Best Case Analysis of Algorithms - GeeksforGeeks
return i;
}
}
return -1;
}
PHP
<?php
// PHP implementation of the approach
// Driver's Code
$arr = array(1, 10, 30, 15);
$x = 30;
$n = sizeof($arr);
// Function call
echo $x . " is present at index ".
search($arr, $n, $x);
Output
30 is present at index 2
We use cookiesComplexity
Time to ensure you have the best browsing
Analysis: experience
(In Big-O on our website. By using
notation)
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy
https://www.geeksforgeeks.org/worst-average-and-best-case-analysis-of-algorithms/?ref=lbp 8/20
13/02/2024, 07:27 Worst, Average and Best Case Analysis of Algorithms - GeeksforGeeks
Best Case: O(1), This will take place if the element to be searched is on
the first index of the given list. So, the number of comparisons, in this
case, is 1.
Average Case: O(n), This will take place if the element to be searched is
on the middle index of the given list.
Worst Case: O(n), This will take place if:
The element to be searched is on the last index
The element to be searched is not present on the list
2. In this example, we will take an array of length (n) and deals with the
following cases :
C++
// Driver's Code
int main()
{
// Declaring two array one of length odd and other of
// length even;
int arr[4] = { 1, 2, 3, 4 };
int a[5] = { 1, 2, 3, 4, 5 };
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you//
acknowledge
Function thatcall
you have read and understood our Cookie Policy & Privacy
cout << getSum(arr, Policy 4)
https://www.geeksforgeeks.org/worst-average-and-best-case-analysis-of-algorithms/?ref=lbp 9/20
13/02/2024, 07:27 Worst, Average and Best Case Analysis of Algorithms - GeeksforGeeks
Java
/* Driver's code*/
public static void main(String[] args)
{
int arr1[]
= { 1, 2, 3,
4 }; // Declaring an array of even length
int n1 = arr1.length;
int arr2[]
= { 1, 2, 3, 4,
5 }; // Declaring an array of odd length
int n2 = arr2.length;
// Function call
System.out.println(getsum(
arr1, n1)); // print 0 because (n) is even
System.out.println(getsum(
arr2,
n2)); // print sum of array because (n) is odd
}
} // This code is contributed by Syed Maruf Ali (Sdmrf)
We usePython3
cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
# Python 3 implementation Policy
of the approach
https://www.geeksforgeeks.org/worst-average-and-best-case-analysis-of-algorithms/?ref=lbp 10/20
13/02/2024, 07:27 Worst, Average and Best Case Analysis of Algorithms - GeeksforGeeks
Sum = 0
for i in range(n):
Sum += arr[i]
return Sum # if (n) is odd
# Driver's Code
if __name__ == '__main__':
arr1 = [1, 2, 3, 4] # Declaring an array of even length
n1 = len(arr1)
arr2 = [1, 2, 3, 4, 5] # Declaring an array of odd length
n2 = len(arr2)
# Function call
print(getsum(arr1, n1)) # print 0 because (n) is even
C#
using System;
// Driver's Code
public static void Main(string[] args)
{
We use cookies
// toDeclaring
ensure you have
twothearray
best browsing
one experience
of length on our
oddwebsite.
and By using of
other
our site, you//
acknowledge that
length even; you have read and understood our Cookie Policy & Privacy
int[] arr = { 1, 2, Policy 3, 4 };
https://www.geeksforgeeks.org/worst-average-and-best-case-analysis-of-algorithms/?ref=lbp 11/20
13/02/2024, 07:27 Worst, Average and Best Case Analysis of Algorithms - GeeksforGeeks
int[] a = { 1, 2, 3, 4, 5 };
// Function call
Console.Write(getSum(arr, 4)+"\n"); // print 0 because (n) is even
Console.Write(getSum(a, 5)); // print sum because (n) is odd
}
}
Javascript
// Driver's Code
// Function call
console.log(getSum(arr, 4)); //print 0 because (n) is even
console.log(getSum(a, 5)); // print sum because (n) is odd
PHP
<?php
// PHP implementation of the approach
// Driver's Code
$arr1 = [1, 2, 3, 4]; // Declaring an array of even length
$n1 = count($arr1);
$arr2 = [1, 2, 3, 4, 5]; // Declaring an array of odd length
$n2 = count($arr2);
// Function calls
echo getSum($arr1, $n1) . "\n"; // print 0 because (n) is even
echo getSum($arr2, $n2) . "\n"; // print sum of array because (n) is od
?>
Output
0
15
Best Case: The order of growth will be constant because in the best case
we are assuming that (n) is even.
Average Case: In this case, we will assume that even and odd are equally
likely, therefore Order of growth will be linear
Worst Case: The order of growth will be linear because in this case, we
are assuming that (n) is always odd.
Advantages:
We use cookies to ensure you have the best browsing experience on our website. By using
1.you
our site, This technique
acknowledge allows
that you developers
have read to our
and understood understand
Cookie Policy the performance of
& Privacy
algorithms under different Policy scenarios, which can help in making informed
https://www.geeksforgeeks.org/worst-average-and-best-case-analysis-of-algorithms/?ref=lbp 13/20
13/02/2024, 07:27 Worst, Average and Best Case Analysis of Algorithms - GeeksforGeeks
Disadvantages:
Important points:
Reference books:
https://www.geeksforgeeks.org/worst-average-and-best-case-analysis-of-algorithms/?ref=lbp 14/20
13/02/2024, 07:27 Worst, Average and Best Case Analysis of Algorithms - GeeksforGeeks
Feeling lost in the world of random DSA topics, wasting time without
progress? It's time for a change! Join our DSA course, where we'll guide you
on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course,
trusted by over 100,000 geeks!
DSA in C++
DSA in Java
DSA in Python
DSA in JavaScript
Recommended Problems
Solve Problems
Frequently asked DSA Problems
Three 90 Challenge ending on 29th Feb! Last chance to get 90% refund by
completing 90% course in 90 days. Explore offer now.
Previous Next
We use cookies
Share to ensure
your you have the best
thoughts browsing
in the experience on our website. By using Add Your Comment
comments
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy
https://www.geeksforgeeks.org/worst-average-and-best-case-analysis-of-algorithms/?ref=lbp 15/20
13/02/2024, 07:27 Worst, Average and Best Case Analysis of Algorithms - GeeksforGeeks
Similar Reads
Asymptotic Notation and Analysis
(Based on input size) in Complexity Analysis of Algorithms | Big-O analysis
Analysis of Algorithms
RFM Analysis Analysis Using Python Time and Space Complexity Analysis of
Tree Traversal Algorithms
Complete Tutorials
Learn Algorithms with Javascript | DSA DSA Crash Course | Revision Checklist
using JavaScript Tutorial with Interview Guide
GeeksforGeeks
Additional
We use Information
cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy
https://www.geeksforgeeks.org/worst-average-and-best-case-analysis-of-algorithms/?ref=lbp 16/20
13/02/2024, 07:27 Worst, Average and Best Case Analysis of Algorithms - GeeksforGeeks
Company Explore
About Us Job-A-Thon Hiring Challenge
Legal Hack-A-Thon
Careers GfG Weekly Contest
In Media Offline Classes (Delhi/NCR)
Contact Us DSA in JAVA/C++
Advertise with us Master System Design
GFG Corporate Solution Master CP
Placement Training Program GeeksforGeeks Videos
Apply for Mentor Geeks Community
Languages DSA
Python Data Structures
Java Algorithms
C++ DSA for Beginners
PHP Basic DSA Problems
GoLang DSA Roadmap
We use cookies to ensure you have the best browsing experience on our website. By using
SQL Top 100 DSA Interview Problems
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
R Language Policy DSA Roadmap by Sandeep Jain
https://www.geeksforgeeks.org/worst-average-and-best-case-analysis-of-algorithms/?ref=lbp 17/20
13/02/2024, 07:27 Worst, Average and Best Case Analysis of Algorithms - GeeksforGeeks
https://www.geeksforgeeks.org/worst-average-and-best-case-analysis-of-algorithms/?ref=lbp 18/20
13/02/2024, 07:27 Worst, Average and Best Case Analysis of Algorithms - GeeksforGeeks
https://www.geeksforgeeks.org/worst-average-and-best-case-analysis-of-algorithms/?ref=lbp 19/20
13/02/2024, 07:27 Worst, Average and Best Case Analysis of Algorithms - GeeksforGeeks
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy
https://www.geeksforgeeks.org/worst-average-and-best-case-analysis-of-algorithms/?ref=lbp 20/20