Array Worksheet (1)
Array Worksheet (1)
13. Write a program to input an array of 50 different integers (including negative and positive) and display the
elements in such a way that the negative elements should be printed first followed by the positive elements.
(Consider zero (0) as positive).
****************************************************************************
Worksheet – Array (Searching)
Simple Searching (Linear Search): In this method each element is compared with the value to be searched in a
sequential manner. The search is also termed as sequential search. Since each element is checked the searching
speed becomes also for arrays with large number of elements.
class simple_search
{
public void demo()
{
double[] data={2,14,5,6,7,3,4};
int target=4;
int pos=0;
for(int i=0;i<7;i++)
{
if(data[i]==target)
{
pos=i+1;
}
}
if(pos>0)
System.out.println("The position of the given number is : " +pos);
else
System.out.println("Not found");
}
}
Binary Search : In this method the array element which is passed should be in sorted format. The middle
element of the sorted array is found out, it is checked with the value to be searched.
a) if search value is greater then middle element then left side elements are ignored from search process.
b) if search value is less then middle element then right side elements are ignored from search process.
c) if search value is equal the middle element then position of elements we are searching is determined.
If element is not found step a) or b) or c) are repeated till the element is found.
Linear Search Binary Search
1. The array elements need not be in a sorted The array elements should be in a sorted
format. format.
2. It checks for each element in the array, so In this process most of the elements are
searching process becomes slow when skipped during the searching process so binary
records are many. search is fast.
import java.util.*;
public class array
{
public static void main( )
{
int first, last, mid;
int arr[]=new int[5];
Scanner sc=new Scanner(System.in);
System.out.println("ENTER 5 NUMBERS IN ASCENDING ORDER :");
for(int i=0;i<5;i++)
{
arr[i]=sc.nextInt();
}
last=4;
first=0;
System.out.println("Enter a number to search in the list : ");
int value=sc.nextInt();
mid=(first+last)/2;
while(value!=arr[mid])
{
if(value<arr[mid])
last=mid-1;
if(value>arr[mid])
first=mid+1;
mid=(first+last)/2;
}
System.out.println("Position is of the given no. is : " + (mid+1));
}
}
***************************************
1. Write a program to initialize an array of 5 names and initialize another array with their respective
telephone numbers. Search for a name input by the User, in the list. If found, display "Search
Successful" and print the name along with the telephone number, otherwise display "Search
unsuccessful. Name not enlisted".
2. Write a program to accept the names of 10 cities in a single dimension string array and their STD
(Subscriber Trunk Dialing) codes in another single dimension integer array. Search for a name of a city
input by the user in the list. If found, display “Search Successful” and print the name of the city along
with its STD code, or else display the message “Search Unsuccessful. No such city in the list”.
3. Write a program to accept a number which to be search form the following array using binary search
technique.
4 7 11 18 29 45 71 80 87 93 96 99
4. Write a program to accept the year of graduation from school as an integer value from the user. Using
the Binary Search technique on the sorted array of integers given below, output the message “Record
exists” if the value input is located in the array. If not, output the message “Record does not exist”.
{1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010}
5. Write a program to perform binary search on a list of integers given below, to search for an element
input by the user, if it is found display the element along with its position, otherwise display the message
“Search element not found” 5,7,9,11,15,20,30,45,89,97
import java.util.Scanner;