swapping is nothing but interchanging the values of a given character
for example : if a=5 , b=4 before swapping then it becomes
a=4,b=5 after swapping
bubble_sort (int N, int *A) { int i; swap = 1; while (swap) { swap = 0; for (i=0; i<N-1; ++i) { if (A[i] > A[i+1]) { swap = 1; A[i] ^= A[i+1]; A[i+1] ^= A[i]; A[i] ^= A[i+1]; } } } }
void swap(int& a, int& b ) { a^=b^=a^=b; }
t = a; a = b; b = t; // t is a third integer variable (swap variable) But here's a way without a swap variable, given as as a macro in C: #define SWAP(a,b) { if (a!=b) { a^=b; b^=a; a^=b; }} // Swap macro by XOR Once you define it, you can say swap(x,y) to swap x and y. The numbers kind of flow through each other and end up swapped.
swap (int *a, int *b) { *a ^= *b; *b ^= *a; *a ^= *b; } int c = 13; int d = 27; swap (&c, &d); /*c is now 27 and d is now 13 */ Note: there is no call-by-reference in C. In C++: void swap (int &a, int &b) { . int tmp; . tmp = a; . a = b; . b = tmp; }
You can't. While a string is a character array, an array is not necessarily a string. Treating arrays as if they were strings simply to swap them is madness. The correct way to physically swap arrays A and B is to copy A to a new array, C, then copy B to A, then C to B. If the arrays are the same size this is not a problem. If they are different sizes, you can only swap them if they are dynamic (not static). This means you must reallocate them. To speed up the process, copy the smallest array to C, first. A much better approach would be to point at the two arrays and swap the pointers instead.
#include<conio.h> main() { int a,b; scanf("%d%d",&a,&b); printf("Before swapping A= %d B=%d",a,b); swap(a,b); getch(); } void swap(int a,int b) { int c; c=a; a=b; b=c; printf("After swapping A= %d B=%d",a,b); }
Ellipses (...) used to emulate indentation... swap(int *a, int *b) { ... int temp; ... temp = *a; ... *a = *b; ... *b = temp; }
#include<iostream> void swap(int* x, int* x){ (*x)^=(*y)^=(*x)^=(*y); } int main() { int a, b; a=10; b=20; std::cout<<"Before swap: a="<<a<<", b="<<b<<std::endl; swap(&a,&b); std::cout<<"After swap: a="<<a<<", b="<<b<<std::endl; return(0); }
To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;
tmp= a; a= d; d= b; b= c; c= tmp; Other permutations are also possible.
void sort (int& a, int& b, int& c) { if (a>b) std::swap (a, b); if (b>c) std::swap (b, c); else return; if (a>b) std::swap (a, b); } Note that this is based upon a bubble sort algorithm. Although usually inefficient as a general sorting algorithm, given that we know there are only three elements means we can implement it reasonably efficiently without any additional space complexity. There will always be 2 or 3 comparisons but at most there will be 3 swaps. The only improvement we could really make is to implement a type of selection sort: void sort (int& a, int& b, int& c) { if (a>b && a>c) std::swap (a, c); else if (b>c) std::swap (b, c); if (a>b) std::swap (a, b); } Here we either make 3 or 4 comparisons but only 2 swaps at most. The assumption here is that a comparison is a quicker operation than a swap thus the selection sort method is more efficient. However, unless you were to sort millions of sets of three one after the other, you are unlikely to see any measurable difference in performance.
void swap(int &x, int &y) { x ^= y ^= x; } - or - void swap(int &x, int &y) { int t = x; x = y; y = t; }