Download presentation
Presentation is loading. Please wait.
Published byAbhishek Sharma Modified over 2 years ago
1
SIMPLE LINEAR DATA STRUCTURE ARRAY UNIT - 1
2
LINEAR ARRAY 1/31/2023 Bhavana Vishwakarma 2 A linear array is a list of finite number ‘n’ of homogeneous data elements such that: (a) The elements of the array are referenced respectively by an index set consisting of ‘n’ consecutive elements. (b) The elements of the array are stored respectively in successive memory locations. The length of the array is obtained by: Length = UB – LB + 1
3
REPRESENTATION OF LINEAR ARRAYS IN MEMORY 1/31/2023 Bhavana Vishwakarma 3 Let ‘B’ be a linear array in the memory of the computer. Let LOC(B[K]) denotes the address of the element B[K] of the array B. Let Base(B) denotes the address of the first element of B called the Base address. The address of the element of ‘B’ is calculated using the formula: LOC(B[K]) = Base(B) + w(K – lower bound) Where, ‘w’ is the no. of words per memory cell for the array B.
4
TRAVERSING LINEAR ARRAYS 1/31/2023 Bhavana Vishwakarma 4 Let us consider ‘A’ as a linear array. Traversing the array ‘A’ means accessing and processing each element exactly once. Algorithm: 1. [Initial counter.] Set K := LB. 2. Repeat Step 3 and 4 while K <=UB. 3. [Visit element.] Apply PROCESS to LA[K]. 4. [Increase counter.] Set K := K + 1, 5. [End of Step 2 loop.] 6. Exit. Alternative Algorithm: 1. Repeat for K = LB to UB: Apply PROCESS to LA[K]. [End of loop.] 2. Exit.
5
INSERTING & DELETING 1/31/2023 Bhavana Vishwakarma 5 Inserting an element at the end. Inserting an element in the middle of the array. Deleting an element from the end. Deleting from the middle. Inserting refers to the operation of adding another element to the array. Deleting refers to the operation of removing one of the elements from the array.
6
Algorithm: (Insert into a Linear Array) 1/31/2023 Bhavana Vishwakarma 6 INSERT ( LA, N, K, ITEM) 1. [Initialize counter.] Set J := N. 2. Repeat Steps 3 and 4 while J ≥K. 3. [Move Jth element downward.] Set LA[J+1] = LA[J]. 4. [Decrease counter.] Set J := J – 1. [End of Step2 loop.] 5. [Insert element.] Set LA[K] := ITEM. 6. [Reset N.] Set N := N + 1. 7. Exit.
7
Algorithm: (Deleting from a Linear array) 1/31/2023 Bhavana Vishwakarma 7 DELETE ( LA, N, K, ITEM) 1. Set ITEM := LA[K]. 2. Repeat for J = K to N – 1: [Move J+1 st element upward.] Set LA[J] := LA[J+1]. [End of loop.] 3. [Reset the number N of elements in LA.] Set N := N – 1. 4. Exit.
8
SEARCHING 1/31/2023 Bhavana Vishwakarma 8 Searching refers to the operation of finding the location of a given item to search in the list of given items. Let DATA be a given collection of data elements in memory, and suppose a specific ITEM of information is given to search. The search is said to be successful if ITEM does appear in DATA and unsuccessful otherwise. If a search is unsuccessful, then the element ITEM should be added to DATA so that the search may be successful next time.
9
LINEAR SEARCH(Algorithm) 1/31/2023 Bhavana Vishwakarma 9 LINEAR( DATA, N, ITEM, LOC) 1. [Insert ITEM at the end of DATA.] Set DATA[N] := ITEM. 2. [Initialize counter.] Set LOC := 0. 3. [Search for ITEM.] Repeat while DATA[LOC] ≠ ITEM: Set LOC := LOC + 1. [End of loop.] 4. [Successful.] If LOC = N, then: Set LOC :=0. 5. Exit.
10
Complexity of the Linear Search Algorithm Measured in terms of the number f(n) of comparisons required to find ITEM in DATA where DATA contains ‘n’ elements. Types of Cases – Worst Case - It occurs when the entire array DATA is searched, but ITEM does not appear in DATA. In this case, the algorithm requires f(n) = n + 1 comparisons. Average Case – Uses the probabilistic notion of expectation. Suppose p k is the probability that ITEM appears in DATA[k], and suppose ‘q’ is the probability that ITEM does not appear in DATA. p 1 + p 2 +p 3 + ---- +p n + q = 1 The average no. of comparisons is given by: f(n) = 1.p 1 + 2.p 2 + ---- +n.p n + (n+1).q 1/31/2023 Bhavana Vishwakarma 10
11
BINARY SEARCH The item or the location can be search quickly by reducing the no. of possible locations for it in the array. The search begins with the middle of the array. If item is found then search is stopped. Otherwise: If it is smaller than the middle element, then upper bound is shifted to middle-1. If it is greater than the middle element, then lower bound is shifted to middle+1. 1/31/2023 Bhavana Vishwakarma 11
12
BINARY SEARCH(Algorithm) 1/31/2023 Bhavana Vishwakarma 12 BINARY( DATA, LB, UB, ITEM, LOC) 1. SET BEG := LB, END := UB, MID = INT(BEG+END)/2. 2. Repeat Steps 3 and 4 while BEG≤END and DATA[MID]≠ITEM 3. If ITEM<DATA[MID], then: Set END := MID -1. Else: Set BEG := MID +1. [End of If structure.] 4. Set MID = INT(BEG+END)/2. [End of Step 2 loop.]
13
5. If DATA[MID] = ITEM, then: Set LOC := MID. Else: Set LOC := NULL. [End of If structure.] 6. Exit. 1/31/2023 Bhavana Vishwakarma 13 (...CONTD)BINARY SEARCH(Algorithm) Complexity of the Binary Search Algorithm f(n) = [log 2 n] + 1
14
BUBBLE SORT Algorithm: (Bubble Sort) BUBBLE (DATA, N) 1. Repeat Steps 2 and 3 for K = 1 to N-1. 2. Set PTR := 1. [Initializes pass pointer PTR.] 3. Repeat while PTR ≤ N – K. a.If DATA[PTR] > DATA[PTR+1], then: Interchange DATA[PTR] & DATA[PTR+1]. [End of IF structure.] b.Set PTR := PTR + 1. [End of inner loop.] [End of Step 1 loop.] 4. Exit. 1/31/2023 Bhavana Vishwakarma 14
15
SELECTION SORT Procedure: MIN(A, K, N, LOC) 1. Set MIN : = A[K] and LOC:=K. 2. Repeat for J = K+1, K+2,….,N. If MIN > A[J], then: Set MIN := A[J] and LOC:=J. [End of loop.] 3. Return. 1/31/2023 Bhavana Vishwakarma 15
16
SELECTION SORT Algorithm: (Selection Sort) SELECTION (A, N) 1. Repeat Steps 2 and 3 for K = 1,2,3,…,N-1. 2. Call MIN(A,K,N,LOC). 3. [Interchange A[K] and A[LOC].] Set TEMP := A[K], A[K]:=A[LOC] and A[LOC]:=TEMP. [End of Step 1 loop.] 4. Exit. 1/31/2023 Bhavana Vishwakarma 16 Complexity of the Binary Search Algorithm f(n) = (n-1) + (n-2) +….+2 + 1 = {(n-1).n}/2 =O(n 2 )
17
INSERTION SORT Algorithm: INSERTION(A, N) 1. Set K := 2. 2. Repeat Steps 3 to 5 for K = 2,3,…, N. 3. Set TEMP := A[K] and PTR := K – 1. 4. Repeat while TEMP<A[PTR] (a) Set A[PTR+1] := A[PTR]. (b) Set PTR := PTR – 1. [End of Loop.] 5. Set A[PTR + 1] := TEMP. [End of Step 2 loop.] 6. Exit 1/31/2023 Bhavana Vishwakarma 17
18
QUICK SORT Also known as Partition Exchange Sort. Works by partitioning the array to be sorted. One of the array elements is chosen as a key value. Key element divides the main list into two parts such that: One partition contains elements smaller than the key value. Another partition contains elements larger than the key value. Two pointers, up(high) and down(low) are initialized to the upper and lower bounds. 1. Repeatedly increase the pointer down by one position until x[down]>a. 2. Repeatedly decrease the pointer up by one position x[up]<=a. 3. If up>down, interchange x[down] with x[up]. 1/31/2023 Bhavana Vishwakarma 18
19
QUICK SORT Algorithm: Quick_Sort (a, l, h) 1. [Initially] low = l. high = h. key = a[(l+h)/2]. 2. Repeat through step7 while(low ≤ high) 3. Repeat step4 while(a[low] < key) 4. Low = low + 1. 5. Repeat step6 while(a[high]>key) 6. High = high – 1. 1/31/2023 Bhavana Vishwakarma 19
20
QUICK SORT 7. If (low ≤ high) (i) temp = a[low] (ii) a[low] = a[high] (iii) a[high] = temp (iv) low = low +1 (v) high = high – 1 8. If (l < high) Quick_Sort(a, l, high) 9. If (low < h) Quick_Sort(a, low, h) 1/31/2023 Bhavana Vishwakarma 20
21
MERGING Merging is a process in which two lists are merged to form a new list. Suppose, A is a sorted list with ‘r’ elements and B is a sorted list with ‘s’ elements. The operation that combines the elements of A and B into a single sorted list C with ‘n=r+s’ elements is called merging. 1/31/2023 Bhavana Vishwakarma 21
22
MERGING Algorithm: MERGING(A, R, B, S, C) 1. [Initialize.] Set NA := 1 and PTR := 1. 2. [Compare.] Repeat while NA ≤ R and NA ≤ S. If A[NA] < B[NB], then: (a) [Assign element from A to C.] Set C[PTR] := A[NA]. (b) [Update pointers.] Set PTR := PTR + 1 and NA := NA + 1 Else: (a) [Assign element from B to C.] Set C[PTR] := B[NB]. (b) [Update pointers.] Set PTR := PTR + 1 and NB := NB + 1 [End of If structure.] [End of loop.] 1/31/2023 Bhavana Vishwakarma 22
23
MERGING 3. [Assign remaining elements to C.] If NA > R, then: Repeat for K = 0, 1,2,…,S – NB: Set C[PTR+K] := B[NB+K]. [End of loop.] Else: Repeat for K = 0, 1,2,…,R – NA: Set C[PTR+K] := A[NA+K]. [End of loop.] [End of If structure.] 4. Exit. 1/31/2023 Bhavana Vishwakarma 23
24
MERGE SORT Procedure: MERGEPASS(A, N, L, B) 1. Set Q:= INT(N/(2*L)), S:= 2*L*Q and R:=N – S. 2. Repeat for J = 1, 2, …, Q. a. Set LB := 1 + (2*J – 2) * L. b. Call MERGE(A, L, LB, A, L, LB + L, B, LB). [End of loop.] 3. If R ≤ L, then: Repeat for J = 1, 2, …, R: Set B(S + J) := A(S + J). [End of loop.] Else: Call MERGE(A, L, S+1, A, R, L+S+1,B,S+1). [End of If structure.] 4. Return. 1/31/2023 Bhavana Vishwakarma 24
25
MERGE SORT Algorithm: MERGESORT(A, N) 1. Set L := 1. 2. Repeat Steps 3 to 6 while L < N: 3. Call MERGEPASS(A, N, L, B). 4. Call MERGEPASS(B, N, 2*L, A). 5. Set L := 4*L. [End of Step 2 loop.] 6. Exit. 1/31/2023 Bhavana Vishwakarma 25
26
1/31/2023 Bhavana Vishwakarma 26
27
col1 col2 col3 col4 col5 col6 row0 row1 row2 row3 row4 row5 8/36 6*65*3 15/15 sparse matrix data structure? Sparse Matrices 1/31/2023 27 Bhavana Vishwakarma
28
Sparse Matrices Matrices with a relatively high proportion of zero entries. Two general types of n-square sparse matrices. Matrix where non-zero entries can occur only on the diagonal or on elements immediately above or below the diagonal is called a tri-diagonal matrix. 4-3 5-56 06-9 -430 2-8
29
Sparse Matrices (cont…) Matrix where all entries above the main diagonal are zero or equivalently where non-zero entries can occur only on or below the main diagonal is called a lower triangular matrix. 4 5-5 306 10-43 684-5-8
30
Sparse Matrices (cont…) The natural method of representing matrices as two dimensional arrays may not be suitable for sparse matrices. That is one may save space by storing only those entries which may be non-zero. One of the basic methods for storing such a sparse matrix is to store non-zero elements in a one dimensional array and to identify each array element with row and column indices.
31
Sparse Matrices (cont…)
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.