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

Single Array Programs

The document contains Java code examples for working with one-dimensional arrays, including user input, displaying elements, and implementing linear and binary search algorithms. It demonstrates how to declare, initialize, and manipulate arrays, as well as how to search for elements using both search methods. Each section includes a main method to execute the functionality of the respective classes.

Uploaded by

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

Single Array Programs

The document contains Java code examples for working with one-dimensional arrays, including user input, displaying elements, and implementing linear and binary search algorithms. It demonstrates how to declare, initialize, and manipulate arrays, as well as how to search for elements using both search methods. Each section includes a main method to execute the functionality of the respective classes.

Uploaded by

a student
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SCM 6: Arrays 1 D array

1./**one dimensional array


*/
import [Link].*;
public class oneDarray
{
public static void main()
{
int a[]=new int[5]; //declaring an integer array of size 5
int b[]={10,20,30,40,50}; // declaring and initializing simulatenously
[Link]("Elements in array b are:");
for(int i=0;i<[Link];i++)
{
[Link]("Element at index " +i+ " is "+b[i]);
}
// accepting array elements from user for array a
Scanner sc=new Scanner([Link]);
[Link]("Enter elements for array a");
for(int j=0;j<[Link];j++)
{
a[j]=[Link](); //accepting array elements
}
[Link]("Elements in array a are:");
for(int i=0;i<[Link];i++)
{
[Link]("Element at index " +i+ " is "+a[i]);
}
}
}

2. import [Link].*;
/**
Linear Search OOP based
*/
public class LinearSearch
{
int arr[];//instance variable
int size;
LinearSearch(int k)
{
size=k;
arr=new int[size];
}
public void input()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter elements for array ");
for(int j=0;j<[Link];j++)
{
arr[j]=[Link](); //accepting array elements
}
}
public void display()
{
[Link]("Elements in array a are:");
for(int i=0;i<[Link];i++)
{
[Link](arr[i]+"\t");
}
[Link]();
}
public void search(int r)
{
int flag=0;//element not found
int i;
for(i=0;i<[Link];i++)
{
if(arr[i]==r)
{
flag=1;//element found
break;
}
}
if(flag==1)
[Link]("Element "+r+ " found at index "+i);
else
[Link]("Element "+r+" not found ");
}
public static void main()
{
Scanner sc=new Scanner([Link]);
LinearSearch obj=new LinearSearch(8);
[Link]();
[Link]();
[Link]("Enter the element you want to search");
int s=[Link]();
[Link](s);
}
}

3. /** Binary Search OOP based


*/
import [Link].*;
public class BinarySearch
{
int arr[];
int size;

BinarySearch(int k)
{
size=k;
arr=new int[size];
}
public void input()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter elements for array in sorted order ");
for(int j=0;j<[Link];j++)
{
arr[j]=[Link](); //accepting array elements
}
}
public void display()
{
[Link]("Elements in array a are:");
for(int i=0;i<[Link];i++)
{
[Link](arr[i]+"\t");
}
[Link]();
}

void search(int r)
{
int len=[Link];
int l=0;
int h=len-1;
int mid;
int index=-1;// element not found
while(l<=h)
{
mid=(l+h)/2;
if(arr[mid]>r)
h=mid-1;
else if(arr[mid]<r)
l=mid+1;
else
{
index=mid;
break;
}
}
if (index == -1)
[Link](r+ " not present in array");
else
[Link](r + " found at index " + index);
}

public static void main()


{
Scanner sc=new Scanner([Link]);
BinarySearch obj=new BinarySearch(8);
[Link]();
[Link]();
[Link]("Enter the element you want to search");
int s=[Link]();
[Link](s);
}
}

You might also like