Lec Array LinearSearch BinarySearch
Lec Array LinearSearch BinarySearch
1
Array
Declaration
Initialization
Accessing Array Elements
Inserting and Deleting in a Unsorted Array
Inserting and Deleting in a Sorted Array
Linear Arrays
• A linear array is a list of a finite number of n
homogeneous data elements (that is data elements of
the same type) such that.
– The elements are of the arrays are referenced
respectively by an index set consisting of n
consecutive numbers.
– The elements of the arrays are stored
respectively in successive memory locations.
3
Linear Arrays
• The number n of elements is called the length or size
of the array.
• The index set consists of the integer 0,1, 2, …n-1
• Length or the number of data elements of the array
can be obtained from the index set by
Length = UB – LB + 1
where
UB is the largest index called the upper bound and
LB is the smallest index called the lower bound of the arrays
4
Linear Arrays
• Element of an array A may be denoted by
– Subscript notation A1, A2, , …. , An
– Parenthesis notation A(1), A(2), …. , A(n)
– Bracket notation A[1], A[2], ….. , A[n]
• The number K in A[K] is called subscript or an index and A[K]
is called a subscripted variable.
5
Declaring and Initializing Arrays
To declare an array:
data_type array_name[SIZE];
int ar_name[10]
data_type is a valid data type that must be common to all elements.
array_name is name given to array
SIZE is a constant value that defines array maximum capacity.
Initializing Arrays
Initialization of an array either one by one or using a single
statement as follows −
int ar[5];
ar[0]=10; ar[1]=20;
double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};
Representation of Linear Array in Memory
Representation of Linear Array in Memory
Representation of Linear Array in Memory
int LA[10]
Let
• LA be a linear array in the memory of the computer.
• LOC(LA[K]) = address of the element LA[K]
9
Example 1
200
Find the address for LA[16] Each
201 LA[0]
element of the array occupy 2 byte
202
LA[1]
203
204
205 LA[2]
206
207 LA[3]
12
Linear Search(LA,N, ITEM)
Linear Search (LA, N, ITEM)
int search(int data[],int n, int
Here LA is a linear array with N item)
elements. This algorithm find an {
element ITEM into the LA. for(int i = 0; i<n; i++)
1. i=0 {
if(data[i] == item)
2. Repeat steps 3 and 4 while return i;
i<n or LA[i]!=ITEM }
return -1;
3. IF LA[i]==ITEM print item }
found at index i and exit
4. i=i+1
5. Print item not found and
exit.
Searching Arrays: Binary Search
Binary search
For sorted arrays
Compares middle element with key
If equal, match found
If key < middle, looks in first half of array
If key > middle, looks in last half
Repeat
14
Searching Arrays: Binary Search
int bsearch(int data[], int n, int value )
{
int first, middle, last;
first = 0;
last = n - 1;
while (true) {
middle = (first + last) / 2;
if (data[middle] == value)
return middle;
else if (first >last)
return -1;
else if (value < data[middle])
last = middle - 1;
else
first = middle + 1;
}
}
Inserting in Unsorted Array
INSERT (LA, N, ITEM) Here LA is a linear array with N
elements. This algorithm inserts an element ITEM into
the LA.
1. If MAX==N, print overflow
2. Set LA[N] :=ITEM
3. [Reset N.] Set N := N + 1.
4. Exit.
Delete in Unsorted Array
DELETE (LA, N, k) Here LA is a linear array with N
elements. This algorithm Delete the element ITEM from
the LA.
1. Set LA[k] :=LA[N-1]
2. [Reset N.] Set N := N - 1.
3. Exit.
INSERT_SORTL (LA, N, K, ITEM)
Here LA is a sorted array with N elements and
0 10
K is a positive integer such that K<N. This
1 20
algorithm insert an element ITEM from the
2 25
Kth position in LA.
3 30
4 40
1. j=N 5 45
2. Repeat step while LA[j]>ITEM; 6 50
3. Set LA[j]=LA[j-1] 7 60
4. Set LA[j]=ITEM 8 70
9 80
5. [Reset N.] Set N := N+1;
6. Exit.
DELETE_SORTL (LA, N, K, ITEM)
Here LA is a sorted array with N elements and K is a
positive integer such that K<N. This algorithm Delete 0 10
an element ITEM from the Kth position in LA.
1 20
2 30
SEARCH(LA, N, K, ITEM)
3 50
/* Find the item and return k as item’s index
Set j : = k. 4 60
2. Repeat Steps 3 and 4 while .j <= n. 5 70
3. Move jth element upward. 6 80
Set LA[j ] := LA[j+1]. 7 80
4. [Decrease counter.] Set j:= j+1. [End of Step 2 loop.]
5. [Reset N.] Set N := N-1;
6. Exit.
Multidimensional Array
• One-Dimensional Array
• Two-Dimensional Array
• Three-Dimensional Array
• Some programming Language allows as
many as 7 dimension
20
Two-Dimensional Array
• A Two-Dimensional m n array A is a
collection of m.n data elements
• with property that 1 ≤ J ≤ m and 1 ≤ K ≤ n
21
2D Arrays
The elements of a 2-dimensional array a is shown
as below
25
2D Array in Memory
A Subscript
A Subscript
(1,1)
(1,1)
(2,1) Column 1 (1,2) Row 1
(1,3)
(3,1)
(1,4)
(1,2) n 2
(2,1)
(2,2) Colum
(2,2)
(3,2)
(2,3) Row2
(1,3) mn 3
(2,4)
(2,3) Colu
(3,1)
(3,3)
(3,2) Row3
(1,4) n 4 (3,3)
(2,4) Colum (3,4)
(3,4)
Column-Major Order
Row-Major Order
26