We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 31
y InitiArray
tatic void main()
wowels{} = ('a', 19
int i= 0; ics,
++)
+ loop, elements of the array are printed
jis used to iterate over the vowels array. inside the body of the for
passed to the vowelsfi
by using the index. When the loop executes first time, the value of is. 0. This value is
value of array is printed. Similarly, all the values are printed.
‘of an array means the total number of elements the array can store. To find it, 2 attribute named length is
‘Syntax to find the length of an array:
yof_array. length:
the example. aie
array_length
public static void main()
bat
int ar()=new int (2017
ant lent te fed Leyte
("Length of array is: " + len);
Speen. out-printin
|
aa. =4
Scanned with CamScannerScanned with CamScannerstatic void main()
pt arr{] = new int (5);
aie <0, 42
ESC = new Scanner (System. in) ;
bout -Println ("Enter ‘five values for array: ")7
Boe PORRE = 05 count: <5) \countes) i x ee Atos 4
arr(count] = sc.nextint();
HE = 02 1 < arr.tengen; iss) 9 fx dose
ithe preceding program, it will ask to enter array elements from runtime. After entering the values, wh
Enter key, following output will appear:
for array:Scanned with CamScannertinear Search <<
‘search is the simplest technique of searching. in this technique,
of the array and will traverse till the end. If the element is found, the searching will stop,
‘the last and then stop.
Let us take an example, in which the number to be searched Is 14:
‘oth
Jing of an item will begin from the
egies else will continue till
Stepi:arlOlie 10 == 14: false
step2:arliJie.21 == 14: false
step3: arf2}ie.24 == 14: false
Step 4: arl4jie.14 9 == 14 : true : break from loop
This process of searching is also known as sequential searching as it searches an item in an array in 2 sequentis
manner either top to bottom or vice versa. The element to be searched Is mapped with each element of the array
When the element is matched, searching stops.
Binary Search —~~
In this technique, an array has to be arranged in ascending or descending order. The sorted array {in ascending orde
has to be divided into two equal halves. Then the element to be searched is checked with the middle element
it matches, then the loop breaks, else it checks whether the searched element is larger or smaller than the midd
element. if it is a smaller than the middle element, then the left side is again divided into two halves and the proce
continues. Similarly, if the searched element is larger than the middle element, in the right half the process continue
Binary search is also known as half-interval search.
Let us take an example, in which the element to be searched is 44:
het
Middle element of the array is = (First Index + Last Index)/2 = (0 + 4)/2 = 2. The element of the 2” position will be
middle element of the array which 24.
‘Now, check whether the element to be searched 44 is greater than middle element of the array 24. If it is true, th
has been concluded that the element to be searched is in the right half of the array. If it is false, then the eleme
be searched is in the left half of the array.
Step 2: Again, find the middle element of the right half of the array.
Scanned with CamScanner=
ment. In this case, 44 =~ 4/1
Step 3: Check whether the element to be searched is greater than to middle element: In M9 Lea
Thus, the searched element is found and the searching stopped:
Difference Between Linear Search and Binary Search cc
‘on sorted arrays.
Rhwarks on both sorted and unsorted arrays Se ceding for the ld comer a
ft starts searching from Oth position. Jther to the left or to the right depending on
the value of the searchedelement.
15.5.2 Sorting as
ne {s the process of arranging the elements an array in a sequential ne eer teacending OF descending ore,
‘According to syllabus, there are two types of sorting: bubble sort and select .
Bubble Sort .
in this technique, the consecutive elements are checked from the beginning, If it is to be searched in @SCENdiNg Orde,
then it is to be checked that the previous elements larger than the next element. Ifitis so, the elements in the POSition
are swapped (interchanged). And this process continues.
Let us take an example in which an array is to be sorted in ascending order:
Iteration 1:
Step 1:
Since ar{0] > ar{1], i.e., 124 > 51 is true.
‘Swapping the numbers in the position, the array becomes:
Step 2:
Since ar{1] > ar[2] , ie., 124 > 10s true.
‘Swapping the numbers in the position, the array becomes:
Step 3:
Since ar{1] > ar{2] , i.e., 124 > 10 is true.
Scanned with CamScannernumbers in the position, the array becomes:
index | 0 1 Fy —
« [a Tie [wae [ar
index [0 1 2 5 4
= Lesa [a7 [ae a6 [an]
> arl2], Le., 124 > 10s true.
the numbers in the position, the array becomes:
index ° 1 x
a Csi [10 [ a | 21st
first iteration, the largest element is placed in the last.
the numbers in the position, the array I
st BA ee Nee?
becomes:
perenne are ) the array
Lt
cues nttiice acne ==
Scanned with CamScannerMteration 3:
‘Step 1:
index
ar
Since ar{0} > ar{1} , Le., 10 >44 is false,
No swapping is done.
‘Step 2:
« 2
Since ar{1} > ar(2}, ie., 44> 21 is true,
Swapping the numbers in the position, the array becomes:
rs
. | 126 J
After the third iteration, the third largest element is placed in third last position.
Iteration 4:
‘Step 1:
index Paps Ls |
a Cae zs |
‘Since ar{0} > ar{4], i.e., 10 > 22 is false.
No swapping is done.
After the fourth iteration, the whole array is sorted.
5 Selection Sort
In this technique, the smallest element is searched and the first element is interchanged with the smallest eleme
Then the second element is interchanged with the second smallest element and so on.
Let us take the previous example in which the array is to be sorted in ascending order:
index | 0
ar
index o
ar
The smallest element is in position ar{2) : 10
After swapping between ar[0] and ar(2] is executed, the array becomes:
index
ar
After the first step, the smallest element is stored in the first position of the array.
ie ‘Touchpad Computer Applications-x
i ait
Scanned with CamScanner
‘Step 1:smallest element isin postion ara): 21
between ar{1] and ari is executed, the array becomes:
index [9 si
ar [40
second step, the second smalle
index [0
a | 10 ig
smallest element is in position ar(3] : 44
swapping between ar{2] and ar{3] is executed, the array becomes:
index ° 1 7
a [40 a |
= ‘third step, the third smallest element is stored in the third position of the array.
sot see
: index [0 | 2
2 3
a Lio | a | a | 124
“The fourth smallest element is in position ar(4) : 51
“After swapping between ar{3] and ar{4] is executed, the array becomes:
index o 1 2
« [olaterers
After the fourth step, the array has been sorted in ascending order.
45.5.3 Difference between Bubble Sort and Selection Sort
‘Compares with the next element and swaps if the Selects the minimum element from the unsorted
condition ‘the next position in
matches part of the array and places it in
the sorted part of the array.
Bxecution is slow. Execution is fast.
Less efficient Mace arlil)
s = ar(il;
ue te ari)?
)
System.out.printin ("Sum = " + sum);
System.out.printn ("Largest number = * + 1)7
System.out.print1n ("Smallest number = “ + s);
)
,
\ese 2017
118. Write a program to initialize the seven Wonders of the World along with their locations In two different arrays. Search for
name of the country input by the user. if found, display the name of the country along with its Wonder, otherwise displs
“Sorry not found!”
ICSE 203
‘Seven Wonders: CHICHEN ITZA, CHRIST THE REDEEMER, TA! MAHAL, GREAT WALL OF CHINA, MACHU PICCHU, PETR
‘COLOSSEUM
Locations: MEXICO, BRAZIL, INDIA, CHINA, PERU, JORDAN, ITALY
Examples:
Country name: INDIA
Output: TA) MAHAL
‘Country name: USA
‘Output: Sorry not found!
Ans. import java-util.*7
class sevenWonders{
public static void main()Scanned with CamScannerScanned with CamScannerScanned with CamScannerScanned with CamScanner