Three methods of variable exchange and simple analysis and three methods of variable exchange

Source: Internet
Author: User

Three methods of variable exchange and simple analysis and three methods of variable exchange

There are two main ways to exchange two variables: With or without temporary variables. There are three simple algorithms for specific operations:

1,Algorithm with temporary variables

#include <stdio.h>int main(void){int a, b, t;scanf("%d%d", &a, &b);t = a;a = b;b = t;printf("a = %d, b = %d\n", a, b);return 0;}


2,Algorithm 1 without temporary variables ( Addition and subtraction)
#include <stdio.h>int main(void){int a, b;scanf("%d%d", &a, &b);a = a + b;b = a - b;a = a - b;printf("a = %d, b = %d\n", a, b);return 0;}


3,Algorithm 2 without using temporary variables ( Exclusive or operation)

#include <stdio.h>int main(void){int a, b;scanf("%d%d", &a, &b);a = a ^ b;b = b ^ a;a = a ^ b;printf("a = %d, b = %d\n", a, b);return 0;}
Summary: in normal use, the algorithm with the help of temporary variables is good enough. Algorithms 1 and 2 that do not use temporary variables look good (with less than one variable), but are rarely used because of its narrow Applicability: this can be done only when the addition, subtraction, or exclusive data types are defined. These two algorithms are only used to improve the ability to read code.


Exchange a and B without intermediate variables (three methods)

A = a-B
B = a + B
A = B-

A = a * B
B = a/B
A = a/B

How can I avoid using two parameters of a third-party variable exchange function?

This requires bitwise operations, which is troublesome,
It is often used to exchange values of two variables when learning programming languages and designing programs. Our practice is usually (especially in the learning phase): to define a new variable and use it for exchange. The Code is as follows:
Int a, B;
A = 10; B = 15;
Int t;
T = a; a = B; B = t;
This algorithm is easy to understand and is especially suitable for beginners to understand the characteristics of computer programs. It is a classic Application of Value assignment statements. In actual software development, this algorithm is simple and clear, and does not produce ambiguity. It facilitates communication between programmers. In general, you may encounter the problem of switching variable values, this algorithm should be used (hereinafter referred to as the standard algorithm ).

The biggest drawback of the above algorithm is that a temporary variable is needed. Can I exchange data without using temporary variables? The answer is yes! Here we can use three algorithms: 1) arithmetic operation; 2) pointer address operation; 3) bitwise operation.

1) arithmetic operations
To put it simply, it is implemented through Common + and-operations. The Code is as follows:
Int a, B;
A = 10; B = 12;
A = B-a; // a = 2; B = 12
B = B-a; // a = 2; B = 10
A = B + a; // a = 10; B = 10
Through the above calculation, the values in a and B are exchanged. It looks simple on the surface, but it is not easy to think of, especially after getting used to standard algorithms.
The principle is to regard a and B as points on the number axis and calculate the distance between two points.
Specific process: in the first sentence "a = B-a", find the distance between the two points of AB and save it in; in the second sentence, "B = B-a", find the distance from a to the origin (the difference between the distance from B to the origin and the distance between B and AB) and save it in B; in the third sentence "a = B + a", find the distance from B to the origin (the sum of the distance between a and AB) and save it in. Complete the exchange.
Compared with the standard algorithm, this algorithm has three more computing processes, but does not use temporary variables. (Hereinafter referred to as an arithmetic algorithm)

2) pointer address operation
Because the operation on the address actually performs an integer operation, for example, the two addresses subtract an integer to indicate how many bytes are separated by the storage location of the two variables in the memory; the address is added with an integer, that is, "a + 10", indicating the address of the next 10 Class a data units whose base address is. Therefore, in theory, address exchange can be achieved through operations similar to arithmetic algorithms, so as to exchange variables. That is:
Int * a, * B; // hypothesis
* A = new int (10 );
* B = new int (20); // & a = 0x00001000 h, & B = 0x00001200 h
A = (int *) (B-a); // & a = 0x00000200 h, & B = 0x00001200 h
B = (int *) (B-a); // & a = 0x00000200 h, & B = 0x00001000 h
A = (int *) (B + int (a); // & a = 0x00001200 h, & B = 0x00001000 h
After the preceding operations, the addresses of a and B have already completed the exchange, and a points to the original B's value. Does B point to the original a's value? The above code can be compiled, but the execution results are incredible! Why?
First, you must understand that the operating system divides the memory into several areas: system code/Data zone, application code/Data zone, stack zone, global data zone, and so on. When compiling the source program, constants and global variables are all placed in the global data zone.

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.