Saturday, June 16, 2012

Some sorting methods in C

BubbleSort

#include<stdio.h>
int main()
{
int i,j,t,a[]={3,9,7,1,4},n=5;
for(i=0;i<=(n-2);i++)
for(j=0;j<5-i-1;j++)
{ if(a[j]>a[j+1])
  {t=a[j];
   a[j]=a[j+1];
   a[j+1]=t;
  }
}
for(i=0;i<5;i++)
printf("%d,",a[i]);
return(0);
}


CountingSort

#include<stdio.h>
int main()
{
int i,t,a[]={8,7,4,1,0,5,9,2},b[]={0,0,0,0,0,0,0,0,0,0},j,k=0;
for(i=0;i<8;i++)
printf("%d,",a[i]);
printf("\n\n");
for(i=0;i<8;i++)
{ t=a[i];
  b[t]=b[t]+1;
}
for(i=0;i<10;i++)
printf("%d,",b[i]);
printf("\n\n");
for(i=0;i<10;i++)
{ if(b[i]==0)
  continue;
  t=b[i];
  for(j=0;j<t;j++)
  {a[k]=i;
   k++;
  }
}
for(i=0;i<8;i++)
printf("%d,",a[i]);
printf("\n\n");
/*Time complexity 3n*/
return(0);
}


HeapSort

#include<stdio.h>
int main()
{
int i,a[]={0,68,-7,45,6,57},n=6,t,k,l;
int b[n+1];
int left,right,pos,j,parent;

for(i=0;i<6;i++)
{
b[i+1]=a[i];
j=i+1;
while(1)
{
parent=j/2;
if(parent<1)
break;
if(b[parent]>=b[j])
break;
t=b[parent];
b[parent]=b[j];
b[j]=t;
j=parent;
}
}
for(i=1;i<7;i++)
printf("%d,",b[i]);
printf("\n\n");


for(i=1;i<=n;i++)
{
a[n-i]=b[1];
l=n-i+1;
b[1]=b[l];
l--;
j=1;
while(1)
{
left=2*j;
right=2*j+1;
if(left>l)
break;
if(right>l)
{
if(b[j]>b[left])
break;
t=b[j];
b[j]=b[left];
b[left]=t;
break;
}
if(b[left]>b[right])
pos=left;
else
pos=right;
if(b[j]>b[pos])
break;
t=b[pos];
b[pos]=b[j];
b[j]=t;
j=pos;
}
}

for(i=0;i<6;i++)
printf("%d,",a[i]);
printf("\n\n");

/*Time complexity nlog(base 2)n*/
/*For Quick and Merge too.*/
return(0);
}
InsertionSort

#include<stdio.h>
int main()
{

int i,j,t,a[]={5,9,3,0,1},n=5;
for(i=0;i<=n-2;i++)
{
  if(a[i]<=a[i+1])
  continue;
 
  j=i+1;
  t=a[j];
  while(j>=1 && a[j-1]>t)
  { a[j]=a[j-1];
    j--;
  }
  a[j]=t;
}
for(i=0;i<n;i++)
printf("%d,",a[i]);
return(0);
}

MergeSort

#include<stdio.h>

void mergesort(int a[],int left,int right);
void main()
{
int i;
int a[]={9,6,8,2,5,90,-8,4,1,10};
mergesort(a,0,9);
for(i=0;i<10;i++)
printf("%d,",a[i]);
printf("\n\n");
}
void mergesort(int a[],int left,int right)
{
int c[10];
int p,mid,i,j,k;
if(left>=right)
return;
mid=(left+right)/2;
mergesort(a,left,mid);
mergesort(a,mid+1,right);
i=left;
j=mid+1;
k=0;
while(i<=mid && j<=right)
{
if(a[i]<=a[j])
{
c[k]=a[i];
i++;
}
else
{
c[k]=a[j];
j++;
}
k++;
}
if(i<=mid)
for(p=i;p<=mid;p++)
{
c[k]=a[p];
k++;
}
else
for(p=j;p<=right;p++)
{
c[k]=a[p];
k++;
}

for(i=left;i<=right;i++)
a[i]=c[i-left];


printf("\nleft=%d  right=%d\n",left,right);

for(i=0;i<10;i++)
printf("%d,",a[i]);
printf("\n\n");
}
QuickSort

