Showing posts with label Algorithm. Show all posts
Showing posts with label Algorithm. Show all posts

May 11, 2012

Swapping Algorithm without extra variable.

One way to this is by using the Exclusive OR (XOR)

Sample Code

$x = $x ^ $y;
$y = $y ^ $x;
$x = $x ^ $y;

Explanation

Lets try to use X=5 and Y=15; Now let convert the value of X and Y to each Binary data. Here's is, X=0101 and Y=1111

  • Lets see first statement.
    $x = $x ^ $y; Binary: 1010 ^ 1111 ------ 0101 ( X now has value of 10)
  • Second Statement
  • Last statement

Paging Algorithm

Paging Algorithm
int topage = maxRecords * currentPage;
int offset = topage - (maxRecords);