Friday, April 2, 2010

Useful links for me...

Tuesday, March 30, 2010

Bag Datastructure

An unordered collection of values that may have duplicates.

A bag has a single query function, numberIn(v), which tells how many copies of an element are in the bag, and two modifier functions, add(v) and remove(v).

numberIn(v) = returns no of element in Bag of type v;
add(v) = add a new element v in the bag;
remove(v) = removes all the element from the Bag of type v;
showBag() = prints all the element of the Bag;

Implementation of Bag in cpp(template).

#include < iostream >
#include < string.h >

using namespace std;

template < class Data >

class Bag
{
private:
struct node
{
Data data;
node *link;
}*ptr;
public:
Bag()
{
ptr = NULL;
}

void add(Data a)
{
if(ptr==NULL)
{
ptr = new node;
ptr- > data = a;
ptr- > link = NULL;
}
else
{
node *temp;
temp = new node;
temp- > data = a;
temp- > link = ptr;
ptr = temp;
}
}

void remove(Data data){
int present = 0;
if(ptr==NULL){
cout < < "Error !! Bag is empty" < < endl;
return;
}
else{
node *temp;
node *preTemp;
temp = ptr;
while(temp != NULL){
if(temp- > data == data){
if(temp == ptr){
ptr = ptr- > link;
delete temp;
temp = ptr;
}
else if(temp- > link!=NULL){
preTemp- > link = temp- > link;
delete temp;
temp = preTemp;
}
else if(temp- > link==NULL){
preTemp- > link = NULL;
delete temp;
}
}
else{
preTemp = temp;
temp = temp- > link;
}
}
}
}

int numberIn(Data data){
int count = 0;
if(ptr==NULL)
{
return count;
}
else
{
node *temp;
temp = ptr;
while(temp != NULL){
if(temp- > data == data){
count++;
}
temp = temp- > link;
}
}
return count;
}
void showBag(){
node *temp;
temp = ptr;
while(temp!=NULL){
cout < < " [" < < temp- > data < < "] " < < endl;
temp = temp- > link;
}
}
};



int main()
{
int input,remove;
Bag < int > intBag;

for(int i=0;i < 20;i++)
{
cin > > input;
intBag.add(input);
}
intBag.showBag();
cin > > remove;
cout < < "NumberIN = " < < intBag.numberIn(1) < < endl;
intBag.remove(remove);
cout < < "removed" < < endl;
intBag.showBag();
}

Adaptive Sorting.

A sorting algorithm is called adaptive sort if it takes advantage of previously shorted inputs.The problem with the common sorting algorithms is that they usually don't adapt to the data. Neither quicksort nor merge sort gives optimal results in partially ordered data (quicksort can even give Θ(n2) performance), and adding a preprocessing pass only gets you so far.Adaptive sorting is usually performed by modifying existing sorting algorithms.

This is an attractive algorithm because nearly sorted sequences are common in practice. Thus, the performance of existing sort algorithms can be improved by taking into account the existing order in the input.

A classic example of an adaptive sorting algorithm is Straight Insertion Sort. In this sorting algorithm, we scan the input from left to right, repeatedly finding the position of the current item, and insert it into an array of previously sorted items.

void straightInsertionSort(int *arr, int n){
*(arr+o) = -someLargeValue;
for(int j = 2; j < n; j++){
i = j-1;
int temp = *(arr+j);
while(temp < *(arr+i)){
*(arr+i+1) = *(arr+i);
i++;
}
*(arr+i+1) = temp;
}
}

Friday, March 26, 2010

Wednesday, December 30, 2009

Tricky question.....

Que: You have 2 arrays each containing a set of numbers. Write an
algorithm that gets the elements present in the first array and missing
from the second. It should have n complexity.


void fun(int a[], int b[], int N){
int max = a[0];
for(int i =0; i < N;i++)
if(max < a[i])
max = a[i];

int c[max]={0};
for(int i =0; i < N;i++)
c[a[i]]++;

for(int i =0; i < N;i++)
c[b[i]]--;

for(int i =0; i < N;i++)
if(c[a[i]]==1)
print a[i];
}

