You are on page 1of 7
-_ Dat: a Structures The memory representation of array is shown in figure (2.5) Computer Science-XII [39 | Memory pent x Array elements are a stored sequentially 1002 Fig. 2.5 : Memory representation of array Traversing linear arrays Traversing array means accessing each element of the array only once. We need to performs this operation several times in a program. The algorithm for this operation is described below. Algorithm - Traversing a linear array. Suppose LA is linear array with lower bound LB and upper bond UB. This algorithm apply PROCESS to each element of LA. 1. Initialize counter Set K=LB 2. Repeat steps 3 and 4 while K < UB . Visit element Apply PROCESS to LA [K] » » . Increase counter Set K=K+1 [End of step 2 loop] 5. EXIT, Inserting and deleting Inserting refers to the operation of adding another element to the existing elements. Deleting refers to the operation of removing one of the element from existing elements. The element can be inserted at the end of array or in the middle of the array. Insertion at end can be eaisily done provided memory space is available. But when We want to insert element in the middle of array, then we need to create space for the element, by moving down the other elements. Similarly deleting an element at end of array is easy. But when element in the middle of array is deleted, then we have to move other elements upward to fill the Bap. —— insertion and deletion operation. Name Anil Atul Millind Rajesh Millind Rajesh Sanjay Rajesh Sanjay Sanjay Original array Addition of name Deletion of name (a) in middle of array from array (>) © Fig. 26: Insertion and deletion operation: inserting into linear array is given below : Inserting element into linear array. INSERT (LA, N, K, ITEM) Here LA is linear array with N elements and K is positive integer such that K = N. This algorithm inserts an element ITEM into the prod gu Bre __ Kt position in LA- 1. Initialize counter. Set J=N 2. Repeat steps 3 and 4 while 3. Move J" element downward. set LA[J+1]=LAU] 4, Decrease the counter. Set J=J-1. Insert the element Set LA [K]=ITEM J2kK. a ment from array is given below : it from linear array. N elements and K is a positive integet thm deletes the K"" element from LA. ome 5 Computer Science-XII [41 | ‘eset N N=N-1 4, EXIT. sorting Sorting means rearranging elements of array in increasing order. i.e. Al0] < A[1] < A[2] ...... < Alt] where A is array name. For example, suppose A originally is the list 8, 4, 19, 2, 7, 13, 5, 16 After sorting, A is the list 2, 4, 5, 7, 8, 13, 16, 19 Insorting we may rearrange data in decreasing order also. There are may sorting techniques. Here we will discuss the simple sorting technique called bubble sort. Suppose the list of numbers, A[1], A[2], ...... A{n] is in memory. The bubble sort algorithm works as follows : Step 1: Compare A[1] and A[2] and arrange them in desired order, so that A[1] < A[2]. Then compare A[2] and A[3] and arrange them so that A[2] < A[3]. Continue this process until we compare A[n - 1] with A[n], so that A[N - 1] ais HSM Aes Step 1 involves n - 1 comparisons. During this step, the largest element is si tais ike a bubble to the n'® position. When step 1 is completed, A[N] iv the largest element. Step 2: Repeat step 1 with one less comparision. Step 2 involves N 2 comparisions, _ when step 2 is completed we get second largest element A[N — 2]. Aoi soc A[2] and arrange them so that A[1] < A[2]. be sorted in increasing order. Each step is called as TRE computer Science! Data Struct . a [End of IF structure {b) Set PTR=PTR+1 [End of inner loop] [End of step 1 outer loop] 4 EXIT. ‘The time required for sorting algorithm is measured in terms of number o, comparisions. There are n- 1 comparisions during first pass, n — 2 comparisions in ‘second pass and so on. = Complexcity of algorithm fin) = (n-1e(n-2)+~—- 42 #1 n(n-)) oe 3 > +o(n) = o(n’) 5 ‘The time required to execute bubble sort algorithm is proportional to n?, where nis number of input items. ing, linear search ing refers to the operation of finding the location of given element in the iccessful if item is found. orithms. The most commonly used algorithms are ss linear search d with each element of list one by list. The search is said to be su ‘There are many searching alg: linear search and binary search. Lets first discus: In linear search the given element is compare: ‘one. This method is also called sequential search. Algorithm - Linear search LINEAR (DATA, N, ITEM, LOC) Here DATA is a linear array with N elements, and ITEM is given element. This algorithm finds the location LOC of ITEM in DATA, or sets LOC = 0 if search is unsuccessful. 1, Insert ITEM at the end of DATA. 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. If LOC=N +1, then set LOC = 0 5. EXIT. In worst case, the running time of algorithm is proportional to n. _— — pinary se suppose a list sorted in increasing order is given and an element for searching is In such a case we can use more efficient algorithm called binary search arch Binary search algorithm works as follows : suppose DATA is array in increasing order. DATA [BEG], DATA [BEG # 1], «..sssses000 , DATA [END] are element of DATA. BEG and END denote the beginning and end locations of segment under consideration. This algorithm compares the ITEM with middle element DATA [MID] of segment, where MID = INT ((BEG + END) / 2) INT gives integer value. If DATA [MID] = ITEM then search is successful and we set LOC = MID. Otherwise there are two possibilities - a) IfITEM DATA [MID], then ITEM can appear only in the right half of the segment. ie. DATA [MID + 1}........., DATA [END] So we reset BEG = MID + 1 and begin searching again. If ITEM is not in DATA, then eventually we obtain END < BEG. This signals that search is unsuccessful. Algorithm - Binary search Binary (DATA, LB, UB, ITEM, LOC) Here DATA is sorted array with lower bound LB and upper bound UB. ITEM is given element. BEG denotes beginning, MID denotes middle and END denotes end locations of segment of DATA. This algorithm finds the location LOC of ITEM in DATA or set LOC = NULL. 1. Set BEG = LB, END = UB and MID = INT ((BEG + END) / 2) 2. Repeat steps 3 and 4 while BEG < END and DATA [MID] # ITEM. 3. IF ITEM

You might also like