Skip to content

Instantly share code, notes, and snippets.

@Bekt
Last active December 10, 2015 05:08
Show Gist options
  • Select an option

  • Save Bekt/4385451 to your computer and use it in GitHub Desktop.

Select an option

Save Bekt/4385451 to your computer and use it in GitHub Desktop.
//http://coolcodebro.blogspot.com/2012/12/swap-number-in-place.html
void run() {
swap(10, 25);
}
void swap(int a, int b) {
if(a > b)
swapM(a, b);
else
swapM(b, a);
}
void swapM(int max, int min) {
System.out.printf("Before:%n a: %d, b: %d %n", max, min);
max = max-min; //25-15 = 15
min = max+min; //15+10 = 25
max = min-max; //25-15 = 10
System.out.printf("After:%n a: %d, b: %d %n", max, min);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment