Discussion about data exchange in the program

Source: Internet
Author: User

Data exchange may be the most basic operation frequently encountered in the program. The question discussed today is about the problem encountered during program writing. Data Exchange has always been implemented in the following ways:

a = a ^ b;b = b ^ a;a = a ^ b;  

Implement data exchange. However, this method suddenly fails during fast sorting.

#include <iostream>  using namespace std;  int QuickPartation(int vec[], int low, int high) {if(low < high) {int i = low - 1;int temp = vec[high];for(int j=low; j<=high-1; j++) {if(vec[j] <= temp) {i = i + 1;vec[i] = vec[i] ^ vec[j];vec[j] = vec[j] ^ vec[i];vec[i] = vec[i] ^ vec[j];}}vec[high] = vec[i+1];vec[i+1] = temp;return i+1;}}  void QuickSort(int vec[], int low, int high) {if(low < high) {int mid = QuickPartation(vec,low,high);QuickSort(vec,low,mid-1);QuickSort(vec,mid+1, high);}}void Print(int vec[], int n) {for(int i=0; i<n; i++)cout<<vec[i]<<' ';cout<<endl;}int main() {      int a[] = {12,4,5,8,21,1,13};      Print(a,7);      QuickSort(a,0,6);      Print(a,7);      system("pause");      return 0;    } 

The running results of programs that I think are not wrong have made me very entangled:

After a short analysis, it may be a problem of exchange, so I adopted a traditional method for exchange.

#include <iostream>  using namespace std;  int QuickPartation(int vec[], int low, int high) {if(low < high) {int i = low - 1;int temp = vec[high];for(int j=low; j<=high-1; j++) {if(vec[j] <= temp) {i = i + 1;int m = vec[i];vec[i] = vec[j];vec[j] = m;}}vec[high] = vec[i+1];vec[i+1] = temp;return i+1;}}  void QuickSort(int vec[], int low, int high) {if(low < high) {int mid = QuickPartation(vec,low,high);QuickSort(vec,low,mid-1);QuickSort(vec,mid+1, high);}}void Print(int vec[], int n) {for(int i=0; i<n; i++)cout<<vec[i]<<' ';cout<<endl;}int main() {      int a[] = {12,4,5,8,21,1,13};      Print(a,7);      QuickSort(a,0,6);      Print(a,7);      system("pause");      return 0;    } 

The running result indicates that the problem lies in data exchange.


What is the problem?

After observing the data, we can find that in the wrong data sorting process, all the exchanged data is cleared by zero. We all know that two identical numbers are cleared after an exclusive or operation, that is to say, the above data is caused by the same number of values. Is there any special case for the use of different or exchange data ?"

Exclusive or exchange is a common data exchange method. There is no special case. It can only be a problem in the program. After analyzing the program, we can find that the data is:

If (VEC [0] <13)

{

I = 0;

VEC [0] = VEC [0] ^ VEC [0];

VEC [0] = VEC [0] ^ VEC [0];

VEC [0] = VEC [0] ^ VEC [0];

}

When I = J occurs in the process of the above program, the data is exchanged with itself, and the VEC [0] = VEC [0] ^ VEC [0] statement is executed, the value stored in VEC [0] is changed to 0. Even after the subsequent calculation, the value of VEC [0] is still zero, because a number is an exclusive or. Therefore, the use of exception or exchange data is cleared.

Therefore, when using an exclusive or data exchange, you must note that this operation is not allowed if the same variable that references the passed value (or pointer. In short, the memory addresses of the two operands cannot be the same. If the two numbers have the same memory address, it means that the two numbers are equal and there is no need for data exchange. Therefore, it is best to increase the determination of whether the data is equal during the data exchange process.

After the above analysis, you can modify the Code as follows:

int QuickSortPartation(int a[],int low,int high) {if(low < high) {int i = low - 1;int temp = a[high];for(int j=low;j

The execution result after the above Code is run is correct.

Therefore,In the process of using an exclusive or data exchange, the memory address of the two data must be different.

 

 

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.