0% found this document useful (0 votes)
19 views

Lec Array LinearSearch BinarySearch

The document discusses linear arrays and two-dimensional arrays. It defines arrays, describes how they are represented in memory, and covers operations like searching, inserting, deleting and sorting of elements. It also discusses multidimensional arrays and provides examples of row-major and column-major ordering.

Uploaded by

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

Lec Array LinearSearch BinarySearch

The document discusses linear arrays and two-dimensional arrays. It defines arrays, describes how they are represented in memory, and covers operations like searching, inserting, deleting and sorting of elements. It also discusses multidimensional arrays and provides examples of row-major and column-major ordering.

Uploaded by

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

CSE225: Data Structure and Algorithm

Array & Searching

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]

• Computer does not keep track of the address of every element of


the array
• Keep track address of the first element of array called the base
address of LA and denoted by Base(LA)

• LOC(LA[K]) = Base(LA) + w(K– lower bound)


where w is the number of words per memory cell

9
Example 1

Find the address for LA[6] Each 200 LA[1]


element of the array occupy 1 byte 201 LA[2]
202 LA[3]
203 LA[4]
204 LA[5]
205 LA[6]
206 LA[7]
207 LA[8]

LOC(LA[K]) = Base(LA) + w(K – lower bound) :


LOC(LA[6]) = 200 + 1(6 – 1) =
205
10
Example 2

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]

LOC(LA[K]) = Base(LA) + w(K – lower bound) :


LOC(LA[16]) = 200 + 2(16 – 0) = 232
11
Searching Arrays
Linear search: Compare each element of array with key value
Search an array for a key value
Simple
Useful for small and unsorted arrays

Suppose you want to find a number in an unordered sequence


You have no choice – look through all elements until you have
found a match
This is called linear or sequential search

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

Very fast; at most n steps, where 2n > number of elements


 30 element array takes at most 5 steps
 25 > 30 so at most 5 steps

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

The element of A with first subscript J and


second subscript K will be denoted by A[J][K]

21
2D Arrays
The elements of a 2-dimensional array a is shown
as below

a[0][0] a[0][1] a[0][2] a[0][3]


a[1][0] a[1][1] a[1][2] a[1][3]
a[2][0] a[2][1] a[2][2] a[2][3]
Rows Of A 2D Array

a[0][0] a[0][1] a[0][2] a[0][3] row 0


a[1][0] a[1][1] a[1][2] a[1][3] row 1
a[2][0] a[2][1] a[2][2] a[2][3] row 2
Columns Of A 2D Array

a[0][0] a[0][1] a[0][2] a[0][3]


a[1][0] a[1][1] a[1][2] a[1][3]
a[2][0] a[2][1] a[2][2] a[2][3]

column 0 column 1 column 3


column 2
2D Array
• Let A be a two-dimensional array m x n
• The array A will be represented in the memory
by a block of m x n sequential memory location
• Programming language will store array A either
– Column by Column
– (Called Column-Major Order) Ex: Fortran,
MATLAB
– Row by Row
(Called Row-Major Order) Ex: C, C++ , Java

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

You might also like