Arrays 1 Linear Arrays A linear array is

  • Slides: 10
Download presentation
Arrays 1

Arrays 1

Linear Arrays • A linear array is a list of a finite number n

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: – The elements of the array are referenced respectively by an index set consisting of n consecutive numbers. – The elements of the array are stored respectively in successive memory locations. • The length or the number of data elements of the array can be obtained from the index set by the following formula: Length = UB - LB + 1

Representation of Linear Arrays in Memory • Let LA be a linear array in

Representation of Linear Arrays in Memory • Let LA be a linear array in the memory of the computer. • The computer calculates the address of kth Element of the array by the following formula: LOC(LA[K]) = Base(LA) + w(K – LB) • Here, W is the number of words per memory cell for the array LA. 1000 LA[1] 1001 LA[2] 1002 LA[3] . . .

Traversing a linear array Algorithm 4. 1: 1. Set K : = LB 2.

Traversing a linear array Algorithm 4. 1: 1. Set K : = LB 2. Repeat step 3 and 4 while K <= UB 3. Apply PROCESS to LA[K] 4. Set K : = K + 1 5. Exit

Inserting into a linear array Algorithm 4. 2: INSERT( LA, N, K, ITEM) 1.

Inserting into a linear array Algorithm 4. 2: INSERT( LA, N, K, ITEM) 1. Set J : = N 2. Repeat Steps 3 and 4 while J >= K 3. Set LA[J+1] : = LA[J] 4. Set J : = J – 1 5. Set LA[K] : = ITEM 6. Set N : = N + 1 7. Exit

Deleting From a Linear Array Algorithm 4. 3: DELETE( LA, N, K, ITEM) 1.

Deleting From a Linear Array Algorithm 4. 3: DELETE( LA, N, K, ITEM) 1. Set ITEM : = LA[K] 2. Repeat for J : = K to N – 1 Set LA[J] : = LA[J+1] 3. Set N : = N - 1 4. Exit

Searching refers to the operation of finding an item from a list of items

Searching refers to the operation of finding an item from a list of items based on some key value. Two Searching Methods (1) Linear Search (2) Binary Search

Linear Search • A linear search is a technique for finding a particular value

Linear Search • A linear search is a technique for finding a particular value in a unsorted or sorted list. • Search each item of list using key value, one at a time, until finding the item from the list. Algorithm: Linear-Search (LA, N, Key) 1. Set k : = 1 and Loc : = 0 2. Repeat Steps 3 and 4 while k <= N 3. If Key = LA[k], then Set Loc : = k, print: Loc and exit. 4. Set k : = k+1 5. If Loc = 0 then Write: Item is not in List 6. Exit Here L - The list of data items N – Total no. of items in L Key – Item to be searched.

Example of Linear Search List : 10, 6, 222, 44, 55, 77, 24 Key:

Example of Linear Search List : 10, 6, 222, 44, 55, 77, 24 Key: 55 Steps: 1. 55 10 2. 55 10 3. 55 10 6 4. 55 10 6 222 5. 55 10 6 222 6 6 So, Item 55 is in position 5. 222 44 55 77 24 K=1 222 44 55 77 24 K=2 222 44 55 77 24 K=3 4455 77 24 K=4 5577 24 K=5 44

Complexity Analysis of Linear Search (1) Worst Case The worst case occurs when Item

Complexity Analysis of Linear Search (1) Worst Case The worst case occurs when Item is the last element in the List or is not in the List. So, C(n) = n = O(n). (2) Average Case The average case occurs when the availability of the Item in the List is equally likely to occur at any position in the List. So, C(n) = = = 0(n)