Official Site

click here to view our new site launched

Pages

Thursday, March 25, 2010

Algorithm






An animated solution of the Tower of Hanoi puzzle for T(4,3).



The Tower of Hanoi or Towers of Hanoi is a mathematical game or puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape.
The objective of the puzzle is to move the entire stack to another rod, obeying the following rules:

  • Only one disk may be moved at a time.

  • Each move consists of taking the upper disk from one of the rods and sliding it onto another rod, on top of the other disks that may already be present on that rod.

  • No disk may be placed on top of a smaller disk.


Reverse the words in a sentence .

Method1

1. Reverse whole sentence first.
2. Reverse each word individually.

I am a good boy
yob doog a ma I
boy good a am I

Method 2

AAnother way to do it is, allocate as much memory as the input for the final output. Start from the right of the string and copy the words one by one to the output.
Input : I am a good boy
Output : boy
: boy good
: boy good a
: boy good a am
: boy good a am I

Method 3:

Create a linked list like

Now its a simple question of reversing the linked list!. There are plenty of algorithms to reverse a linked list easily. This also keeps track of the number of spaces between the words. Note that the linked list algorithm, though inefficient, handles multiple spaces between the words really well.

2 comments:

  1. hai... i welcome u all to join in this blog

    ReplyDelete
  2. Its a good attempt da.. Java coding section is really useful for everyone..

    ReplyDelete

$2.49 .COM at GoDaddy.com! Expires 5/14/13