What is Meant by an Array
What is Meant by an Array
Example : Consider :
for(int i=1;i<=10;i++)
int m = Integer.parseInt(in.readLine());
In the above Java statements , we can input the value of the variable ‘m’ for 10 times
using the for loop as shown , but as soon as the next value of the variable ‘m’ is
entered by the user the previous value of the ‘m’ gets deleted , therefore , If we want
to calculate the sum of the 10 values entered by the user , it is not very easy , therefore
, to perform this activity , we can use Arrays to make it easier and simple to avoid
confusion for better programming.
OR
EX : int l = m.length //here ‘m’ is the array and l is the length is the array.
m[0] = 12
m[1] = 58
m[2] = 69
m[3] = 87
m[4] = 45
m[5] = 78
m[6] = 25
m[7] = 36
Types of Arrays :
1.Single Dimensional array
In the above statement we have declared the array ‘m’ , in the method ‘teja’ of the
program.
Example Program to input the elements of an array through Bluej
Mehod :
class Arrays_Bluejmthd
{
static void test(int m[])
{
System.out.println(“The Entered Array is : “);
for(int i=0;i<m.length;i++)
{
System.out.print(m[i]+” “);
}
}
}
Input :
Input,
Output of the Program ,
Do check out the video tutorial on Searching, at the end of this topic.
Here is the modified program of Linear Search which also shows the place where the
search item was present !! :
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.
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.
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
class BinarySearch
{
public static void main()
{
int c, first, last, middle, n, search, array[];
Scanner in = new Scanner(System.in);
System.out.println(“Enter number of elements”);
n = in.nextInt();
array = new int[n];
System.out.println(“Enter ” + n + ” integers”);
for (c = 0; c < n; c++)
array[c] = in.nextInt();
System.out.println(“Array entered is : “);
for(int i=0;i<array.length;i++)
System.out.print(array[i]+” “);
System.out.println(“\nEnter value to find”);
search = in.nextInt();
first = 0;
last = n – 1;
middle = (first + last)/2;
while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
System.out.println(search + ” found at location ” + (middle + 1) + “.”);
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.