Array(ADT)Linear Data Structure www.eshikshak.co.in
What are Arrays ? ● An Aay is collection of elements stored in adjacent memory locations. ● By ‘finite’ – specific number of elements in an Aay ● By ‘similar’ – all the elements in an Aay are of the same data type www.eshikshak.co.in
What are Arrays ? (cont.) ● An Aay containing number of element is reference using an index values 0, 1, …n-1 ○ Lower bound–Lowest index value ○ Upper bound–Highest index value ● An Aay is set of pairs of an index and a value, for each index there is value associated with it. ● Various categories of Aay ○ 1D, 2D and Multi-D www.eshikshak.co.in
What are Arrays ? (Cont.) ● The number of elements in the Aay is called its range. ● No matter how big an Aay is, its elements are always stored in contiguous memory locations. www.eshikshak.co.in
Array OperationsOperation DescriptionTraversal Processing each element in the AaySearch Finding the location of an element with a given valueInsertion Adding new element to an AayDeletion Removing an element from an AaySorting Organizing the elements in some orderMerging Combining two Aays into a single AayReversing Reversing the elements of an Aay www.eshikshak.co.in
Row-Major and Column-MajorArrangement ● All the elements of Aay are stored in adjacent memory. ● This leads to two possible Aangements of elements in memory ○ Row Major ○ ColumnMajor ● Base address , no. of rows ,& no. of columns helps to know any element in an Aay www.eshikshak.co.in
Algorithm for Array Traversal ● Let A be a linear Aay with Lower Bound LB and Upper Bound UB. The following algorithm traverses A applying an operations PROCESS to each element of AStep 1. Initialize Counter Set Counter = LBStep 2. Repeat steps 3 and 4 while counter <= UB Else GoTo Step 5Step 3. Visit element Apply PROCESS to A[counter]Step 4. Increase Counter Set counter = counter + 1 GoTo 2Step 5. Exit www.eshikshak.co.in
Algorithm for InsertionLet A be a Linear Array, N is number of elements, k is the positive integer suchthat k<=N, VAL to insert element at kth Position in an Array AStep 1. StartStep 2. Initialize Counter Set J = NStep 3. Repeat Steps 3 and 4 while J>=k otherwise GoTo StepStep 4. Move Jth element downward Set A[J+1] = A[J]Step 5. Decrease Counter Set J = J + 1 End of step 2 loopStep 6. Insert element Set A[k] = ITEMStep 7. Reset N Set N = N + 1Step 8. Exit www.eshikshak.co.in
Algorithm for DeletionDELETE(A, N, K, VAL)Let A be an linear Aay. N is the number of elements, k is the positiveinteger such that k<=N. The algorithm deletes kth element from theAay.Step 1. StartStep 2. Set VAL = A[k]Step 3. Repeat for J = k to N-1[Move J+1 element Upward]Set A[J] = A[J+1)End of LoopStep 4. Reset the number N of elements in ASet N = N– 1Step 5. Exit www.eshikshak.co.in
Algorithm for Linear SearchSuppose A is linear Array with N elements, and VAL is the given item ofinformation. This algorithm finds the location LOC of item in A or sets LOC=0 ifsearch is unsuccessfulStep 1. StartStep 2. [Insert VAL at the end of A] Set A[N+1] = VALStep 3. [Initialize counter] SET LOC = 1Step 4. [Search for VAL] Repeat while A[LOC] != VAL Set LOC = LOC + 1 [End of loop]Step 5. [Successful ?] if LOC = N+1 then set LOC = 0Step 6. Exit www.eshikshak.co.in
Algorithm for sortingLet A be an Aay of N elements. The following algorithm sortsthe elements of A.Step 1. StartStep 2. Repeat Steps 2 and 3 for k=1 to N-1Step 3. Set PTR = 1 [Initialize pass pointer PTR]Step 4. Repeat while PTR<=N-K [Execute Pass]a. If A[PTR] > A[PTR+1], thenInterchange A[PTR] and A[PTR+1] [End of if structure]b. Set PTR = PTR + 1[End of inner loop][End of step1 outer loop]Step 5. Exit www.eshikshak.co.in
A particular slide catching your eye?
Clipping is a handy way to collect important slides you want to go back to later.