#include<stdio.h>
void quicksort(int a[],int left,int right);
int main()
{
int i,fp,pivot,t,a[]={9,1,17,3,10,-8,5},n=7;
quicksort(a,0,n-1);
for(i=0;i<n;i++)
printf("%d,",a[i]);
return(0);
}
void quicksort(int a[],int left,int right)
{
int i,fp,pivot,t;
if(left>=right)
return;
fp=left;
pivot=a[left];

for(i=left + 1;i<=right;i++)
{
if(a[i]>=pivot)
continue;

fp++;
t=a[i];
a[i]=a[fp];
a[fp]=t;

}
t=a[fp];
a[fp]=pivot;
a[left]=t;
quicksort(a,left,fp-1);
quicksort(a,fp+1,right);
}
SelecionSort

#include<stdio.h>
int main()
{
int i,j,smallpos,a[]={3,9,7,1,4},t;
for(i=0;i<1;i++)
{
  smallpos=i;
  for(j=i+1;j<=4;j++)
  if(a[j]<a[smallpos])
 
  smallpos=j;
  t=a[smallpos];
  a[smallpos]=a[i];
  a[i]=t;
  a[0]=a[i];
 
}
printf("%d",a[0]);
return(0);
}

Detecting mouse positions through JavaScript



<script type="text/javascript">
document.onmousemove=f1;
document.onmousedown=f2;
document.onmouseup=f3;
function f1(event)
{
  var mouseX = event.clientX + document.body.scrollLeft;
      var mouseY = event.clientY + document.body.scrollTop;
document.getElementById("currentx").value=mouseX;
document.getElementById("currenty").value=mouseY;
}


function f2(event)
{
  var mouseX = event.clientX + document.body.scrollLeft;
      var mouseY = event.clientY + document.body.scrollTop;
document.getElementById("downx").value=mouseX;
document.getElementById("downy").value=mouseY;
}


function f3(event)
{
  var mouseX = event.clientX + document.body.scrollLeft;
      var mouseY = event.clientY + document.body.scrollTop;
document.getElementById("upx").value=mouseX;
document.getElementById("upy").value=mouseY;
}
</script>
<table width="100%" border="2">
<tr><td colspan="4" align="center">Mouse Data</td></tr>
<tr><td>Current Mouse X</td><td>
<input type="text" id="currentx"/>
</td><td>Current Mouse Y</td><td>
<input type="text" id="currenty"/>
</td></tr>



<tr><td>Down Mouse X</td><td>
<input type="text" id="downx"/>
</td><td>Down Mouse Y</td><td>
<input type="text" id="downy"/>
</td></tr>



<tr><td>Up Mouse X</td><td>
<input type="text" id="upx"/>
</td><td>Up Mouse Y</td><td>
<input type="text" id="upy"/>
</td></tr>
</table>



Saturday, June 9, 2012

Methods of Console IO in Java

In this Post we shall study the methods of Console IO in Java. We shall examine 4 different methods for doing so.However, first of all we require some background information:-

The Standard Input, Standard Output & Standard Error

As , we all know every process is given three open streams by the operating system. Namely , Standard Input,Standard Output and Standard Error.

Standard Input:- The standard input is known as stdin in C, cin in C++ and System.in in Java. By default it is the keyboard but it can be reassigned. System.in is an object of type java.io.InputStream and provides a method read(), which reads the next int from the input source. It returns -1 on End Of File (EOF). From the keyboard we can simulate EOF by pressing CTRL + Z .This will show ^Z on the screen.

Standard Output:- The standard output is known as stdout in C,cout in C++ and System.out in Java. By default it is the monitor but it can be redirected. It is represented in Java by an object of type java.io.PrintStream.

Standard Error:- The standard error is known as stderr in C,cerr in C++ and System.err in Java. By default it is the monitor but it can be redirected. It is represented in Java by an object of type java.io.PrintStream.


