You are on page 1of 16

Array Data Structure

C#ODE Studio

Various operations performed on Arrays


Traversal- processing each element in the list Searching- Finding location of an element Insertion- Adding a new element Deletion- Removing an element Sorting-Arranging elements in some type of order Merging- Combining two lists into a single list

C#ODE Studio

Traversing Linear Arrays- Traversing a linear array is basically, visiting each element of array exactly once. Traversing is usually required to count number of elements of array or to perform other operations on each element of array. Traversing a linear array depends on size of array. Thus, traversal is O(n) operation for an array of size n. Algorithm: (Traversing a linear Array) 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 Step 1: [Initialize Counter] Set k:=LB Step 2: Repeat step 3 and 4 while k<=UB Repeat for k=LB to UB: Step 3: [Visit Element] Apply PROCESS Apply PROCESS to to LA[k] LA[k] Step 4: [increase Counter] set k:=k+1 [End of step 2 loop] Step 5: Exit
C#ODE Studio

Insertion and Deletion in an Array- Insertion refers to operation of adding an element to an array and deletion refers to deleting an element from an array Insertion- Insertion can be done at various positions but the main cases undertaken are: Insertion at the start Insertion at the end Insertion in the middle Insertion at any other position Inserting an element at the end of the array can be done easily provided the memory space allocated for the array is large enough to accommodate the additional element. For inserting at any other position, elements of array have to be shifted downwards from the position where insertion is to be done. The new element is then inserted in vacated position.
C#ODE Studio

