Arraylineardatastructure21 110805065315-phpapp02

11
Array(ADT) Linear Data Structure www.eshikshak.co.in

description

 

Transcript of Arraylineardatastructure21 110805065315-phpapp02

Page 1: Arraylineardatastructure21 110805065315-phpapp02

Array(ADT)Linear Data Structure

www.eshikshak.co.in

Page 2: Arraylineardatastructure21 110805065315-phpapp02

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

Page 3: Arraylineardatastructure21 110805065315-phpapp02

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

Page 4: Arraylineardatastructure21 110805065315-phpapp02

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

Page 5: Arraylineardatastructure21 110805065315-phpapp02

Array Operations

Operation 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

Page 6: Arraylineardatastructure21 110805065315-phpapp02

Row-Major and Column-Major Arrangement

● 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

Page 7: Arraylineardatastructure21 110805065315-phpapp02

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 A

Step 1. Initialize Counter

Set Counter = LB

Step 2. Repeat steps 3 and 4 while counter <= UB

Else GoTo Step 5

Step 3. Visit element

Apply PROCESS to A[counter]

Step 4. Increase Counter

Set counter = counter + 1

GoTo 2

Step 5. Exit

www.eshikshak.co.in

Page 8: Arraylineardatastructure21 110805065315-phpapp02

Algorithm for InsertionLet A be a Linear Array, N is number of elements, k is the positive integer such that k<=N, VAL to insert element at kth Position in an Array A

Step 1. Start

Step 2. Initialize Counter

Set J = N

Step 3. Repeat Steps 3 and 4 while J>=k otherwise GoTo Step

Step 4. Move Jth element downward

Set A[J+1] = A[J]

Step 5. Decrease Counter

Set J = J + 1

End of step 2 loop

Step 6. Insert element

Set A[k] = ITEM

Step 7. Reset N

Set N = N + 1

Step 8. Exit

www.eshikshak.co.in

Page 9: Arraylineardatastructure21 110805065315-phpapp02

Algorithm for DeletionDELETE(A, N, K, VAL)Let A be an linear Aay. N is the number of elements, k is the positive integer such that k<=N. The algorithm deletes kth element from the Aay.

Step 1. Start

Step 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 Loop

Step 4. Reset the number N of elements in A

Set N = N– 1Step 5. Exit

www.eshikshak.co.in

Page 10: Arraylineardatastructure21 110805065315-phpapp02

Algorithm for Linear SearchSuppose A is linear Array with N elements, and VAL is the given item of information. This algorithm finds the location LOC of item in A or sets LOC=0 if search is unsuccessful

Step 1. Start

Step 2. [Insert VAL at the end of A]

Set A[N+1] = VAL

Step 3. [Initialize counter]

SET LOC = 1

Step 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 = 0

Step 6. Exit

www.eshikshak.co.in

Page 11: Arraylineardatastructure21 110805065315-phpapp02

Algorithm for sortingLet A be an Aay of N elements. The following algorithm sorts the 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