Reading Input Directly from the Standard Input using System.in

DirectIO.java

public class DirectIO
{

public static void main(String[] args)
{
//Enter the testing code here
}
public static int readInt()
{
return Integer.parseInt(readLine());
}



public static String readString()
{
try
{
String s="";
while(true)
{
int n=System.in.read();
if(n==-1 || n==13 || n==' ')
return s.trim();
char ch=(char)n;
s=s+ch;
}
}
catch(Exception ex)
{
return "";
}
}









public static String readLine()
{
try
{
String s="";
while(true)
{
int n=System.in.read();
if(n==-1 || n==13)
return s.trim();
char ch=(char)n;
s=s+ch;
}
}
catch(Exception ex)
{
return "";
}
}
}

Explanation: In the the readLine and readString methods we simply read the next input from System.in. This is an int and is stored in n. If n is 13 we have <Enter> being pressed , while n==-1 means an EOF. In both cases we return the current Line. Till these conditions are met , we simply append the next character to the input.
For readString, we also check for the space character ' '.

For readInt, we simply convert the already read Line or String to the required type.

For reading and printing a line use:-
String str=readLine();
System.out.println(str);
For reading and printing an int use:-
int str=readInt();
System.out.println(str);

For reading and printing a Stringuse:-
String str=readString();
System.out.println(str);
For reading and printing a list of Strings :-


String str=readString();
while(str!="")
{
System.out.println(str);
str=readString();
}




Using the java.util.Scanner class
The Scanner class in java.util is a very versatile instrument for text input. It can read all types of data , and from a variety of data sources.The Scanner essentially has two groups of methods:-



1) The hasNextType group. This group returns true if some input of the given type is available and returns false otherwise.
Some functions are hasNext for Objects, hasNextLine for Lines,hasNextInt for integers, and so on.


2) The hasType group. This group returns the next type element from the input source.Some examples are:-

next reads Object, nextLine reads Line, nextInt reads Integers and so on.

Here is some sample code:-

import java.util.Scanner;
public class ScannerIO
{
public static void main( String[] args)
{
Scanner s=new Scanner(System.in);
int a =s.nextInt();
System.out.println(a);
}
}


To be continued...



Saturday, June 2, 2012

Drawing Pyramids


#include<stdio.h>

#include<conio.h>

void main()

{

int n=5,i,j,k;

clrscr();

for(i=1;i<=n;i++)

{

for(j=1;j<=n-1;j++)

printf(" ");

for(k=1;k<=i;k++)

printf("*");

printf("\n");

}

getch();

}

#include<stdio.h>
#include<conio.h>
void main()
{
int n=5,i,j,k;
clrscr();
for(i=1;i<=n;i++)
{
for(j=1;j<=0;j++)
printf(" ");
for(k=1;k<=i;k++)
printf("*");
printf("\n");
}
getch();
}



#include<stdio.h>
#include<conio.h>
void main()
{
int n=5,i,j,k;
clrscr();
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
printf(" ");
for(k=1;k<=2*i-1;k++)
printf("*");
printf("\n");
}
getch();
}

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n=5,i,j,k;
int x,y;
clrscr();
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
printf(" ");
for(k=1;k<=2*i-1;k++)
{
x=i-abs(i-k);
printf("%d",x);
}
printf("\n");
}
getch();
}









#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n=5,i,j,k;
int x,y;
clrscr();
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
printf(" ");
for(k=1;k<=2*i-1;k++)
{
x=i-abs(i-k);
printf("%c",'A' + x -1);
}
printf("\n");
}
getch();
}



#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n=5,i,j,k;
int x,y;
clrscr();
for(i=n;i>=1;i--)
{
for(j=1;j<=n-i;j++)
printf(" ");
for(k=1;k<=2*i-1;k++)
{
x=i-abs(i-k);
printf("%c",'A' + x -1);
}
printf("\n");
}
getch();
}


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n=5,i,j,k;
clrscr();
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
printf(" ");
for(k=1;k<=i;k++)
{
printf("* ");
}
printf("\n");
}
getch();
}