Algorithm: (Inserting into a Linear 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 Kth position in LA. Step 1: [Initialize counter] Set J:=N Step 2: Repeat steps 3 and 4 while J K: [Move Jth element downward] Set LA[J+1]:=LA[J] [ Decrease Counter] Set J:=J -1 [End of Step 2 Loop] Step 3: [Insert element] Set LA[K]:=ITEM Step 4:[Reset N] Set N:=N+1 Step 5: Return

C#ODE Studio

Deletion refers to the operation of removing an element from exiting list of elements. Like insertion, deletion can be done easily from the end of the list. However to delete an element from any other location, the elements are to be moved upwards in order to fill up the location vacated by the removed element.

C#ODE Studio

Algorithm: (Deleting from a Linear Array) 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 an element ITEM from Kth position in LA. Step 1: [Initialize counter] Set ITEM:=LA[K] Step 2: Repeat for J=K to N-1: [Move J+1st element upward] Set LA[J]:=LA[J+1] [End of Step 2 Loop] Step 3: [Reset N] Set N:=N+1 Step 4: Return

C#ODE Studio

Analysis of Insertion and deletion operation


The best possible case in insertion operation is when the item is inserted at the last position. In this case, no movement of elements is required. The worst case occurs when the element has to be inserted at the beginning of the list. In this case, we have to move all the elements down the list. Therefore, the while loop executes n times, each moving one element down. Thus complexity of insertion operation is O(n), i.e linear time. The best case in deletion occurs when the element to be deleted is the last element of the array. In this case, no element is moved up. The worst case occurs when element is deleted from the first position. In this case, all (n-1) elements are moved up. The while loop executes n-1 times, each time moving one element down. Thus complexity of deletion operation is also O(n) i.e linear time.

C#ODE Studio

Algorithm: (Linear Search) LINEAR(DATA, N, ITEM, LOC) Here DATA is a linear array with N elements and ITEM is a given item of information. This algorithm finds the location LOC of ITEM in DATA, or sets LOC:=0 if search is unsuccessful. Step 1: [Initialize Counter] Set LOC:=1 Step 2: [Search for ITEM] Repeat while LOC<=N: If DATA[LOC] = ITEM, Then: Write: Element is at the location, LOC Exit [End of if structure] Set LOC:=LOC+1 [End of Loop] Step 3: [Unsuccessful] If LOC=N+1 , then: Set LOC:=0 Step 4: Return
C#ODE Studio

Binary Search: Binary search is a method of searching in which array list is divided into two equal parts. The main requirement of binary search method is that the array has to be in sorted order. If the elements of array list are in ascending order, then , if desired element is less than the middle element of the list, it will never be present in second half of the array and if desired element is greater than middle element, it will never be present in first half of the array list. Thus focus is given only to the desired half of the array list. Algorithm: BINARY(DATA, LB, UB, ITEM, LOC) Here DATA is a sorted array with lower bound LB and upper bound UB, and the ITEM is a given item of information. The variables BEG, END and MID denote, beginning, end and middle location of a segment of elements of DATA. This algorithm finds the location LOC of ITEM in DATA or set LOC=NULL Step 1: [Initialize segment variable] Set BEG:=LB, END:=UB and MID:= INT((BEG+END)/2)
C#ODE Studio

Step 2: Repeat while BEG<=END and DATA[MID]ITEM If ITEM < DATA[MID], then: Set END:= MID -1 Else: Set BEG:= MID +1 [End of if structure] Step 3: Set MID:=INT((BEG+END)/2) [End of step2 loop] Step 4: If DATA[MID]=ITEM, then: Set LOC:=MID Else : Set LOC:=NULL [End of if structure] Step 5: Return
C#ODE Studio

Analysis of Linear Search and Binary Search LINEAR SEARCH In best possible case, item may occur at first position. In this case, search operation terminates in success with just one comparison. However, the worst case occurs when either item is present at last position or element is not there in the array. In former case, search terminates in success with n comparisons. In latter case, search terminates in failure with n comparisons. Thus in worst case, the linear search is O(n) operation. In average case, the average number of comparisons required o find the location of item is approximately equal to half of the number of elements in the array. BINARY SEARCH In each iteration or in each recursive call, search is reduced to one half of the size of array. Therefore, for n elements, there will be log2n iterations or recursive calls. Thus complexity of binary search is O(log2n). This complexity will be same irrespective of position of element, even if it is not present in array. C#ODE Studio

Sorting Techniques in Array


Bubble sort method- The bubble sort method requires n-1 passes to sort an array where n is the size of array. In each pass, every element of the array a[i] is compared with a[i+1] for i=0 to n-k where k is the pass number. If a[i]>a[i+1], they are swapped. This will cause largest element to move or bubble up.

C#ODE Studio

Algorithm: BUBBLE(DATA, N) Here DATA is an array with N elements. This algorithm sorts the elements in DATA. Step 1: Repeat step 2 and 3 for K=1 to N-1: Step 2: [Initialize pass pointer PTR] Set PTR:=1 Step 3:[Execute Pass] Repeat while PTR N-K IF DATA[PTR] > DATA[PTR + 1], then: Interchange DATA[PTR] and DATA[PTR + 1] [End of If structure] Set PTR:=PTR +1 [End of Step 3 loop] [End of Step 1 Loop] Step 4: Return

C#ODE Studio

Complexity analysis of Bubble sort method: Traditionally, time for sorting an array is measured in terms of number of comparisons. The number f(n) of comparisons in bubble sort is easily computed. Specifically, there are n-1 comparisons during first pass, n-2 comparisons in second pass and so on. Thus, f(n)=(n-1) +(n-2) +-------+2+1 = n(n-1)/2 = n2 / 2 n/2 = O(n2) Thus time required to sort an array using bubble sort method is proportional to n2 where n is number of input items. Thus complexity of bubble sort is O(n2)

C#ODE Studio

References
Dinesh Mehta and Sartaj Sahni Handbook of Data Structures and Applications, Chapman and Hall/CRC Press, 2007. Niklaus Wirth, Algorithms and Data Structures, Prentice Hall, 1985. Diane Zak, Introduction to programming with c++, copyright 2011 Cengage Learning Asia Pte Ltd Schaumm Series ,McGraw Hill.

C#ODE Studio

You might also like