Professional Documents
Culture Documents
Rizoan Toufiq
Assistant Professor
Department of Computer Science & Engineering
Rajshahi University of Engineering & Technology
Rajshahi-6204
Outline 2/71
• Introduction
• Linear Array
• Representation of Linear Array in Memory
• Traversing Linear Array
• Inserting and Deleting
• Sorting: Bubble Sort
• Searching: Linear Search
• Binary Search
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
3/56
Introduction
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Introduction 4/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Introduction 5/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
6/56
Linear Arrays
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Linear Arrays 7/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
8/56
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Representation of Linear Arrays
in Memory 9/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Representation of Linear Arrays
in Memory 10/71
• Given,
• Base(AUTO)=200
• LOC(AUTO[1932])=200
• LOC(AUTO[1933]) = 204
• LOC(AUTO[1933]) = 208
• Find the LOC(AUTO[1965]) = ?
• Solution:
– K = 1965. w = 4,
– LOC(AUTO[1965]) = Base(AUTO)+w(K-
Lower Bound)
– LOC(AUTO[1965]) = 200+4(1965-1932)
= 200+4x33 = 200+132 = 332
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
11/56
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Traversing Linear Arrays 12/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Traversing Linear Arrays 13/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Traversing Linear Arrays 14/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Traversing Linear Arrays 15/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Traversing Linear Arrays 16/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Traversing Linear Arrays 17/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Traversing Linear Arrays 18/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Traversing Linear Arrays 19/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
20/56
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Inserting 21/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Inserting 22/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Inserting 23/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Inserting 24/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Inserting 25/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Inserting 26/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Inserting 27/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Deleting 28/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Deleting 29/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Deleting 30/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Deleting 31/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Deleting 32/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Deleting 33/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Deleting 34/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Deleting 35/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Deleting 36/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
37/56
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Sorting: Bubble Sort 38/71
32<51, No alter
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Sorting: Bubble Sort 39/71
32<51, No alter
51>27, alter
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Sorting: Bubble Sort 40/71
32<51, No alter
51>27, alter
51<85, No alter
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Sorting: Bubble Sort 41/71
32<51, No alter
51>27, alter
51<85, No alter
85>66, alter
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Sorting: Bubble Sort 42/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Sorting: Bubble Sort 43/71
32>27, alter
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Sorting: Bubble Sort 44/71
32>27, alter
32<51, no alter
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Sorting: Bubble Sort 45/71
32>27, alter
32<51, no alter
51<66, no alter
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Sorting: Bubble Sort 46/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Sorting: Bubble Sort 47/71
27<32, No alter
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Sorting: Bubble Sort 48/71
27<32, No alter
32<51, No alter
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Sorting: Bubble Sort 49/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Sorting: Bubble Sort 50/71
27<32, No alter
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Sorting: Bubble Sort 51/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Sorting: Bubble Sort 52/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Sorting: Bubble Sort 53/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Sorting: Bubble Sort 54/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Sorting: Bubble Sort
(Complexity) 55/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
56/56
Searching
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Searching: Linear Search 57/71
1 10 Search ITEM = 40
2 20
3 30
4 40
N 5 50
6
7 Memory Cost increased but
8 Time complexity reduced
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Searching: Linear Search 58/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Searching: Linear Search 59/71
1 10 Search ITEM = 40
2 20 LOC
3 30
4 40
N 5 50
✗
=
6 40
7
8
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Searching: Linear Search 60/71
1 10 Search ITEM = 40
2 20
3 30 LOC
4 40
N 5 50
✗
=
6 40
7
8
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Searching: Linear Search 61/71
1 10 Search ITEM = 40
2 20
3 30
4 40 LOC
N 5 50 =
6 40
7
8
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Searching: Linear Search 62/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Searching: Linear Search
(Complexity) 63/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Searching: Binary Search 64/71
1 10 Search ITEM = 40
2 20
3 30
LB = 1 and UB = N = 8
4 35
5 40 BEG = LB and END = UB
6 60 MID = INT((BEG+END)/2) = 4
7 70
N 8 80
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Searching: Binary Search 65/71
1 10 BEG
Search ITEM = 40
2 20
3 30
4 35 MID <
5 40
6 60
7 70 BEG = MID +1=5
N 8 80 END MID = 6
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Searching: Binary Search 66/71
1 10 Search ITEM = 40
2 20
3 30
4 35 >
5 40 BEG
6 60 MID
7 70 END = MID -1=5
N 8 80 END MID = 5
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Searching: Binary Search 67/71
1 10 Search ITEM = 40
2 20
3 30
4 35 =
5 40 BEG END MID
6 60
7 70
LOC := MID
N 8 80
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Searching: Binary Search 68/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Searching: Binary Search
(Complexity) 69/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Searching: Binary Search
(Limitation) 70/71
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
71/71
END
Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers