The analysis of pointer reference and time-space complexity

Source: Internet
Author: User

Analysis of time-space complexity:

Sacrifice time in exchange for space, or sacrifice space for time:
If the space is very large, you can use multi-threading, can greatly speed up
If the space is small and only one element is used, this increases the time

Use XOR ^ without the help of intermediate variables to achieve the exchange of data a^=b
To complete the interchange between the two arrays, using an array to swap as a temporary intermediate variable, you can use multithreading,
Compared to using only one data as a temporary variable, greatly speed up the speed, multithreading can be processed in parallel,
O (n) speeds up compared to O (1) and O (1) Saves space compared to O (n)
With xor ^ a^=b is O (0) Save time and save space
XOR Exchange: (*P1) ^= (*P2);
(*P2) ^= (*P1);
(*P1) ^= (*P2);

#include <iostream>
#include <time.h>

using namespace Std;


In general, look at the time of an algorithm to see the cycle, look at the space to see how much memory saved
void search (int *p,int n,int data)
{

for (int i = 0; i < n;i++)//Cycle count time
{
if (P[i]==data)
{
Return
}
}
}
void swap (int *p1,int *p2)
{
(*P1) ^= (*P2);
(*P2) ^= (*P1); /* With Xor ^ a^=b is O (0) Save time and save space */
(*P1) ^= (*P2);

{
int temp = *P1;
*P1 = *P2; With the middle variable speed will be very slow to change space with time
*P2 = temp;
}

}


void Main ()
{
time_t ts;
unsigned int times = time (&ts);
Srand (times);

int a[100];
for (int i = 0; i < 100;i++)
{
cout << (A[i] = rand ()) << Endl;
}
unsigned short data = 65535;
Data + = 1, fault tolerance, can be handled in any situation
printf ("Brother Fei has%d", data);

Cin.get ();
}

How to index:
Compared to arrays: compared to chained storage:
n indexes, m elements n indexes, M elements
Delete Order find binary lookup linked list lookup: O (M) index O (m/n)
Array index array index array index list insert: O (1) index O (1)
O (M) O (m/n) O (M) O (m/n) increased deletion of O (N) O (m/n) linked list: 0 (1) index O (1)
Added list: O (1) index O (1)

Finally, we share two little perverted interview questions:

1. Int (*z (int x,int (*y) (int))) (int)
Analysis:
The prototype is: Int (*t) (int)
Int (*y) (int) =y function pointer
1 can be abbreviated as: Y z (int x, y)
Z is a function whose argument is a function pointer return value is also a function pointer


2. Int (*&z (int x,int (*&y) (int))) (int)
&z a reference to a function pointer whose argument is a reference to a function pointer that returns a reference to a function pointer

The analysis of pointer reference and time-space complexity

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.