Arrays - Part 2
Arrays - Part 2
PART - 2
2
SEARCHING IN AN ARRAY:
a. Usually this type of search takes place in an array which is not sorted.
b. The value is looked for in a sequence, beginning from the first element in
the array till the element is found or till the end of the array.
if( b==false)
{
System.out.println(num+“ not present in array”);
}
Linear Search / Sequential Search Program(with Strings) 8
if( b==false)
{
System.out.println(s+“ not present in array”);
}
9
SORTING IN AN ARRAY
Sorting means to put data in order; either numerically or alphabetically.
There are various sorting techniques:
Merge sort, Insertion sort, Bubble sort, Selection sort and so on.
BUBBLE SORT:
* It is the simplest of algorithms.
* The array is traversed from the first element to the last element.
* The current element is compared with the next element.
* If the elements are not in desired order then they are swapped.
BUBBLE SORT TECHNIQUE Values to be arranged in ascending order.
10
4. Initialize an array with the computer marks(out of 100) of 10 students, initialize another
array with the corresponding names of the Students. Under appropriate heading, using
Bubble sort technique, display the marks of the students from the highest mark to the
lowest mark along with the corresponding names of the students.
5. Initialize an array ‘states’ with the names of 5 states of India. Initialize another array
‘capitals’ with the names of the corresponding capitals of the states. Write a program to
accept the name of a state in India and search for the accepted state in the array. If found
then display “The state is found in ___ position and the capital is ____”. Otherwise display
“state not found in list”.
PROGRAMS 14
1. Write a program to accept 10 int values into the array. Accept a number and search for
the number in the array. If found, display that “The number is found in ___ position”.
Otherwise display “Number not found in the array”.
2. Write a program to accept 10 int values into the array. Accept a number and search for
the number using binary search in the array. If found, display that “The number is found in
___ position”. Otherwise display “Number not found in the array”.
3. Write a program to create an array x and accept 5 String values into the array. Accept a
word and search for the word in the array. If found, display that “The word is found in ___
position”. Otherwise display “Word not found in the array”.
4. Write a program to create an array x and accept 5 String values into the array. Accept a
word and search for the word using binary search in the array. If found, display that “The
word is found in ___ position”. Otherwise display “Word not found in the array”.
5. Initialize an array with the computer marks(out of 100) of 10 students, initialize another
array with the corresponding names of the Students. Under appropriate heading, using
Bubble sort technique, display the marks of the students from the highest mark to the
lowest mark along with the corresponding names of the students.
15
Thanks
!