0% found this document useful (0 votes)
14 views15 pages

Arrays - Part 2

Uploaded by

Vishal kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views15 pages

Arrays - Part 2

Uploaded by

Vishal kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
You are on page 1/ 15

ARRAYS

PART - 2
2

1. TECHNIQUE OF SEARCHING – LINEAR SEARCH


AGENDA 2. TECHNIQUE OF SORTING – BUBBLE SORT
3. PROGRAMS
3

SEARCHING IN AN ARRAY:

Looking for an element in an array. If found, then return the index


position.

LINEAR SEARCH OR SEQUENTIAL SEARCH

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.

c. It is a time consuming method if the size of the array is huge.


Sequential search technique 4
Consider an array arr with values
12, 5, 10, 15, 31, 20, 25, 2,
40 stored in it.
Now search for number 20 in the array.
5

○ Store a definite list of 5 integer values in an


array.
○ Accept the number to be searched for.
Algorithm ○ Write a loop statement that starts with the
first index number to one less than the
length of the array.
○ Check if the number is same as the element
in the list.
○ If yes then display the index number and
break out of the loop
○ Display an appropriate message if the
number is not found at all.
6
Linear Search / Sequential Search Program(with numbers) 7

int[] arr = {98, 76, 100, 67, 21};


boolean b = false;
int num = scr.nextInt(); // accepting number to be searched
for(int i=0; i<arr.length; i++)
{
if (num == arr[i] ) // checking the number against all elements of array
{
System.out.println(num +“present at index number” + i);
b = true;
break;
}
}

if( b==false)
{
System.out.println(num+“ not present in array”);
}
Linear Search / Sequential Search Program(with Strings) 8

String[] arr = {“rohan”, “geetha”, “maya”, “arnav”, “mayur”};


boolean b = false;
String s = scr.next(); // accepting name to be searched
for(int i=0; i<arr.length; i++)
{
if (s.equals( arr[i] ) ==true) // checking the name against all elements of array
{
System.out.println(s +“present at index number” + i);
b = true;
break;
}
}

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

PASS 1 PASS 2 PASS 3 PASS 4


11

First Accept Numbers Into The Array.

for( int i =0; I < arr.length - 1; i++)


{
for( int j =0; j < arr.length - i - 1; i+
+)
Bubble Sort Program {
Arranging numbers in if(arr[j]>arr[j+1])
{
ascending order temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp
}
}
}
Finally Display The Sorted Array.
12

First Accept String Values Into An


Array.

for( int i =0; I < arr.length - 1; i++)


{
Bubble Sort Program for( int j =0; j < arr.length - i - 1; i+
Arranging Strings in +)
alphabetical order. {
if(arr[j].compareTo(arr[j+1]) > 0)
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp
}
}
}
Finally Display The Sorted Array.
PROGRAMS 13
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 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”.
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 using binary search in the array. If found, display that “The
word is found in ___ position”. Otherwise display “Word not found in the array”.

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
!

You might also like