Home
| | Sitemap
||Page number :29
Operations on Array
Many operations can be performed on elements in an array. According to the scope of syllabus we will discuss some of them here ---
- Searching(Linear, Binary)
- Sorting(Selection, Bubble)
Searching
We can easily search for any content in an array. Let us suppose that our array has four integers and we want to find out if one of them is ‘6’. Searching will serve the purpose.
1st Approach
The first approach is to compare each and every element of the array with the number being searched. Say we have an array with elements --- 4, 5, 2, 0, 3 Searched Number- 6 We will start the searching from index zero which contains the value four, the process will continue till the limit of Array (4) is reached.
Actual Program – LINEAR SEARCH
import java.io.*; class linsrch { public static void main(String args[]) throws IOException { int arr[]={5,2,7,8,1,0}; int c,no; boolean fnd=false; InputStreamReader rd=new InputStreamReader(System.in); BufferedReader inp=new BufferedReader(rd);
System.out.println("Enter a number to Search"); String v1=inp.readLine(); no=Integer.parseInt(v1);
for(c=0;c<6;c++) { if(no==arr[c]) { System.out.println("Successful Search"); fnd=true; } }
if(fnd==false) System.out.println("Requested Number Not Found!!"); } }
2nd Approach
The second approach of searching is a bit more complicated than the first one. This scheme of searching can only be applied on a sorted array (An array with elements arranged either in ascending or descending order). The basic concept is --
An array num[] ={4, 9, 11, 13, 15, 19}; Imagine that you want to know if this array “num” contains the digit ‘9’. Follow these steps to search for the number --- Have two variables, low and high. Variable low should be initialized with zero and high with the array’s limit (Use the function array_name.length).
- Sum up the lower and upper limit. (In this case sum=6).
- Divide it by two. (Here the answer is 3).
- Check the value in the array’s index. (Here it is 13)
- If the searched number is greater than the number in the index, set the low variable to the index number. If the searched number is smaller (Just like our present example) set the upper bound to the index.
- Repeat the process till the number is found.
Actual Program --- Binary Search
import java.io.*; class binsrch { public static void main(String args[]) throws IOException { int arr[]={3,5,6,9,10}; int no; int low=0,up=arr.length,mid; boolean fnd=false; int add=0; InputStreamReader rd=new InputStreamReader(System.in); BufferedReader inp=new BufferedReader(rd);
System.out.println("Enter a number"); String v1=inp.readLine(); no=Integer.parseInt(v1);
while(low<=up) { mid=(low+up)/2; if(no==arr[mid]) { fnd=true;
add=mid; break; } else if(no>arr[mid]) low=mid+1; else up=mid-1; }
if(fnd==true) { System.out.println("Search Successful!!"); System.out.println("Index No. " +add); } else System.out.println("Search Unsuccessful!!"); } }
Sorting
The word “sorting” means to arrange systematically in a certain order, imagine a telephone directory that contains names in an alphabetically sorted way. Here we will study to sort numbers in increasing and decreasing order.
1st Approach
The first approach to sorting is the “selection sort”. In this technique the smallest number is checked and placed at the first index. The checking is again done in between the rest of the numbers and the smallest number is calculated. If we are sorting the array in descending order, the largest number is calculated.
Actual Program ---
import java.io.*; class selsort { public static void main(String args[]) throws IOException { InputStreamReader reader=new InputStreamReader(System.in); BufferedReader input=new BufferedReader (reader); { int k,n,temp=0,c,j; int ever[]=new int[150]; System.out.println("Enter the Limit of Array"); String v1=input.readLine(); n=Integer.parseInt(v1); for(c=0;c<n;c++) { System.out.println("Enter the Element to sort "); String v2=input.readLine(); ever[c]=Integer.parseInt(v2); } for(k=0;k<n ;k++) { for(j=k+1;j<n;j++) { if(ever[j]<ever[k]) { temp=ever[k]; ever[k]=ever[j]; ever[j]=temp; } } } for(c=0;c<n;c++) { System.out.println("Array after sorting is"+ever[c]); } } } }
The Logic Understanding logic behind a program is the most important part. Most of the students escape this step and finally mug up the programs. Never try to do this.
The main code in the whole program is ---
for(k=0;k<n ;k++) { for(j=k+1;j<n;j++) { if(ever[j]<ever[k]) { temp=ever[k]; ever[k]=ever[j]; ever[j]=temp; } } }
Look at the structure of the code. We have two ‘for’ loops, an ‘if’ condition, some cryptic statements inside the block.
The two loops --The inner nested loop compares an element with other elements. The outer loop repeats this step for each an every element.
‘if ‘condition The ‘if’ condition checks, whether the number in the first index is the smallest.
Statements inside ‘if’ block The statements inside the ‘if’ block are executed and help us to swap the values. In this process we have to take a temporary variable.
2nd Approach
The second approach to sorting is known as bubble sort. In this sorting technique an element is compared with the next element.
//A program to sort an array using bubble the sort technique import java.io.*; class bub { public static void main(String args[]) throws IOException { InputStreamReader reader=new InputStreamReader(System.in); BufferedReader input=new BufferedReader (reader); { int i,k=0,j,n,temp=0; int a[]=new int[150]; System.out.println("Enter the Limit of Array"); String v1=input.readLine(); n=Integer.parseInt(v1); for(i=0;i<n;i++) { System.out.println("Enter the Element to sort "); String v2=input.readLine(); a[i]=Integer.parseInt(v2); } i=0; for(k=0;k<n;k++) { for(j=0;j<n-1;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } for(i=0;i<n;i++) { System.out.println(+a[i]); } } } }
In a quick glance both of the sorting techniques might appear to be same but there is a slight difference which is quite significant. The general use of loops, if statement and the statements inside the ‘if’ block remain same.
Q1. Explain arrays.
Q2. State the difference between Single or Multidimensional array.
Q3. Why loops are are used to take input?
Q4. State the difference between String and arrays of character.
Q5. Explain the two types of searching with their algorithm.
Q6. What do you understand by the term sorting?
Q7. State the significance of subscript in arrays.
page 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29