Linear Array Linear Arrays • A linear array is a list of finite number n of homogeneous data elements. • Length = UB-LB+1 • UB: largest index, LB: smallest index Memory representation • Let LOC(LA[K]) denotes address of the element LA[K] of the array LA • LOC(LA[K])=Base(LA)+W(K-LB) Example-1 • AUTO which records the number of automobiles sold each year from 1932 to 1984. Base(Auto)=200 W=4 words per memory cell • Find the address of element which store the info about sale in year 1965? Example 2 Find the address for LA[6] Each element of the array occupy 1 byte 200 LA[1] 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 5 Example 3 Find the address for LA[16] Each element of the array occupy 2 byte 200 LA[1] 201 202 LA[2] 203 204 LA[3] 205 206 LA[4] 207 LOC(LA[K]) = Base(LA) + w(K – lower bound) : LOC(LA[16]) = 200 + 2(16 – 1) = 230 6 Basic Operations on linear Array Following are the basic operations supported by an array: 1. Traverse − print all the array elements one by one. 2. Insertion − add an element at given index. 3. Deletion − delete an element at given index. 4. Search − search an element using given index or by value. 5. Sort − arrange elements in ascending or descending order Algorithm for inserting an element into an array • 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. 1. [Initialize counter.] Set J := N. 2. Repeat Steps 3 and 4 while J >= K. 3. [Move Jth element downward.] Set LA[J+1] := LA[J]. 4. [Decrease counter.] Set J := J – 1. [End of Step 2 loop.] 5. [Insert element.] Set LA[K] := ITEM. 6. [Reset N.] Set N := N + 1. 7. Exit. Assignment-1: Implementation of Insertion algorithm in C #include <stdio. h> main() { Int LA[ ] = {1, 3, 5, 7, 8}; Int item = 10, k = 3, n = 5; Int i = 0, j = n; printf(" The original array elements are : \n" ); for(i = 0; i<n; i++) { printf(" LA[ %d] = %d \n" , i, LA[ i] ); } n = n + 1; while( j >= k){ LA[ j+1] = LA[ j] ; j = j - 1; } LA[ k] = item; printf(" The array elements after insertion : \n" ); for(i = 0; i<n; i++) { printf(" LA[ %d] = %d \n" , i, LA[ i] ); } } Algorithm for deleting an element from an array DELETE (LA, N, K, ITEM): 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. 1. Set ITEM := LA[K]. 2. Repeat for J = K to N - 1. [Move J + 1st element upward.] Set LA[J] := LA[J + 1]. [End of loop.] 3. [Reset the number N of elements in LA.] Set N := N - 1. 4. Exit. Assignment-2: Deleting and element from the array in C language #include <stdio. h> main() { int LA[ ] = {1, 3, 5, 7, 8}; int k = 3, n = 5; int i, j; printf(" The original array elements are : \n" ); for(i = 0; i<n; i++) { printf(" LA[ %d] = %d \n" , i, LA[ i] ); } j= k; while( j < n){ LA[ j- 1] = LA[ j] ; j = j + 1; } n= n - 1; printf(" The array elements after deletion : \n" ); for(i = 0; i<n; i++) { printf(" LA[ %d] = %d \n" , i, LA[ i] ); } }