Tuesday, September 6, 2011

Algo for Searching and Sorting


Linear Search
This is most frequently used search method. We simply traverse the list or array or records and
check if the identification number matches with the id number of our interest.

Algorithm:
Begin:
Found = false
Count = 0
Obtain the input array.
Obtain number of terms, key
Do
{ If array[count]== key
Found = true.
Else
Count ++
} while ( count <=N) && found==false)
if false declare the key is not present in the array
Else declare the position and value of the element
End

Binary Search Algorithm
Algorithm
begin :
max=length-1
min=0;
success=false
while ((!success) && max>=min)
{
mid=max+min/2
if ( mid==key)
declare the result. Mid is the position. success=true;
else
if key < array[mid]
// adjust max to left sub array
Max = Mid-1
Else
Min=min+1
end

Bubble Sort
Algorithm
Procedure bubblesort(sort[],len)
for i=1 to len-1 do
begin
for j=0 to len-i do
begin
if [sort[j] > sort[j+1]
swap (sort[i],sort[j+1])
end
end
return

Selection Sort
Algorithm :
Selectsort(sort[],len)
for i=0 to i<len-1 do
begin
min=i
for j=i+1 j<len do
begin
if[sort[j] < sort[min]
min=j
end
if(min != i)
swap(sort[min],sort[i])
end

INSERTION SORT
Algorithm for Insertion Sort
for i=1 to i<len
begin
temp = sort[i]
for j=I to j=1
for j=i to j=1
begin
if [sort[j-1] > temp]
sort[j] = sort[j-1] // shift one position to the right
end
sort[j] = temp
end

QUICK SORT
Procedure QSort[int sort[], int lo, int hi]
Where sort[] is the input array which has to be sorted, ‘lo’ and ‘hi’ are indexes to the first and last
elements of the sub array that has to be sorted.
If (lo >= hi)
Return;
Initialize :
i = lo-1
j = hi
pivot = sort[hi] // set pivot element to print to last element of the array
while (i < j)
{
while (Sort[++i] < pivot);
while (j>=0 && sort[—j] > pivot);
if (i<j)
swap(sort[i],sort[j])
}



Thursday, May 26, 2011

notes on calloc and malloc

calloc() allocates memory for an array of nmemb elements of sizebytes each and returns a pointer to the allocated memory. The memory is set to zero.


malloc() allocates size bytes and returns a pointer to the allocated memory. The memory is not cleared.

free() frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(),


calloc() orrealloc(). Otherwise, or if free(ptr)has already been called before, undefined behaviour occurs. If ptr is NULL, no operation is performed.

realloc() changes the size of the memory block pointed to by ptr to size bytes. The contents will be unchanged to the minimum of the old and new sizes; newly allocated memory will be uninitialized. If ptr is NULL, the call is equivalent to malloc(size); if size is equal to zero, the call is equivalent to free(ptr). Unlessptr is NULL, it must have been returned by an earlier call to malloc(), calloc() or realloc(). If the area pointed to was moved, a free(ptr) is done.

Saturday, May 7, 2011

Java program for display difference between two date

class date_difference
{
int r=0;
public int datechk(int d,int m,int y)
{
if((y>100)&&(y<9999))
{
if((m==2)&&(y%4==0))
{
if((d>=1)&&(d<=29))
 
      r=1;
  else
  r=0;
    }
    else if((m==2)&&(y%4!=0))
    {
        if((d>=1)&&(d<=28))
       
            r=1;
            else
            r=0;
       
    }
   
    else if((m==1)||(m==3)||(m==5)||(m==7)||(m==8)||(m==10)||(m==12))
    {
        if((d>=1)&&(d<=31))
       
            r=1;
            else
            r=0;
       
    }
    else if((m==4)||(m==6)||(m==9)||(m==11))
    {
        if((d>=1)&&(d<=30))
       
            r=1;
            else
            r=0;
       
    }   
    else
       r=0;
      
    }
    return r;
}
      
       public void main(int d1,int d2, int m1,int m2,int y1,int y2)
       {
           int mo[]={31,28,31,30,31,30,31,31,30,31,30,31};
           int f,f1,i,tot=0;
           f=datechk(d1,m1,y1);
           f1=datechk(d2,m2,y2);
          
          
           if(f==1 && f1==1)
        {
            if(y1==y2)
            {
                if(y1%4==0)
                mo[1]=29;
                for(i=m1+1;i<m2;i++)
                    tot+=mo[i];
                if(m1 == m2)
                    tot+=d2-d1;
                else
                    {
                    tot+=((mo[m1-1]-d1)+1);
                    tot+=d2;
                    }
                System.out.println(tot);
                }
            else if(y1<y2)
            {
                for(i=(y1+1);i<y2;i++)
                {
                    tot+=(i%4==0?366:365);
                }
                if(y1%4==0)
                mo[1]=0;
                for(i=m1;i<12;i++)
                tot+=mo[i];
                tot+=((mo[m1-1]-d1)+1);
                if(y2%4==0)
                mo[1]=29;
                for(i=0;i<m2-1;i++)
                tot+=mo[i];
                tot+=d2;
                System.out.println(tot);
            }
            else
            System.out.println("invalid years");
            }
        }
    }
       
      
       
       
       
       
       
       

Java program for display difference between two date

class date_difference
{
int r=0;
public int datechk(int d,int m,int y)
{
if((y>100)&&(y<9999))
{
if((m==2)&&(y%4==0))
{
if((d>=1)&&(d<=29))
 
      r=1;
  else
  r=0;
    }
    else if((m==2)&&(y%4!=0))
    {
        if((d>=1)&&(d<=28))
       
            r=1;
            else
            r=0;
       
    }
   
    else if((m==1)||(m==3)||(m==5)||(m==7)||(m==8)||(m==10)||(m==12))
    {
        if((d>=1)&&(d<=31))
       
            r=1;
            else
            r=0;
       
    }
    else if((m==4)||(m==6)||(m==9)||(m==11))
    {
        if((d>=1)&&(d<=30))
       
            r=1;
            else
            r=0;
       
    }   
    else
       r=0;
      
    }
    return r;
}
      
       public void main(int d1,int d2, int m1,int m2,int y1,int y2)
       {
           int mo[]={31,28,31,30,31,30,31,31,30,31,30,31};
           int f,f1,i,tot=0;
           f=datechk(d1,m1,y1);
           f1=datechk(d2,m2,y2);
          
          
           if(f==1 && f1==1)
        {
            if(y1==y2)
            {
                if(y1%4==0)
                mo[1]=29;
                for(i=m1+1;i<m2;i++)
                    tot+=mo[i];
                if(m1 == m2)
                    tot+=d2-d1;
                else
                    {
                    tot+=((mo[m1-1]-d1)+1);
                    tot+=d2;
                    }
                System.out.println(tot);
                }
            else if(y1<y2)
            {
                for(i=(y1+1);i<y2;i++)
                {
                    tot+=(i%4==0?366:365);
                }
                if(y1%4==0)
                mo[1]=0;
                for(i=m1;i<12;i++)
                tot+=mo[i];
                tot+=((mo[m1-1]-d1)+1);
                if(y2%4==0)
                mo[1]=29;
                for(i=0;i<m2-1;i++)
                tot+=mo[i];
                tot+=d2;
                System.out.println(tot);
            }
            else
            System.out.println("invalid years");
            }
        }
    }
       
      
       
       
       
       
       
       

Java program for insert or delete element using array

import java.io.*;
class arr
    {
        public static void main(String args[])throws IOException
            {
                BufferedReader br=new BufferedReader(
                    new InputStreamReader(System.in));
                int ar[]=new int[100];
                int n,i,j,pos,ch;
                System.out.print("Enter number of element :-");
                n=Integer.parseInt(br.readLine());
                if(n<=99)
                    {
                        for(i=0;i<n;i++)
                            {
                                System.out.print("Enter any no :-");
                                ar[i]=Integer.parseInt(br.readLine());
                            }
                        do
                            {
                                System.out.println("\n1.Ele insert...");
                                System.out.println("2.Ele delete...");
                                System.out.println("3.Ele display...");
                                System.out.println("4.Exit");
                                System.out.print("Enter your choice :-");
                                ch=Integer.parseInt(br.readLine());
                                switch(ch)
                                    {
                                        case 1:
                                            System.out.print("Enter position number :-");
                                            pos=Integer.parseInt(br.readLine());
                                            if(pos<n)
                                                {
                                                    for(j=n-1;j>=pos-1;j--)
                                                        ar[j+1]=ar[j];
                                                    System.out.print("Enter new inserting element :-");
                                                    ar[j+1]=Integer.parseInt(br.readLine());
                                                    n++;
                                                }
                                            else
                                                System.out.println("Sory invalid position number enterde.....");
                                            break;
                                        case 2:
                                            System.out.print("Enter position number :-");
                                            pos=Integer.parseInt(br.readLine());
                                            if(pos<n)
                                                {
                                                    for(j=pos-1;j<n;j++)
                                                        ar[j]=ar[j+1];
                                                    System.out.println("element deleted......");
                                                    n--;
                                                }
                                            else
                                                System.out.println("Sory invalid position number enterde.....");
                                            break;
                                        case 3:
                                            for(i=0;i<n;i++)
                                                System.out.print(ar[i]+"\t");
                                            System.out.println();
                                            break;
                                        case 4:
                                            System.out.println("Program end.......");
                                            break;
                                        default:
                                            System.out.println("Invalid enter select......");
                                    }
                            }while(ch != 4 );
                    }
                else
                    System.out.println("Out of range........");
                   
            }
    }

Java Prog for convert any base to any base

import java.io.*;
class anybase_anybase
{
public String deci_to_other(int no,int b)
{
String cno="";
int r;
         while(no>0)
         {
             r=no%b;
             if(r>9)
             {
                 r+=55;
                 char c=(char)r;
                 cno=c+cno;
                }
                else
                {
                    cno=r+cno;
                }
                no=no/b;
            }
                   return cno;
           
        }
   
   
    public int other_to_decimal (String s,int base)
{
int g,num=0,a=0;
   
  
int l=s.length();
for(int i=l-1;i>=0;i--)
{
char c=s.charAt(i);
if(c>=65 && c<=70)
{c-=55;
 g=c;

num=num+(g*((int)Math.pow(base,a)));
    }
    else
    {
        c-=48;
         g=c;
        
         num=num+(g*((int)Math.pow(base,a)));
        }
        a++;
    }
   return num;
}
   
   
    public  void meth()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        {
            System.out.println("enter choice");
            System.out.println("1.to convert from decimal number to number in other base");
            System.out.println("2.to convert from number in other base to decimal no");
            System.out.println("3.to convert no in any base to number in any base");
           
            int n=Integer.parseInt(br.readLine());
           
            switch(n)
            {
                case 1:
                {
                    System.out.println("enter no and base both ");
                    int no=Integer.parseInt(br.readLine());
                    int b=Integer.parseInt(br.readLine());
                   
                    String f=deci_to_other(no,b);
                    System.out.println(f);break;
               
            }
                case 2:
             {  
                    System.out.println("enter no in String and base");
                    String s=br.readLine();
                    int b=Integer.parseInt(br.readLine());
                   
                    int f=other_to_decimal(s,b);
                    System.out.println(f);break;
                } 
                    case 3:
                {
                    System.out.println("enter no ,its base and the base to which it is to be converted");
                     String s=br.readLine();
                 int b=Integer.parseInt(br.readLine());
                  int bas=Integer.parseInt(br.readLine());
                    int k=other_to_decimal(s,b);
                    String f=deci_to_other(k,bas);
                    System.out.println(f);
                }}
            }
                   
                }
            }
                   

Java Prog for convert any base to any base

import java.io.*;
class anybase_anybase
{
public String deci_to_other(int no,int b)
{
String cno="";
int r;
         while(no>0)
         {
             r=no%b;
             if(r>9)
             {
                 r+=55;
                 char c=(char)r;
                 cno=c+cno;
                }
                else
                {
                    cno=r+cno;
                }
                no=no/b;
            }
                   return cno;
           
        }
   
   
    public int other_to_decimal (String s,int base)
{
int g,num=0,a=0;
   
  
int l=s.length();
for(int i=l-1;i>=0;i--)
{
char c=s.charAt(i);
if(c>=65 && c<=70)
{c-=55;
 g=c;

num=num+(g*((int)Math.pow(base,a)));
    }
    else
    {
        c-=48;
         g=c;
        
         num=num+(g*((int)Math.pow(base,a)));
        }
        a++;
    }
   return num;
}
   
   
    public  void meth()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        {
            System.out.println("enter choice");
            System.out.println("1.to convert from decimal number to number in other base");
            System.out.println("2.to convert from number in other base to decimal no");
            System.out.println("3.to convert no in any base to number in any base");
           
            int n=Integer.parseInt(br.readLine());
           
            switch(n)
            {
                case 1:
                {
                    System.out.println("enter no and base both ");
                    int no=Integer.parseInt(br.readLine());
                    int b=Integer.parseInt(br.readLine());
                   
                    String f=deci_to_other(no,b);
                    System.out.println(f);break;
               
            }
                case 2:
             {  
                    System.out.println("enter no in String and base");
                    String s=br.readLine();
                    int b=Integer.parseInt(br.readLine());
                   
                    int f=other_to_decimal(s,b);
                    System.out.println(f);break;
                } 
                    case 3:
                {
                    System.out.println("enter no ,its base and the base to which it is to be converted");
                     String s=br.readLine();
                 int b=Integer.parseInt(br.readLine());
                  int bas=Integer.parseInt(br.readLine());
                    int k=other_to_decimal(s,b);
                    String f=deci_to_other(k,bas);
                    System.out.println(f);
                }}
            }
                   
                }
            }
                   

Thursday, April 21, 2011

Dos Internal Command Notes


1.        Internal Commands

These are for performing basic operations on files and directories and they do not need any external file support.

2.        External Commands

These external commands are for performing advanced tasks and they do need some external file support as they are not stored in COMMAND.COM

Most Commonly Used Internal DOS Commands

1.        DATE

This command is used to display the system current date setting and prompt you to enter a new date. The syntax is: DATE [/T | date]

f you type DATE without parameters then it displays current date and prompts to enter new date. We should give new date in mm-dd-yy format. If you want to keep the same date just Press ENTER. DATE command with /T switch tells the command to just output the current system date, without prompting for a new date.

2.        TIME

This command is used to displays or set the system time.
The syntax is: TIME [/T | time]

Same as DATE command, typing TIME with no parameters displays the current time and a prompt for a new one. Press ENTER to keep the same time. TIME command used with /T switch tells the command to just output the current system time, without prompting for a new time.

3.        COPY CON

It is used to create a file in the existing directory. Syntax is: COPY CON filename after that press Enter and start typing your text and after you’re done typing your text, to save and exit hit F6 key.

TYPE

This command is used to display the contents of a text file or files. The syntax is: TYPE [drive:][path]filename
Now, lets try to display the contents of the file named filename we’ve created earlier using COPY CON command.

4.        CLS

It is used to clear the screen. Syntax is CLS

5.        REN

This command is used to change/modify the name of a file or files.
Syntax is: REN [drive:] [path] filename1 filename2.

Here, filename1 is source file for which you wanted to change the name, and filename2 will obviously becomes your new file name. Also note that you cannot specify a new drive or path for your destination file.

6.        DIR

This command displays a list of files and subdirectories in a directory. Syntax is: DIR [drive:] [path] [filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N] [/O[[:]sortorder]] [/P] [/Q] [/S] [/T[[:]timefield]] [/W] [/X] [/4]

Here,

[drive:][path][filename]
Specifies drive, directory, and/or files to list.
/A:attributes
Displays files with specified attributes. The possible attributes are as follow: D → Directories, R → Read-only files, H → Hidden files, A → Files ready for archiving, S → System files, – Prefix meaning not
/B
display in bare format with no heading information or summary
/C
Using this attribute with dir by default displays the thousand separator in file sizes. To disable display or separator use /-C
/D
Displays file list sorted by column.
/L
Uses lowercase in listing file names and sub-directories.
/N
Display in new long list format where filenames are on the far right.
/O:sortorder
Displays list by files in sorted order. The sortorder attributes are as follow: N → By name (alphabetic), S → By size (smallest first), E → By extension (alphabetic), D → By date/time (oldest first), G → Group directories first, – Prefix to reverse order
/P
Display page wise pausing after each screenful of information and prompts to press any key to continue.
/Q
Displays the owner of a file or files.
/S
Displays files in specified directory and all subdirectories. Bear caution in using this in your root directory as you may end up in overflowing information. To stop the screen overflow at any point hit Pause-Break key.
T:timefield
This sorts and displays the list based on time field specified. C for Creation, A for Last Access, W for Last Written
/W
Displays list width wise or wide list format.
/X
This is used to display the short names generated for non-8dot3 file names.

Note that switches may be different in the DIRCMD environment variable, in which case just override present switches by prefixing any switch with – (hyphen), for example instead of using /P use /-P

7.        VER

This command displays the version of the Microsoft Windows running on your computer.

8.        VOL

It displays the disk volume label and serial number, if they exist for the drive specified. If no drive is specified it displays for the active drive.
Syntax is VOL [drive:]

9.        DEL/ERASE

Used to delete one or more files.
Syntax is DEL [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names
Here,

names
Specifies a list of one or more files or directories. Wildcards * and ? may be used to delete multiple files. * indicates group of unknown characters whereas using wildcard ? in file-names is for single unknown character. And using this command if a directory is specified, all files within the directory will be deleted.
/P
Prompts for (Y)es/(N)o confirmation before deleting each file.tr>
/F
Used to force delete read-only files.
/S
Delete specified files from all subdirectories. If Command Extensions are enabled DEL and ERASE change while using /S switch such that it shows you only the files that are deleted, not the ones it could not find.
/Q
Delete in quite mode and do not ask if ok to delete on global wildcard
/A:attributes
Delete files based on specified attribute. The attributes are: R for Read-only files, S for System files, H for Hidden files, A for files ready for archiving and – Prefix meaning not.

10.     COPY

This command is useful in copying one or more files to another file or location. Syntax is COPY [/D] [/V] [/N] [/Y | /-Y] [/Z] [/A | /B ] source [/A | /B] [+ source [/A | /B] [+ ...]] [destination [/A | /B]]
The different switches that can be used with this command as follow along with their use.

source
It specifies the file or files to be copied.
/A
Indicates an ASCII text file.
/B
This switch indicates a binary file.
/D
This allows the destination file to be created with decryption.
destination
This specifies the directory and/or filename for the new file or files.
/V
Helps to verify new files to be written correctly.
/N
Specifying this switch uses short filename, if available, when copying a file with a non-8dot3 file name.
/Y
If destination file already exists, this switch suppresses prompting to confirm you want to overwrite it and does it asap.
/-Y
Contrary to above switch, this causes prompting to confirm you want to overwrite an existing destination file.
/Z
Copies networked files in restartable mode.

For appending multiple files for source use wildcard or file1+file2+file3 format and make sure to specify a single file for destination.

11.     MD, CD and RD

o    MD (or MKDIR) command stand for make directory and it is used to create a directory.
Syntax is MD [drive:]path

o    CD (or CHDIR) stands for create or change directory and it allows to display the name of or change the current directory or rather we can say come out of a directory. Syntax is CD [/D] [drive:][path]
→ Typing CD drive: displays the current directory in the specified drive. This CD (or CHDIR) command does not treat spaces as delimiters due to which it allows to CD into a subdirectory name that contains a space without surrounding the name with quotes.
For example:
CHDIR \program files\mozilla firefox
is the same as:
CHDIR “\program files\mozilla firefox”

→ If you type CD without any parameters it displays current drive and directory. CD.. specifies that you want to change to the higher directory in the current path. Whereas, using CD\ you can directly change to parent/root directory from any location in the current drive.

→Using /D switch changes current drive in addition to current directory for a drive.

o    RD (or RMDIR) command removes or deletes a directory. There are two conditions to remove any directory – (1) Directory to be removed should be empty. and (2) We should be outside the directory we are commanding to delete.
Syntax is RD [/S] [/Q] [drive:]path
Here, using the switch /S removes a directory tree meaning it removes all directories and files in the specified directory in addition to the directory itself. And using /Q is the quiet mode that doesn’t asks for ok approval to remove a directory tree.

Most Commonly Used External DOS Commands

1.        LABEL

It is used to create, change, or delete the volume label of a disk.
Syntax is LABEL [drive:] [label]
LABEL [/MP] [volume] [label]

Here, [drive:] is for secifying the drive letter of a drive to be labelled and [label] specifies the label of the volume disk. [/MP] is used to specify that the volume should be created as a mount point and [volume] is used to specify volume name, usually mentioned after drive letter followed by colon and then giving volume name required.

2.        CHKDSK

This command is used to check a disk and display a status report with properties of disk like serial number, volume label, memory and other properties along with errors on the disk if any.
Syntax is CHKDSK [volume path] [/F] [/V] [/R] [/X] [/I] [/C] [/L[:size]]

[volume path] is where you specify the drive letter followed by a colon and volume name to be checked. using /F switch allows you to fix errors on the disk. /V display full path and/or cleanup message if any. /R is used in tandem with /F and used to locate bad sectors and recover readable information. If you wanted to perform a less vigorous check of index entries on the disk then the right option is to use /I or /C rather then /R as they skip checking of cycles on the volume and helps in reducing the amount of time required to run chkdsk. Using /X forces the volume to dismount first before checking is performed. /L:size is all about specifying the log file size in kilobytes.

3.        TREE

This command is very useful to view the list of directories and subdirectories present on the disk in graphical form. If you wanted to include files also with directories and subdirectories, then you’ll have to give the command line as tree/f which presents the tree view of all the content on your disk. Here is the syntax for this command with allowed switches:
TREE [drive:path] [/F] [/A]
In case you wanted use ASCII instead of extended characters, then go ahead include /A in the command line.

4.        DELTREE

This command is used to remove a directory along with its contents.
Syntax is deltree [drive:path]
here, [drive:path] specifies the directory name to be deleted. All the subdirectories and files in this directory will be deleted without prompt and there’s not getting back. So, keep caution while using this command.