0% found this document useful (0 votes)
2 views4 pages

Arrayprogramsnew

Uploaded by

rithvika.d
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Arrayprogramsnew

Uploaded by

rithvika.d
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

ARRAY PROGRAMS

1. Sum and average


class sumaverage
{
public sum(int a[])
{
double s=0;
for(int i=0;i<a.length;i++)
s=s+a[i];
double avg=s/a.length;
System.out.println("sum="+s);
System.out.println("average"+avg);
}}

Minimum and maximum


class min
{
public min(int a[])
{
int max=a[0];
for(int i=0;i<a.length;i++)
{if (max<a[i])
max=a[i];
}
int min=a[0];
for(int i=0;i<a.length;i++)
{if (min>a[i])
min=a[i];
}
System.out.println("minimum"+min);
System.out.println("maximum"+max);
}}
2. Bubble sort
class bubble
{
public bubble(int a[])
{
int temp=0;
for(int i=0;i<a.length-1;i++)
{
for(int j=0;j<a.length-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp; }}}
for(int i=0;i<a.length;i++)
System.out.println(a[i]); }}
3. selection sort
class sum
{
public sum(int a[])
{
int temp=0,small=0,pos=0;
for(int i=0;i<a.length;i++)
{
small=a[i];
pos=i;
for(int j=i+1;j<a.length;j++)
{
if(a[j]<small)
{
small=a[j];
pos=j;
}}
temp=a[i];
a[i]=a[pos];
a[pos]=temp;
}
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}}
4. Binary Search
class binary
{
public binary(int a[],int n)
{
int k=0,m=0;
int l=0,u=a.length-1;
while(l<=u)
{
m=(l+u)/2;
if(n<a[m])
u=m-1;
else if(n>a[m])
l=m+1;
else
{
k=1;
break;
}
}
if(k==1)
System.out.println("element found "+(m+1));
else
System.out.println("element not found");
}}
5. Linear search
class linear
{
public linear(int a[],int n)
{
int k=0;
for(int i=0;i<a.length;i++)
{
if(a[i]==n)
{
k=1;
break;
}}
if(k==1)
System.out.println("element found");
else
System.out.println("element not found");}}

6. Merge
class sum
{
public sum(int a[],int b[])
{
int c[]=new int[a.length+b.length];
for(int i=0;i<a.length;i++)
c[i]=a[i];
for(int j=0;j<b.length;j++)
c[a.length+j]=b[j];
for(int i=0;i<c.length;i++)
System.out.println(c[i]);
}}
7. 2 SDA [Arranging name and marks using bubble sort]

class correspondingelement
{
public correspondingelement (int a[],String b[])
{
int temp=0;String t=””
for(int i=0;i<b.length-1;i++)
{
for(int j=0;j<b.length-i-1;j++)
{
if(b[j].compareTo(b[j+1])>0)
{
t=b[j];
b[j]=b[j+1];
b[j+1]=t; // two swaps for corresponding elements

temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;

}}}
for(int i=0;i<b.length;i++)
System.out.println(b[j]+” “+a[i]);
}}

8. pascal’s Triangle
class pascal
{
public pascal(int n)
{
int m[]=new int[20];
m[0]=1;
for(int i=0;i<n;i++)
{
for(int j=0;j<=i;j++)
System.out.print(m[j]+" ");

System.out.println();
for(int j=i+1;j>0;j--)
m[j]=m[j]+m[j-1];
}
}}

You might also like