Documente Academic
Documente Profesional
Documente Cultură
Analysis
Slide Set 2
13 CS- I & II
Engr. Maria Shaikh
maria.sheikh@faculty.muet.edu.pk
Arrays
An array is a fixed number of data items that are stored contiguously and
that are accessible by an index
An Array is a linear collection of Homogeneous data items.
An array is a collection of elements of similar data type.
Contiguous memory allocation takes place.
An array is a DS in which we can access every element directly using position
variable .
It is rather an organizational concept.
Array elements can be accessed individually
9/9/2014
Types of array
ONE DIMENSIONAL ARRAY:
One-dimensional array (or linear array) is a set of n finite numbers of
homogenous data elements such as
1. The elements of the array are referenced respectively by an index set
consisting of n consecutive numbers.
2. The elements of the array are stored respectively in successive memory
locations.
n number of elements is called the length or size of an array. The elements of
an array A may be denoted in C as
A[0], A[1], A[2], ...... A[n1].
The number n in A[n] is called a subscript or an index and A[n] is called a
subscripted variable. :
9/9/2014
9/9/2014
9/9/2014
Now this age array will have four elements, starting from age[0] ..
To age [3], total 4 elements. So, the index of first element is zero and
last element is one less than arrays size i.e. 3 = 4-1.
9/9/2014
9/9/2014
10
a[0] i=0
int a[10]={7,1,32,58,0,5,8,16,9,23};
14
a[1] i=1
Integer array a.
32
a[2] i=2
58
a[3] i=3
a[4] i=4
a[5] i=5
a[6] i=6
16
a[7] i=7
23
a[8] i=8
a[9] i=9
12
13
14
15
16
9/9/2014
17
18
19
20
if(j<n)
cout<<a[j]<<',';
else
cout<<a[j]<<'}';
}
return 0;
}
9/9/2014
21
9/9/2014
22
23
24
9/9/2014
25
}
return 0;
}
9/9/2014
26
9/9/2014
27
9/9/2014
28
9/9/2014
29
9/9/2014
30
9/9/2014
31
9/9/2014
32
9/9/2014
33
34
Step n-1: Compare A [1] with A [2] and arrange them so that A [1] < A [2].
After n-1 steps, the list will be sorted in the increasing order.
9/9/2014
35
36
9/9/2014
37
38
39
9/9/2014
40
9/9/2014
41
Searching
The goal of the search is to find all records with keys matching
a given search key.
Applications of searching are widespread, and involve a variety
of different operations.
Two common terms often used to describe data structures for
searching are dictionaries and symbol tables.
In searching have programs that are in widespread and frequent
use to study a variety of methods that store records in arrays
that are either searched with key comparisons or indexed by
key value.
42
Searching (Continue)
Search algorithms as belonging to packages implementing a
variety of generic operations that can be separated from
particular implementations, so that alternate implementations
can be substituted easily. The operations of interest include:
Searching (Continue)
Search and Insert operation is often included for efficiency in situations where
records with duplicate keys are not to be kept within the data structure
Records with duplicate keys can be handled in several ways:
the primary searching data structure contain only records with distinct
keys
to leave records with equal keys in the primary searching data structure
and return any record with the given key for a search
to assume that each record has a unique identifier (apart from the key)
and require that a search find the record with a given identifier, given the
key
to arrange for the search program to call a specified function for each
record with the given key
44
Searching
Algorithm.
9/9/2014
45
Linear Search
9/9/2014
46
47
48
49
50
9/9/2014
51
Binary Search
Suppose DATA is an array which is sorted in increasing numerical order or
equivalently alphabetically then there is an extremely efficient searching
algorithm called binary search which can be used to find the location of a
given item of information in data array.
The basic idea is that you divide the array being searched into sub-arrays,
and compare the middle element to the value for which you are searching.
The binary search algorithm applied to our DATA works as follows. During
each stage of our algorithm, our search ITEM is reduced to a segment of
element of DATA:
DATA[BEG], DATA[BEG+1], DATA[BEG+2] ,--------------DATA[END]
9/9/2014
52
Binary Search
Note that the variables BEG and END denote respectively the beginning and
end location of the segments under consideration. The algorithm compares
ITEM with middle element DATA[MID] of the segment, where MID is
obtained by:
MID = INT ( ( BEG + END) / 2 )
a) We use INT (A) for the integer value of A. If DATA [MID] = ITEM then
the search is successful and we set LOC = MID. Otherwise new segment of
DATA obtained as follows:
b) If ITEM < DATA [MID], then ITEM can appear only in the left half of the
segment.
DATA[BEG], DATA[BEG+1] ,--------------DATA [MID -1]
So we reset END: = MID 1 and begin searching again.
9/9/2014
53
Binary Search
a) If ITEM > DATA [MID], then ITEM can appear only in the right half of
the segment.
DATA [MID+1], DATA[MID+2] ,--------------DATA [END]
So we reset BEG: = MID + 1 and begin searching again.
Initially we begin with the entire DATA; i.e.., we begin with BEG =1 and
END =n, or more generally, with BEG = LB and END = UB.
If ITEM is not in DATA, then eventually we obtain
END < BEG
This condition signals that search is unsuccessful, and in such a case we
assign LOC: = NULL. Here NULL is a value that lies outside the set of
indices of DATA: (In most cases, we can choose NULL=0)
9/9/2014
54
55
56
9/9/2014
57
58
59
60
61
9/9/2014
62
9/9/2014
63
9/9/2014
64