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 the algorithm is a function, which quantitatively describes the execution time of the algorithm.

This is a function of the length of the string representing the input value of the algorithm. Time complexity is often expressed using the large O notation, which does not contain the lower order and first coefficients of the function. When using such a way. The complexity of time can be called asymptotic . It examines the situation when the input value is approaching infinity.

Note : This article takes on a "data structure and algorithm-function asymptotic growth", want to understand the asymptotic growth, please click: Data structure and algorithm-the asymptotic growth of functions


Now the code first. Please read the gaze specifically. As the entire computational process has been reflected in the gaze.


/** * 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 N's scale 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 degree, the time complexity takes the highest power only. and ignoring 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.printl N (a);//Operation =1//Total operation in the For 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 a Abstract concept, when the scale of n reaches a certain degree, time complexity only takes the highest power. and ignoring other minor items 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);}}



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.