Algorithm worst, average and best case (worst, Average and good Cases)-------Geeksforgeeks translation

Source: Internet
Author: User

Worst, average and optimal uptime (worst, Average and best Cases)

In the previous article, we discussed how incremental analysis can solve the problem of analytic algorithms, so in this article, we use linear search to illustrate how to analyze the performance of the algorithm using the Stepwise analysis method.

We analyze the algorithm from three aspects:

1. Worst Case scenario

2. The average situation

3. Best case

This is a very simple linear lookup code to find x from arr[]

//linearly search x in arr[]. If x is present then return the index,//otherwise return-1intSearchintArr[],intNintx) {    inti;  for(i=0; i<n; i++)    {       if(Arr[i] = =x)returni; }    return-1;} /*Driver program to test above functions*/intMain () {intArr[] = {1,Ten, -, the}; intx = -; intn =sizeof(arr)/sizeof(arr[0]); printf ("%d is present at index%d", X, Search (arr, n, x));    GetChar (); return 0;}

Worst case analyses (usually all can be done) worst cases analysis (usually do)

In the worst run-time analysis, we calculate the upper bounds of the run time. In this worst case scenario, the maximum number of operations will be run (maximum numbers of operations) in a linear search, where the worst case occurs when X is not in array[], so search () is compared with the X and all elements in array[]. So the worst time complexity is O (n);

Average situation analysis (sometimes completed) Average case analyses (sometimes done)

In the average situation analysis, we calculate the run time of all possible inputs, add all the time divided by the quantity, we must know the distribution of these conditions. Under the linear search algorithm, we assume that all situations are evenly distributed (including x not in array[]. (The position of x in an array of n numbers is n+1)

Average case time = = = O (n)

Best cases Analysis (Bogus)

In the best case analysis, we calculate the running time of the lower bounds, the algorithm with the fewest operations can be done, in the linear search, the best case is the position of X is the first, so that the total number of operations is fixed, does not depend on the size of N. So in the best case the time complexity is O (1).

In most cases, we use the worst case analysis algorithm, we can guarantee the upper bounds of the running time, which is a good information for the algorithm.

The analysis of the average situation is not easy to analyze for many specific examples. Because we have to know the distribution of the input.

And the best case analysis is not very good, to ensure that the bottom line does not provide any information, especially in the worst case, it is possible that your algorithm will run for a long time or even a year.

For some algorithms, the asymptotic analysis of these three scenarios is the same, for example, without the worst, best case scenario. For example, merge sort, all the time complexity of the case is O (Nlogn). For most other sorting algorithms there are worst-case and best-case scenarios, such as basic quick sorting (pivot selects the leftmost or most right element when selecting a datum). The worst case scenario is that the input array is already sorted, and the best case is that the array can always be divided into two sub-arrays of the same size. For the insertion sort, the worst case is that the input array is in reverse order, and the best case is that the input array is sorted.

Original link:

http://www.geeksforgeeks.org/analysis-of-algorithms-set-2-asymptotic-analysis/

Translation:

Rui

Algorithm worst, average and best case (worst, Average and good Cases)-------Geeksforgeeks translation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.