0% found this document useful (0 votes)
34 views23 pages

Arrays

Arrays contain a fixed number of elements of the same data type. They are defined before use and their size cannot change. A linear array stores elements in sequential memory locations, allowing the address of any element to be calculated from the base address. Binary search efficiently locates an element in a sorted array by repeatedly checking the middle element and narrowing the search space.

Uploaded by

shadowriser240
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)
34 views23 pages

Arrays

Arrays contain a fixed number of elements of the same data type. They are defined before use and their size cannot change. A linear array stores elements in sequential memory locations, allowing the address of any element to be calculated from the base address. Binary search efficiently locates an element in a sorted array by repeatedly checking the middle element and narrowing the search space.

Uploaded by

shadowriser240
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/ 23

An Array

Arrays
● Contain fixed number of elements of same data type.
● Static Entity- same size throughout the program.
● An array must be defined before it is used.
● Array definition specifies array name, variable type & size.
● Size specifies how many data items the array will contain.
Linear Arrays
Two basic Representation in Memory:
Have a Linear Relationship between the data elements
represented by means of sequential memory locations. i.e.
Arrays
Have the Linear Relationship between the elements
represented by means of Pointer or Links i.e. Linked List
Linear Array is a list of a finite number of n
homogeneous data elements such that all the elements of
the array are referenced respectively by an index and are
stored respectively in successive memory locations.
n is called the length or range of the array.
Length = UB – LB + 1
Representation of Linear Array
in Memory
Let LA be a linear array in the memory of the computer &
LOC(LA[K]) = address of the element LA[K].
Now, as array is the Linear Array, computer does not need to
keep track of the address of every element of LA but need to
track only the address of the first element of the array
denoted by Base(LA) called the base address of LA.
LOC(LA[K]) = Base(LA) + w(K – lower bound)
● where w is the no. of words per memory cell.
For any value, time to calculate LOC(LA[K]) is same.
Representation of Linear Array
in Memory
Find the address for LA[6]

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

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


Representation of Linear Array
in Memory
Find the address for LA[16]

LOC(LA[16]) = 200 + 2(16 – 1) = 230


Traversing Linear Arrays
Traversing is Accessing and Processing each element of
an array structure exactly ones.
Traversing Linear Array
Here LA is a Linear array & this algorithm traverses LA
applying an operation PROCESS to each element of LA.
1. [Initialize counter] Set K = LB
2. Repeat steps 3 and 4 while K<=UB
3. [visit element] Apply PROCESS to LA[K]
4. [Increase counter] Set K=K+1 [end of step 2 loop]
5. Exit
i.e. Find no. of years in which more than 300 automobiles were
sold.
Insertion and Deletion
Insertion & Deletion of an element may be at:
● Beginning
● Middle
● End

Insert Ford at the End of Array


Insertion in Array

Insert Ford as the 3rd Element of Array


Insertion is not Possible without loss of data if the
array is FULL
Deletion in Array

Deletion of Wagner at the End of Array


Deletion in Array

Deletion of Davis from the Array


No data item can be deleted from an Empty Array
Insertion and Deletion
• Inserting an element at the ‘end’ of array can be done
easily provided there must be memory space available.
• To insert an element in the middle of array, then on
average, half of the elements must be moved downward to
new locations to accommodate the new element and keep
the order of the other elements.
• Similarly,deleting an element at the ‘end’ of an array
presents no difficulties, but deleting and element in middle
would require that each subsequent element be moved one
location upward in order to ‘fill up’ the array.
Insertion Algorithm
INSERT (LA, N , K , ITEM), K is a positive integers such
that K ≤ N. This algorithm insert an element ITEM into the
Kth position in LA.
1. [Initialize Counter] Set J := N
2. Repeat Steps 3 and 4 while J ≥ K
3. [Move the Jth element downward ]Set LA[J + 1] :=
LA[J]
4. [Decrease Counter] Set J := J -1
5 [Insert Element] Set LA[K] := ITEM
6. [Reset N] Set N := N +1;
7. Exit
Deletion Algorithm
DELETE (LA, N , K , ITEM). This algorithm deletes Kth
element from LA.
1. Set ITEM := LA[K]
2. Repeat for J = K to N -1
3. [Move the J + 1st element upward]Set LA[J] := LA[J + 1]
3. [Reset the number N of elements] Set N := N - 1;
4. Exit
Searching in Linear Array
Searching refers to finding the location LOC of ITEM in
DATA or printing some message when item does not appear.
We are going to apply two type of search:
• LINEAR search
• BINARY search

LINEAR search is simplest technique for searching an


unordered array for a particular element is to scan each entry in
the array in a sequential manner until the desired element is
found.
Algorithm for Linear Search
LINEAR [DATA, N, ITEM, LOC] here DATA is a linear array
with N elements, and ITEM is given item of information. We
have to find the LOC of ITEM or sets LOC=0 if search is
unsuccessful.
1. [Insert ITEM at the end] Set DATA[N + 1] = ITEM
2. [Initialize counter] Set LOC = 1
3. [Search for ITEM] Repeat while DATA[LOC] != ITEM
Set LOC = LOC + 1 [End of Loop]
4. [Successful ?] IF LOC = N + 1 then Set LOC = 0
5. Exit
Binary Search
• Inthis method, the main requirement is that the array
should be arranged in Ascending Order.
• Here, the approximate Middle Element of the array is
located, and its value is examined.
● If its value is too high, then the value of the middle
element of the first half of the array and the procedure is
repeated to the first half until the required item is found.
● If the value is too low, then the value of the middle
element of the second half of the array is checked and the
procedure is repeated.
Algorithm for Binary Search
BINARY[DATA,LB, UB,ITEM,LOC] here DATA is a Sorted
array, ITEM is given item & variables BEG, END and MID. We
will find the LOC of ITEM in DATA or sets LOC=NULL if
search is unsuccessful.
1. [initialize segment variables] Set BEG = LB ; END = UB; &
MID = INT((BEG + END) / 2)
2. Repeat steps 3 and 4 while BEG <=END & DATA [MID] != ITEM
3. IF ITEM < DATA [MID] then Set END = MID – 1 else Set
BEG = MID + 1
4. Set MID = INT((BEG + END) / 2)
5. If DATA [MID] = ITEM then Set LOC = MID else Set LOC
= NULL
6. Exit
Binary Search
75, 151, 203, 275, 318, 489, 524, 591, 647, and 727
X = 275 and X = 727…
Two-Dimensional Array

•A Two-Dimensional array A is a collection of m*n


data elements such that each element is specified by a
pair of integer [J,K] called subscript with property
that:
1 ≤ J ≤ m and 1 ≤ K ≤ n
• Theelement of A with first subscript J and second
subscript K will be denoted by AJ,K or A[J][K].
• A row is a horizontal list of elements.
• A column is a vertical list of elements
2D Array in Memory

Column-Major Order Row-Major Order


2D Array in Memory
• LOC (A[J,K])
• Column-Major Order:
LOC(A[J,K]) = Base(A) + w[m(K-1) + (J-1)]
• Row-MajorOrder:
LOC(A[J,K]) = Base(A) + w[n(J-1) + (K-1)]
Consider a 25 x 4 array A with Base(A) = 200 and w =4.
Suppose the programming store 2D array using row-major.
Compute LOC(A[12,3])

• LOC(A[12,3]) = 200 + 4[4(12-1) + (3 -1)] = 384

You might also like