#include<stdio.h>
#include<conio.h>
#include<math.h>
int pascalvalue(int y,int x);
void main()
{
int n=5,i,j,k;
clrscr();
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
printf(" ");
for(k=1;k<=i;k++)
{
printf("%d ",pascalvalue(i,k));
}
printf("\n");
}
getch();
}

int pascalvalue(int y,int x)
{
if(x==1)
return(1);
if(x==y)
return(1);
return(pascalvalue(y-1,x-1) + pascalvalue(y-1,x));
}
#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
int n=5,i,j,k;
int mid=(n+1)/2,x,y;
clrscr();
for(i=1;i<=n;i++)
{
x=abs(mid-i);
y=mid-x;
for(j=1;j<=y;j++)
printf(" ");
for(k=1;k<=2*x+1;k++)
printf("*");
printf("\n");
}
getch();
}



#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
int n=5,i,j,k;
int mid=(n+1)/2,x,y,z;
clrscr();
for(i=1;i<=n;i++)
{
x=abs(mid-i);
y=mid-x;
for(j=1;j<=y;j++)
printf(" ");
for(k=1;k<=2*x+1;k++)
{
z=x-abs(x+1-k)+1;
printf("%d",z);
}
printf("\n");
}
getch();
}



#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
int n=5,i,j,k;
int mid=(n+1)/2,x,y,z;
clrscr();
for(i=1;i<=n;i++)
{
x=abs(mid-i);
y=mid-x;
for(j=1;j<=y;j++)
printf(" ");
for(k=1;k<=2*x+1;k++)
{
z=x-abs(x+1-k);
printf("%c",'A' + z);
}
printf("\n");
}
getch();
}

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
int n=5,i,j,k;
int mid=(n+1)/2,x,y,l,r;
clrscr();
for(i=1;i<=n;i++)
{
x=abs(mid-i)+1;
y=mid-x;
for(j=1;j<=n;j++)
{
l=j;
r=n+1-j;
if(j<=x || r<=x)
printf("*");
else
printf(" ");
}
printf("\n");
}
getch();
}
#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
int n=11,i,j,k;
int mid=(n+1)/2,x,y,l,r,z;
clrscr();
for(i=1;i<=n;i++)
{
x=abs(mid-i)+1;
y=mid-x;
for(j=1;j<=n;j++)
{
l=j;
r=n+1-j;
z=abs(mid-j);
if(j<=x || r<=x)
printf("%d",z);
else
printf(" ");
}
printf("\n");
}
getch();
}
#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
int n=11,i,j,k;
int mid=(n+1)/2,x,y,l,r,z;
clrscr();
for(i=1;i<=n;i++)
{
x=abs(mid-i)+1;
y=mid-x;
for(j=1;j<=n;j++)
{
l=j;
r=n+1-j;
z=mid-abs(mid-j)-1;
if(j<=x || r<=x)
printf("%c",'A' + z);
else
printf(" ");
}
printf("\n");
}
getch();
}

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define n 4
void main()
{

int l=0,r=n-1,t=0,b=n-1,i,x=1,j;
int a[n][n];
clrscr();
while(x<=n*n)
{
for(i=l;i<=r;i++)
a[t][i]=x++;
t++;
for(i=t;i<=b;i++)
a[i][r]=x++;
r--;
for(i=r;i>=l;i--)
a[b][i]=x++;
b--;
for(i=b;i>=t;i--)
a[i][l]=x++;
l++;
}
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
printf("%4d",a[i][j]);
printf("\n");
}
getch();
}

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define n 4
void main()
{

int l=0,r=n-1,t=0,b=n-1,i,x=1,j;
int a[n][n];
clrscr();
while(x<=n*n)
{
for(i=l;i<=r;i++)
a[t][i]=x++;
t++;
for(i=t;i<=b;i++)
a[i][r]=x++;
r--;
for(i=r;i>=l;i--)
a[b][i]=x++;
b--;
for(i=b;i>=t;i--)
a[i][l]=x++;
l++;
}
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
printf("%4d",n*n + 1-a[i][j]);
printf("\n");
}
getch();
}