1, with the third variable value exchange
int a = 1;
int b = 2;
int C = A;
A = b;
b = C;
2. Exchange with addition and subtraction
A = a + B;
b = a-b;
A = A-b;
Or
A = A-b;
b = A + B;
A = b-a;3, in different or (XOR) method Exchange
A = a ^ b;
b = a ^ b;
A = a ^ B; codeproject on an article << the magic of the Magical Exclusive or (XOR) >> below we'll test the efficiency of these three switching methods in the bubble sort algorithm
The code is as follows |
Copy Code |
public enum Echangetype { <summary> Pre-and post-value exchange with third-party variable Temp </summary> Thridvar, <summary> Using A=a+b;b=a-b;a=a-b calculation method to Exchange before and after value </summary> Js <summary> Using A=a^b;b=a^b;a=a^b calculation method to Exchange before and after value </summary> Xor }///<summary> Bubble Sort algorithm: A, first put all the numbers to be sorted into the work list b, from the first number of the list to the penultimate number, check each: If the number on one is greater than the next, swap it with the next one C, repeat step 2nd until you can no longer exchange </summary> public class Bubblesort { public void Sort (int[] arrint, Echangetype type) { System.Diagnostics.Stopwatch st = new System.Diagnostics.Stopwatch (); St. Start (); int count = 0; int temp; BOOL Hasexchangeaction; Record whether the two adjacent numbers in the current cycle have been interchanged (the array is already in order if no swaps are in place) for (int i = 0; i < arrint.length-1; i++) { count++; Hasexchangeaction = false; Each cycle is assumed to be array orderly for (int j = i + 1, J < Arrint.length; J + +) { count++; if (Arrint[i] > Arrint[j]) { Switch (type) { Case Echangetype.thridvar: temp = Arrint[i]; Arrint[i] = arrint[i + 1]; Arrint[i + 1] = temp; Break Case Echangetype.js: Arrint[i] = Arrint[i] + arrint[j]; ARRINT[J] = Arrint[i]-arrint[j]; Arrint[i] = Arrint[i]-arrint[j]; Break Case ECHANGETYPE.XOR: Arrint[i] = Arrint[i] ^ arrint[j]; ARRINT[J] = Arrint[i] ^ arrint[j]; Arrint[i] = Arrint[i] ^ arrint[j]; Break Default Break } Hasexchangeaction = true; Swaps have occurred } } if (!hasexchangeaction)//If no swaps have occurred, the array is already ordered, jump out of the loop { Break } } St. Stop (); Switch (type) { Case Echangetype.thridvar: Console.WriteLine ("Use third party variable temp for value exchange:"); Break Case Echangetype.js: Console.WriteLine ("With a = a + B;") b = a-b; A = A-b; (or a = A-b; b = A + B; A = b-a n calculation method for the value exchange before and after: "); Break Case ECHANGETYPE.XOR: Console.WriteLine ("With a = a ^ B"; b = a ^ b; A = a ^ b; The calculation method is used to exchange before and after value: "); Break Default Break } Console.WriteLine ("Total loops: {0} sort time consuming: {1}{2}", Count, St. Elapsedmilliseconds,environment.newline); } } static void Main (string[] args) { list<int> list = new list<int> (); for (int i = m >= 0; i--) { List. ADD (i); } Console.WriteLine ("Bubble sort for 100 * 100 digits {0}", Environment.NewLine);
New Bubblesort (). Sort (list. ToArray (), Echangetype.thridvar);
New Bubblesort (). Sort (list. ToArray (), echangetype.js);
New Bubblesort (). Sort (list. ToArray (), echangetype.xor);
Console.ReadLine (); } results |
Life is no place to PK