Vamsi Pavan’s Place

When curiousity outbursts …..

Entries Tagged as 'Algorithms'

Permutations and Combinations of a string

October 6th, 2006 · No Comments · Algorithms

void permute(char *aHead,char *aTail)
{
// If there are no characters remaining in Head, i.e. we got one permutation
// So print it.
if(strlen(aHead) == 0){
printf(”%s\n”,aTail);
return;
}
// Each time remove one character from Head and add it to the tail,
// and then recurse ……
for(int i=0;i< strlen(aHead);i++)
int j=0,insertAt;
char *NewTail = new char[100];
char *NewHead = new char[100];
// Create the arguments that need […]

Bookmark it! These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Live
  • StumbleUpon
  • BlinkList
  • YahooMyWeb
  • NewsVine
  • blogtercimlap
  • Netvouz
  • Technorati
  • Slashdot
  • Print this article!

[Read more →]

Tags:

Removing duplicates in an array in one pass

October 6th, 2006 · No Comments · Algorithms

int insertAt=0,index;
for(index=insertAt+1;index
if(a[insertAt] == a[index])
index+=1;
else
a[insertAt++] = a[index];
}
insertAt–;
Identification of duplicant element in an array
Identify the element that is not repeating in an array of 2n+1 elements, in O(n) time
X-or all the elements, same elements cancel each other.

Bookmark it!
These icons link to social bookmarking sites where readers can share and discover new web pages.

Bookmark it! These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Live
  • StumbleUpon
  • BlinkList
  • YahooMyWeb
  • NewsVine
  • blogtercimlap
  • Netvouz
  • Technorati
  • Slashdot
  • Print this article!

[Read more →]

Tags:

Prime number verification efficient way

October 6th, 2006 · No Comments · Algorithms

If p is a prime number other than 2, then using formats thearom it follows the condition:
2^(p-1) mod p = 1
So In order to check whether a number is prime or not, we have to check the value 2^(p-1) mod p.
And 2^(p-1) can be calculated efficiently, by following a binary search strategy. (Refer to the […]

Bookmark it! These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Live
  • StumbleUpon
  • BlinkList
  • YahooMyWeb
  • NewsVine
  • blogtercimlap
  • Netvouz
  • Technorati
  • Slashdot
  • Print this article!

[Read more →]

Tags:

Raising a number to a larger power ( power calculation)

October 6th, 2006 · No Comments · Algorithms

We will see it with an example, Lets calculate pow(x,23)
x^23 = x^22 * x
x^22 = x^11 * x^11
x^11 = x^10 * x
x^10 = x^5 * x^5
x^5

Bookmark it!
These icons link to social bookmarking sites where readers can share and discover new web pages.

Bookmark it! These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Live
  • StumbleUpon
  • BlinkList
  • YahooMyWeb
  • NewsVine
  • blogtercimlap
  • Netvouz
  • Technorati
  • Slashdot
  • Print this article!

[Read more →]

Tags:

pseudo random number generator

October 6th, 2006 · No Comments · Algorithms

The pseudo random numbers can be generated using linear congruential method. Successive members of the linear congruential sequence {x} are generated using the expression:
X_n+1 = ( a*X_n + b) mod m

Bookmark it!
These icons link to social bookmarking sites where readers can share and discover new web pages.

Bookmark it! These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Live
  • StumbleUpon
  • BlinkList
  • YahooMyWeb
  • NewsVine
  • blogtercimlap
  • Netvouz
  • Technorati
  • Slashdot
  • Print this article!

[Read more →]

Tags:

Delete a node from a Double Linked list, with out using any extra variables

October 6th, 2006 · No Comments · Algorithms

DLNode *DelNode(DLNode *aHead, int DelValue)
{

Bookmark it!
These icons link to social bookmarking sites where readers can share and discover new web pages.

Bookmark it! These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Live
  • StumbleUpon
  • BlinkList
  • YahooMyWeb
  • NewsVine
  • blogtercimlap
  • Netvouz
  • Technorati
  • Slashdot
  • Print this article!

[Read more →]

Tags:

Reverse a Double Linked list

October 6th, 2006 · No Comments · Algorithms

DLNode *revList(DLNode *aHead)
{

Bookmark it!
These icons link to social bookmarking sites where readers can share and discover new web pages.

Bookmark it! These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Live
  • StumbleUpon
  • BlinkList
  • YahooMyWeb
  • NewsVine
  • blogtercimlap
  • Netvouz
  • Technorati
  • Slashdot
  • Print this article!

[Read more →]

Tags:

Swap two variables in one line with out using temporary variable

October 6th, 2006 · No Comments · Algorithms

a = a+b-(b=a)
OR
a = a^b^(b=a)
And both of them will work also for chars and stuff ….

Bookmark it!
These icons link to social bookmarking sites where readers can share and discover new web pages.

Bookmark it! These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Live
  • StumbleUpon
  • BlinkList
  • YahooMyWeb
  • NewsVine
  • blogtercimlap
  • Netvouz
  • Technorati
  • Slashdot
  • Print this article!

[Read more →]

Tags:

Detect cycle in a linked list

October 6th, 2006 · No Comments · Algorithms

ptr1 = ptr2 = head;
do{
ptr1 = ptr1 -> next;
ptr2 = ptr2 -> next -> next;
}while(ptr1 != ptr2);
ptr2 is iterating along the list with double the speed of ptr1. If there is any cycle, then ptr1 and ptr2 will loop in the list. And since ptr1 is moving at one step and ptr2 is moving faster […]

Bookmark it! These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Live
  • StumbleUpon
  • BlinkList
  • YahooMyWeb
  • NewsVine
  • blogtercimlap
  • Netvouz
  • Technorati
  • Slashdot
  • Print this article!

[Read more →]

Tags: