Important question of DS

5
Array, linear and Binary Search 1. (a) Define Array (one, two/multidimensional) with example. A linear array is a list of finite number n of homogeneous data elements such that : a) The elements of the array are referenced respectively by an index set consisting of n consecutive numbers. b) The elements of the array are stored respectively in successive memory locations. The number n of elements is called the length or size of the array. Two-dimensional Arrays An array keeps track of multiple pieces of information in linear order, a one-dimensional list. However, the data associated with certain systems (a digital image, a board game, etc.) lives in two dimensions. To visualize this data, we need a multi- dimensional data structure, that is, a multi-dimensional array. A two-dimensional array is really nothing more than an array of arrays (a three-dimensional array is an array of arrays of arrays). A two-dimensional array looks like this: int[][] myArray = { {0,1,2,3}, {3,2,1,0}, {3,5,6,1}, {3,8,3,4} };

description

Important question of DS

Transcript of Important question of DS

Array, linear and Binary Search

1. (a) Define Array (one, two/multidimensional) with example.

A linear array is a list of finite number n of homogeneous data elements such that :

a) The elements of the array are referenced respectively by an index set consisting of n consecutive numbers.

b) The elements of the array are stored respectively in successive memory locations.

The number n of elements is called the length or size of the array.

Two-dimensional ArraysAn array keeps track of multiple pieces of information in linear order, a one-dimensional list. However, the data associated with certain systems (a digital image, a board game, etc.) lives in two dimensions. To visualize this data, we need a multi-dimensional data structure, that is, a multi-dimensional array. A two-dimensional array is really nothing more than an array of arrays (a three-dimensional array is an array of arrays of arrays).

A two-dimensional array looks like this:

int[][] myArray = { {0,1,2,3}, {3,2,1,0}, {3,5,6,1}, {3,8,3,4} };

(b) What is sorting? How Bubble sort algorithm works (p4.10, also see algorithm 4.4,p4.11). Practice the exercise: Ex. 4.7, p4.10

Sorting is nothing but storage of data in sorted order, it can be in ascending or descending order. The term Sorting comes into picture with the term Searching.

Sorting arranges data in a sequence which makes searching easier.

Buble Short Example :

Algorithm Bubble_Sort (DATA, N):1. Repeat steps 2 and 3 for K = 1 to N-1.2. Set PTR: =1.[Initializes pass pointer PTR]3. Repeat while PTR<=N-K: [Executes pass]a) If DATA[PTR]>DATA[PTR+1],then:

TEMP := A[PTR], A[PTR] := A[PTR+1], A[PTR+1] := temp[End of if structure]

b) Set PTR: =PTR+1[End of inner loop][End of step 1 Outer loop]

4. Exit

2. (a) Define linear and binary search. Write their algorithms (Algorithm 4.5, p4.13 andAlgorithm 4.6, p4.16).

Linear Search

A linear search is the basic and simple search algorithm. A linear search searches an element or

value from an array till the desired element or value is not found and it searches in a sequence

order. It compares the element with all the other elements given in the list and if the element is

matched it returns the value index else it return -1. Linear Search is applied on the unsorted or

unordered list when there are fewer elements in a list.

Binary Search

Binary Search is applied on the sorted array or list. In binary search, we first compare the value with

the elements in the middle position of the array. If the value is matched, then we return the value. If

the value is less than the middle element, then it must lie in the lower half of the array and if it's

greater than the element then it must lie in the upper half of the array. We repeat this procedure on

the lower (or upper) half of the array. Binary Search is useful when there are large numbers of

elements in an array.

(c) Differentiate between the complexity of linear and binary search.

3. Write the algorithm of Matrix multiplication (Alg. 4.7, p4.36).