Académique Documents
Professionnel Documents
Culture Documents
}
}
Output of the above program :
System.out.print(m[i][j]+ );
}
System.out.println();
}
}
}
Output of the above program :
Searching is a process in which the entered value is checked with the already present values to find out whether
the entered value is present in the array.
Two types of Searching Methods ,
(i).Linear Searching :
It is the method in which the the elements present in the array are checked from 0th position with the value
entered by the user , the checking starts from 0th position, if the element is present in the array , it results as
Search Successful else
it results as Search Unsuccessful
Example Program on Linear Searching ,
import java.util.*;
class LinearSearch
{
static void test(int m[])
{
int a=0;
Scanner in=new Scanner(System.in);
System.out.println(The Array obtained is : );
for(int k=0;k<m.length;k++)
{
System.out.println(m[k]);
}
System.out.print(Enter the number which is to be searched :);
int c=in.nextInt();
boolean isPresent = false;
for(int j=0;j<m.length;j++)
{
if(c==m[j])
a++;
}
if(a>=1)
System.out.println(Search Successful );
else
System.out.println(Search Unsuccessful );
}
}
Input ,
Entered array = {15,154,48,48,48}
import java.util.*;
class LinearSearch
{
static void test(int m[])
{
Scanner in = new Scanner(System.in);
System.out.println(Elements of the Array are : );
for(int i=0;i<m.length;i++)
System.out.print(m[i]+ );
System.out.println(\n\nEnter the number to be searched );
int c = in.nextInt();
int a = 0;
for(int j=0;j<m.length;j++)
{
if(c==m[j])
{
a++;
}
}
int[] s=new int[a];
int k=0;
for(int j=0;j<m.length;j++)
{
if(c==m[j])
{
a++;
s[k]=j;
k++;
}
}
if(a>=1)
{
System.out.print(\n\nSearch successful, +c+ is present at : );
for(int i=0;i<s.length;i++)
System.out.print(s[i]+ , );
System.out.print(Positions(s) );
}
else
System.out.println(\n\nSearch unsuccessful ! );
}
}
(ii)Binary Searching :
Binary search can only be applied to array whose elements are in either or descending order.
let us , take the array {4,7,8,10,14,21,22,36,62,77,81,91}
Here , there are 11 elements in the array , the process that happens in binary search is :
Step 1, The mid term of the array gets calculated , it will be calculated as (first element + last element)/2 and
based on the mid term , the array gets divided into two parts.
6
Step 2, The value entered by the user gets compared with the mid term let us say, the user entered 22 , now, the
mid term i.e., 21<22 therefore, the search goes to the second half(greater half)
Step 3 , Then , the first half again gets divided into two halves by calculating the mid term of the first half last
2= mid-1 ; mid2=first+last/2
Step 4 , if the value entered by the user matches with any of the element of the array , then the search is said to
be successful , else
the search is said to be unsuccessful.
For better understanding , refer the image below ,
break;
}
else
last = middle 1;
middle = (first + last)/2;
}
if ( first > last )
System.out.println(search + is not present in the list.\n);
}
}
2.Sorting
Sorting is a process in which the elements of the array are either arranged in ascending or descending order.
Two methods of sorting in JAVA ,
(i)Selection Sort :
It is the method in which the control starts from 0th position and it checks for the lowest value , and replaces it
with the value present at the 0th position of the array , in such a way it replaces the elements which are not in
order , and thus results in ascending order.
Consider the array {5,1,12,-5,16,2,12,14} ,
Look at the image for better understanding
Finally ,
is the result