How to find median of a BST?

If the no of keys are odd, there will be only one median, if it is even there will be two median.
  • Execute two InOrder traversal, the first one runs as the double speed the second one. It means the first one visits two nodes while the second one visits one nodes. So when the first one finish traverse, the second one point the mid value!

Thursday, December 3, 2009

Creating Own exception class in java.

Note: Exception class must be inherited


class OutOfRange extends Exception{
String message;
OutOfRange(){
message = new String("Please Enter No's between 0 and 100");
}
String toStr(){
return message;
}
}

public class MainCls{
public static void main(String args[]){
int input;
String inputStr;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);

inputStr = br.readLine();
input = Integer.parseInt(inputStr);

try{
if(input < 0 || input > 100)
throw new OutOfRange();
}
catch(OutOfRange e){
System.out.println(e.toStr());
}
//Rest of code...
}
}

Difference between compiler and Interpretor.

Interpreter
An interpreter reads the source code one instruction (a line) at a time, converts this line into machine code and executes it. The current machine code is then discarded and the next line is read. Examples of interpreters are Basic and script interpreters such as Tcl script, JavaScript etc.

Advantages
The advantage of this is it's simple and you can interrupt it while it is running, change the program and either continue or start again.

Disadvantages

Every line has to be translated every time it is executed, because of this interpreters is relatively slow.

Compiler
A compiler reads the whole source code and translates it into a complete machine code output as a new file(object file). This completely separates the source code from the executable file. Examples of compilers are Visual Basic, C, C++, C#, Fortran, Cobol, Ada, Pascal etc.

Advantages
Advantage of this is that the translation is done once only and as a separate process. The program that is run is already translated into machine code so is much faster in execution.

Disadvantages
The disadvantage is that you cannot change the program without going back to the original source code, editing that and recompiling.

Thursday, September 10, 2009

trick to access a private member of class through its object


#include < iostream >

using namespace std;

class Base{
public: virtual void fun()=0;
};

class derived:private Base{
private: void fun(){
cout < < "accessing private method" < < endl;
}
};

int main(){
Base *base;
derived *dptr = new derived();
base = (Base *)dptr;
base- > fun();
return 0;
}

Thursday, July 30, 2009

Find out tolal no of triangles...........


Find total no of triangle in the following diagram.....
It takes too much time to count. The counting will we too difficult on increasing the no of levels........

Here i am going to give a general procedure to solve such type of problems......






Lets start with level one........If there is only one triangle
then total no of triangle will be one.

T(1) = Tri(1)
= 1



Now for the level two....
The total no of triangle will be

T(2) = Tri(1) +Tri(2)
= 4 + 1
= 5

Here T(n) represent the total no of triangle and Tri(n) represents the triangles of size n.


For level 3 you can see there is 9 triangles of size 1, 3 triangles of size 2 and 1 of size 3......

T(3) = Tri(1) + Tri(2) + Tri(3)
= 9 + 3 + 1
= 13

For level 4 you can see there is 16 triangles of size 1, 7 triangles of size 2, 3 of size 3 and 1 of size 4.



T(4) = Tri(1) + Tri(2) + Tri(3) + Tri(4)
= 16 + 7 + 3 + 1
= 27


Now we have a sequence 1,5,13,27..........
put this sequence on "http://www.research.att.com/~njas/sequences/"
you will get the formula for the any sequence which exist.

I found the that the above sequence does not exist but it is approximate to
a sequence so the formula for the nth term is Floor[n*(n+2)*(2n+1)/8........


using formula for the size 5 (level 5)

T(5) = Floor(5*7*11/8)
= Floor(48.12500)
= 48

hence for the size 6 (level 6)

T(6) = Floor(6*8*13/8)
= Floor(78)
= 78

Search Ranjeet's Blog