This document summarizes lectures on arrays given by Muhammad Fayyaz. It defines linear and multi-dimensional arrays, and how they are represented and accessed in memory. It also discusses operations like traversing, inserting, deleting and searching in linear arrays. Algorithms to perform these operations like insertion, deletion and linear search are provided. Finally, it covers representation of two-dimensional arrays in memory and provides the formula to calculate the address of an element.
This document summarizes lectures on arrays given by Muhammad Fayyaz. It defines linear and multi-dimensional arrays, and how they are represented and accessed in memory. It also discusses operations like traversing, inserting, deleting and searching in linear arrays. Algorithms to perform these operations like insertion, deletion and linear search are provided. Finally, it covers representation of two-dimensional arrays in memory and provides the formula to calculate the address of an element.
Original Description:
this will let you understand basics of data structures
This document summarizes lectures on arrays given by Muhammad Fayyaz. It defines linear and multi-dimensional arrays, and how they are represented and accessed in memory. It also discusses operations like traversing, inserting, deleting and searching in linear arrays. Algorithms to perform these operations like insertion, deletion and linear search are provided. Finally, it covers representation of two-dimensional arrays in memory and provides the formula to calculate the address of an element.
This document summarizes lectures on arrays given by Muhammad Fayyaz. It defines linear and multi-dimensional arrays, and how they are represented and accessed in memory. It also discusses operations like traversing, inserting, deleting and searching in linear arrays. Algorithms to perform these operations like insertion, deletion and linear search are provided. Finally, it covers representation of two-dimensional arrays in memory and provides the formula to calculate the address of an element.
Download as PPTX, PDF, TXT or read online from Scribd
Download as pptx, pdf, or txt
You are on page 1of 33
CSC112
Algorithms and Data Structures
Lecture 03 & 04 Arrays Linear and Multi-dimensional Arrays
By Muhammad Fayyaz
Contact Information Instructor: Muhammad Fayyaz (Lecturer) Department of Computer Sciences Comsats Insititute of Information Technology, Wah Cantt
Office Hours: Thuesday, 09:00 am - 12:00 pm, at CS faculty Hall
9 / 1 1 / 2 0 1 4
2 Previous Lecture Complexity of Algorithm O Notation Data Structure Operations 9 / 1 1 / 2 0 1 4
3 Todays Lecture
What is Array? Declaration Linear Arrays & its representation in memory (reference book) Traversing in Linear Arrays (reference book) 9 / 1 1 / 2 0 1 4
4 Arrays What is an array? A linear structure approach (sequential memory location). A useful way of collecting the similar type of data items.
Example: In case of Employee Record: 1) Declare 50 different variables to store the age of employees.
Note: Arrays can also be termed as Linear Arrays
9 / 1 1 / 2 0 1 4
5 Linear Arrays A linear array is a list of a finite number n of homogeneous data elements (i.e., data elements of the same type) such that:
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.
9 / 1 1 / 2 0 1 4
6 Linear Arrays (Cont) The number n of elements is called the length or size of the array. Index set consists of the integers 1, 2, 3, .., n.
The general equation to find the length or the number of data elements of the array is,
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 array. Remember that length=UB when LB=1. 9 / 1 1 / 2 0 1 4
7 Linear Arrays (Cont) Array DATA be a 6-element linear array of integers such that DATA[1]=247, DATA[2]=56, DATA[3]=429, DATA[4]=135, DATA[5]=87, DATA[6]=156.
9 / 1 1 / 2 0 1 4
8 Declaration of Array
Like other variables, we also declare the arrays. The declared array should have:
1) Data Type 2) Array Name 3) Number of Sub-scripts/elements in the bracket.
The syntax of declaration of array is :
DataType arrayName[expression]; 9 / 1 1 / 2 0 1 4
9 Linear Arrays and its Representation in Memory Consider LA be a linear array in the memory of the computer. As we know that the memory of the computer is simply a sequence of addressed location as shown in fig. below: 9 / 1 1 / 2 0 1 4
10 Computer Memory Linear Arrays and its Representation in Memory (Cont) Using this address Base address, the computer calculates the address of any element of LA by the following formula: LOC(LA[K]) = Base(LA) + w (K- lower bound) 9 / 1 1 / 2 0 1 4
11 Base Address the number of words per memory cell for the array LA. Linear Arrays and its Representation in Memory (Cont) Example Consider the example array AUTO, which records the number of automobiles sold each year from 1932 through 1984. Array AUTO appears in memory is pictured in Fig. 01 (next slide) . Assume, Base(AUTO) = 200 and w = 4 words per memory cell for AUTO.
Then the base addresses of following arrays are, LOC(AUTO[1932]) = 200, LOC(AUTO[1933]) = 204, LOC(AUTO[1934]) = 208, . 9 / 1 1 / 2 0 1 4
12 Example (Linear Arrays) Let we find the address of the array element for the year K = 1965. It can be obtained by using Equation:
13 Fig. 01 Array AUTO appears in Memory Traversing Linear Arrays Consider let A be a collection of data elements stored in the memory of the computer.
Suppose we want to either print the contents of each element of A or to count the number of elements of A with a given property. This can be accomplished by traversing A, that is, by accessing and processing (frequently called visiting) each element of A exactly once. 9 / 1 1 / 2 0 1 4
14 Traversing Linear Arrays (Cont) The following algorithm is used to traversing a linear array LA. As we know already, here, LA is a linear array with lower bound LB and upper bound UB. This algorithm traverses LA applying an operation PROCESS to each element of LA. 9 / 1 1 / 2 0 1 4
15 Traversing Linear Arrays (Cont) We also state an alternative form of the algorithm which uses a repeat-for loop instead of the repeat- while loop. This algorithm traverses a linear array LA with lower bound LB and upper bound UB. 9 / 1 1 / 2 0 1 4
16 Lecture 04 9 / 1 1 / 2 0 1 4
17 Insertion, Deletion and Searching in Linear Arrays (reference book) Multidimensional Arrays & its representation in memory (reference book) Insertion and Deletion Let A be a collection of data elements in the memory of the computer. Inserting refers to the operation of adding another element to the collection A, and deleting refers to the operation of removing one of the elements from A. 9 / 1 1 / 2 0 1 4
18 Insertion in an Array Inserting an element at the end of a linear array can be easily done provided the memory space allocated for the array is large enough to accommodate the additional element. On the other hand, suppose we need to insert an element in the middle of the array. Then, on the 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. 9 / 1 1 / 2 0 1 4
19 Insertion in an Array (Cont) Example
Consider TEST has been declared as a 5-element array but data have been recorded only for TEST [1]=10, TEST [2]=5, and TEST [3]=8.
If X is the value to the next element, then we may simply assign, TEST [4] := X to add X to the Linear Array. Similarly, if Y is the value of the subsequent element, then we may assign, TEST [5] := Y to add Y to the Linear Array. 9 / 1 1 / 2 0 1 4
20 Example 9 / 1 1 / 2 0 1 4
21 Insertion in Middle of an Array (Insert Ford at 3 rd
location) Insertion of an Array (Insert Taylor at 6 th
location) Deletion of element from an Array (Delete Davis from 2 nd location) Deletion in an Array Deleting an element at the end of an array presents no difficulties, but deleting an element somewhere in the middle of the array would require that each subsequent element be moved one location upward in order to fill up the array. 9 / 1 1 / 2 0 1 4
22 Algorithms in Arrays For Insertion: Algorithm inserts a data element ITEM into the Kth position in a linear array LA with N elements i.e. INSERT(LA,N,K,ITEM).
Here LA is a linear array with N elements and K is a positive integer such that K<=N. This algorithm inserts an element ITEM into the Kth position in LA.
9 / 1 1 / 2 0 1 4
23 Deletion Algorithm The following algorithm deletes the Kth element from a linear array LA and assigns it to a variable ITEM i.e. DELETE(LA,N,K,ITEM). Here LA is a linear array with N elements and K is a positive integer such that K<=N. This algorithm deletes the Kth element from LA. 9 / 1 1 / 2 0 1 4
24 Note: We may conclude that if many deletions and insertions are to be made in a collection of data elements, then a linear array may not be the most efficient way of storing the data. 9 / 1 1 / 2 0 1 4
25 Searching in Linear Arrays (Linear Search) Search (LA, N, ITEM, LOC)
Here LA is a linear array with N elements and ITEM is given item of information. The algorithm finds the location LOC of ITEM in LA, or sets LOC:=0, if search is unsuccessful. 9 / 1 1 / 2 0 1 4
26 Searching Algorithm 1. [Insert ITEM at the end of LA.] Set LA[N+1]:=ITEM 2. [Initialize counter] Set LOC:=1 3. [Search for ITEM] Repeat while LA[LOC] != ITEM: Set LOC:=:LOC+1 [End of Loop]
4. [Successful?] if LOC = N+1 then Set LOC:=0 5. Exit 9 / 1 1 / 2 0 1 4
27 Multidimensional Arrays Multidimensional arrays can be described as "arrays of arrays". For example, a bi-dimensional array can be imagined as a bi-dimensional table made of elements, all of them of a same uniform data type. jimmy represents a bi-dimensional array of 3 per 5 elements of type int. The way to declare this array in C++ would be: 9 / 1 1 / 2 0 1 4
28 Representation of Two-Dimensional Arrays in Memory Let A be a two-dimensional m x n array. Although A is pictured as a rectangular array of elements with m rows and n columns, the array will be represented in memory by a block of m*n sequential memory locations.
Specifically, the programming languages will store the array A in either, 1. Column by column, called column-major order, or 2. Row by row, called row-major order. 9 / 1 1 / 2 0 1 4
29 Representation of Two-Dimensional Arrays in Memory (Cont) 9 / 1 1 / 2 0 1 4
30 Address of Element in Multi-Dimensional Array (Cont) For two-dimensional m*n array:
The formula for column order is, LOC(A[J, K]) = Base(A) + w[M(K-1) + (J-1)]
The formula for row major order is, LOC(A[J, K]) = Base(A) + w[N(J-1) + (K-1)] 9 / 1 1 / 2 0 1 4
31 Example: Consider the example of 25 x 4 matrix array SCORE[Student , test]. Suppose Base(SCORE) = 200 and there are w =4 words per memory cell.
Further more let the programming language stores two-dimensional arrays using row-major order.
Then the address of SCORE[12,3], the third test of the twelfth student, follows: LOC(SCORE[12,3]) = 200 + 4[4(12 -1) + (3 -1)] = 200 + 4[46] = 284 9 / 1 1 / 2 0 1 4
32 For any query Feel Free to ask 9 / 1 1 / 2 0 1 4