Data structures and algorithms-how to calculate the complexity of time

Source: Internet
Author: User

Today we will talk about how to calculate the complexity of time.

The concept of Time complexity: (Baidu version)

The same problem can be solved by different algorithms, and the quality of an algorithm will affect the efficiency of the algorithm and even the program. The purpose of the algorithm analysis is to select the suitable algorithm and the improved algorithm.

In computer science, the time complexity of an algorithm is a function that quantitatively describes the time it takes to run the algorithm. This is a function of the length of the string representing the input value of the algorithm. Time complexity is often expressed in large O notation, excluding the lower order and first coefficients of the function. In this way, the time complexity can be called asymptotic , and it examines the situation when the input value is approaching infinity.

Note : This article takes on a "data structure and algorithm-function asymptotic growth", to learn more about asymptotic growth, click: Data structure and algorithm-the asymptotic growth of functions


Now the code, please read the comments in detail, because the entire calculation process has been reflected in the comments.


/** * Calculate Time complexity * * @author Ray * */public class Test {private void test1 (int n) {System.out.println (n);//Operation =1}private VO ID test2 (int n) {int a = 0;for (int i = 0; i < n; i++) {//Operation =na++;//operation =1}//Total operation =n*1=n//time complexity =o (1)}private void test3 (int n) {int a = 0;for (int i = 0; i < n; i++) {//Operation =nfor (int j = 0; J < N; j + +) {//Operation =na++;//operation =1}}//Total Operation =n*n*1=n^2// Inter-complexity =o (n^2)}private void test4 (int n) {int a = 0;for (int i = 0; i < n; i++) {for (int j = i; J < N; j + +) {//Operation =n,n -1,n-2,n-3 ... 1 = (n+1) n/2a++;//operation =1}}//Total operation = (n+1) n/2=n^2/2+n/2//because time complexity is an abstract concept, when the scale of n reaches a certain level, time complexity takes only the highest power, and ignores other minor items and coefficients//time complexity =o (n^2)}private void test5 (int n) {int a = 0;for (int i = 0; i < n; i++) {for (int j = i; J < N; j + +) {//Operation =n,n-1,n -2,n-3 ... 1 = (n+1) n/2a++;//Operation =1system.out.println (a);//Operation =1//for Loop total operation =2}}//total operation = (n+1) n/2*2=n^2+n//because time complexity is an abstract concept,  When the scale of n reaches a certain level, the time complexity takes only the highest power, and ignores other minor items and coefficients//Time complexity =o (n^2)}private void Test6 (int n) {int a = 0;for (int i = 0; i < n; i++) {for (int j = i; J < N; J + +) {//Operation =n,n-1,n-2,n-3 ... 1 = (n+1) n/2a++;//Operation =1system.out.println (a);//Operation =1system.out.println (i);//Operation =1//for Loop total operation =3}}//total operation = (n+1) n/2*3=n^ 2*3/2+n*3/2//because time complexity is an abstract concept, when the scale of n reaches a certain level, time complexity takes only the highest power, and ignores other minor items and coefficients//Time complexity =o (n^2)}private void test7 (int n) {int a = 0;int B = 0;for (int i = 0; i < n; i++) {//Operation =nfor (int j = 0; J < N; j + +) {//Operation =na++;//Operation =1system.out.println ( a);//Operation =1//for the total operation within the loop =2for (int k = 0; k < n; k++) {//Operation =nb++;//operation =1//for Loop total Operation =1}}}//Total operation ==n^3+2n^2//because time complexity is an abstraction Concept, when the scale of n reaches a certain level, time complexity takes only the highest power, and ignores other minor terms and coefficients//Time complexity =o (n^3)}public static void Main (string[] args) {int n = 10; Test T = new test (); T.test1 (n); T.test2 (n); T.test3 (n); T.test4 (n); T.test5 (n); T.test6 (n); T.test7 (n);}}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Data structures and algorithms-how to calculate the complexity of time

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.