0% found this document useful (0 votes)
6 views3 pages

Array notes

An array is a collection of variables of the same type stored in contiguous memory locations, allowing for easy access and manipulation of data. There are one-dimensional and multi-dimensional arrays, with the former storing data in a single row or column and the latter organizing data in a grid format. Linear search checks each element sequentially, while binary search divides the array and is more efficient for sorted data.

Uploaded by

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

Array notes

An array is a collection of variables of the same type stored in contiguous memory locations, allowing for easy access and manipulation of data. There are one-dimensional and multi-dimensional arrays, with the former storing data in a single row or column and the latter organizing data in a grid format. Linear search checks each element sequentially, while binary search divides the array and is more efficient for sorted data.

Uploaded by

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

Question 1

What is an array ? What is the need for arrays ?


Answer
An array is a collection of variables of the same type that are referenced by a
common name. It is a reference data type which stores data values in
contiguous memory locations.
An array is declared and initialized as follows:
int arr[] = new int[10];
The use of arrays have the following benefits:
1. Easy to specify — The declaration, allocation of memory space,
initialization can all be done in one line of code.
2. Free from run-time overheads — There is no run-time overhead to
allocate/free memory, apart from once at the start and end.
3. Random access of elements — Arrays facilitate random (direct) access
to any element via its index or subscript.
4. Fast Sequential Access — It is usually faster to sequentially access
elements due to contiguous storage and constant time computation of
the address of a component.
5. Simple code — As arrays facilitate access of multiple data items of same
type through a common name, the code becomes much simpler and
easy to understand.
Question 2
What are different types of arrays? Give examples of each array type.
Answer
Arrays are of different types :
1. One-dimensional array — It comprises of finite homogeneous
elements.
2. Multi-dimensional arrays — It comprises of elements, each of which is
itself an array. A two-dimensional array is the simplest of
multidimensional arrays, having two indices (rows and columns).

Question 3

Differentiate between one-dimensional and two-dimensional arrays.

Answer

One-dimensional array Two-dimensional array

1
One-dimensional array stores Two-dimensional array stores data in a grid
data in a single row or column. or table, with rows and columns.

It uses one index to access It uses two indices to access array elements.
array elements.

For example, For example,


int arr[] = new int[10]; int arr[][] = new int [3][3];
It creates a one dimensional It creates a two dimensional array which
array arr which stores 10 has three rows and three columns to store
elements of int type. 3 x 3 = 9 elements of int type.

Question 4
What is the difference between linear search and binary search?
Answer
Linear Search Binary Search
Linear search works on sorted and Binary search works only on sorted
unsorted arrays. arrays (both ascending and
descending).
Each element of the array is checked Array is successively divided into 2
against the target value until the halves and the target element is
element is found or end of the array searched either in the first half or in the
is reached. second half.
Linear Search is slower. Binary Search is faster.

Question 5

Explain

(i) Linear search method,

(ii) Binary search method.

Which of the two is more efficient for sorted data ?

Answer

2
(i) Linear Search — Linear Search refers to the searching technique in which
each element of an array is compared with the search item, one by one, until
the search-item is found or all elements have been compared.

For example, consider an array

int arr[] = {5, 8, 11, 2, 9};


and the search item 2. Linear search will compare each element of the array
to 2 sequentially until either the search value 2 is found or all the elements
have been compared.
(ii) Binary search — Binary Search is a search-technique that works for sorted
arrays. Here search-item is compared with the middle element of array. If the
search-item matches with the element, search finishes. If search-item is less
than middle (in ascending array), we perform binary-search in the first half of
the array, other wise we perform binary search in the second half of the array.

For example, consider an array

int arr[] = {2, 5, 8, 11, 14};


and the search item 11. First, the search value will be compared with the
middle value 8. Since the values are not equal, binary search will be performed
in the latter half of the array, i.e., {11, 14}. The search value will be compared
with the mid value, which will be 11. Since search value is found, binary search
will stop.
Binary search is more efficient than linear search as it searches the given item
in minimum possible comparisons.

You might also like