Data structure 1-space-time complexity

Source: Internet
Author: User
Tags assert printf
data Structure 1-space-time complexity

Program design = algorithm + data structure.
Algorithm: A description of the solution steps to solve the characteristic problem, which is represented by the finite strip instruction in the computer.
The algorithm has three characteristics of certainty, feasibility and poverty. Complexity of Time

In short, time complexity estimates the number of times a program needs to be executed.
The calculation method of the general solution time complexity:
1. Replace all addition constants in run time with constant 1
2. In the modified run function, only the highest item is kept.
3. If the highest order coefficient exists and is not 1, the coefficient is removed.

Chestnut 1.1
#define _crt_secure_no_warnings
#include <stdio.h>
#include <Windows.h>
int main ()
{
    int arr[] = {9, 5, 6, 4, 7, 8, 2,, 5,.};
    int sz = sizeof (arr)/sizeof (arr[0]);
    int i = 0, j = 0;
    int flag = 0;
    for (i = 0; i < sz-1, i++)
    {for
        (j = 0; J < sz-i-1, j + +)
        {
            if (Arr[j-1]>arr[j]) 
  
   {
                arr[j-1] ^= arr[j];
                ARR[J] ^= arr[j-1];
                ARR[J-1] ^= arr[j];
                flag = 1;
            }   
        }
        if (flag = = 0) break
            ;
    }
    for (int k = 0; k < sz; k++)
        printf ("%d", arr[k]);
    printf ("\ n");
    System ("pause");
    return 0;
}
  
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

Above the classic bubble sort, its time complexity is O (n^2). Complexity of Space

Similar to the discussion of time complexity, an algorithm's spatial complexity (space complexity) S (n) is defined as the storage space consumed by the algorithm.  
We know that the space complexity of Chestnut 1.1 is O (1). Recursive and non-recursive binary lookup methods

Non-recursive binary search #include <stdio.h> #include <Windows.h> #include <assert.h> int binary_search (int*arr, int len
    , int key) {assert (arr);
    int left = 0;
    int right = len-1;
    int mid = 0;
        while (left <= right) {mid = left + ((right-left) >> 1);
        if (Arr[mid] > key) {right = Mid-1;
        } else if (Arr[mid] < key) {left = mid + 1;
        } else {return mid;
}} return-1;
    } void Test () {int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

    int len = sizeof (arr)/sizeof (arr[0]);
    printf ("%d\n", Binary_search (arr, len, 0));
    printf ("%d\n", Binary_search (arr, Len, 1));
    printf ("%d\n", Binary_search (arr, Len, 2));
    printf ("%d\n", Binary_search (arr, Len, 3));
    printf ("%d\n", Binary_search (arr, Len, 4));
    printf ("%d\n", Binary_search (arr, Len, 5));
    printf ("%d\n", Binary_search (arr, Len, 6)); printf ("%d\n", BiNary_search (arr, Len, 7));
    printf ("%d\n", Binary_search (arr, Len, 8));
    printf ("%d\n", Binary_search (arr, Len, 9));
printf ("%d\n", Binary_search (arr, Len, 10));
    } int main () {Test ();
    System ("pause");
return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 4 4 45 46 47 48 49 50 51
 recursive #include <stdio.h> #include <Windows.h> #include <assert.h> int Binary_
    Search (Int*arr, int left, int right,int key) {assert (arr! = NULL);
        while (left<=right) {int mid = left + ((right-left) >> 1);
        if (Arr[mid] > key) return Binary_search (arr, left, mid-1, key);
        else if (Arr[mid] < key) return Binary_search (arr, mid + 1, right, key);
    else return mid;
} return-1;
    } void Test () {int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};


    int len = sizeof (arr)/sizeof (arr[0]);
    printf ("%d\n", Binary_search (arr, 0, 9, 0));
    printf ("%d\n", Binary_search (arr, 0, 9, 1));
    printf ("%d\n", Binary_search (arr, 0, 9, 2));
    printf ("%d\n", Binary_search (arr, 0, 9, 3));
    printf ("%d\n", Binary_search (arr, 0, 9, 4));
    printf ("%d\n", Binary_search (arr, 0, 9, 5)); printf ("%d\n", Binary_search (arr, 

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.