You are on page 1of 71

Arrays, Records and Pointers

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

• Data structure are classified as either linear or nonlinera.


• A data structure is said to be linear if its elements form a
sequence, or, in other words, a linear list.
– The elements represented by means of sequential memory
location (Array)
– The elements represented by means of pointers or links
(linked list)

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Introduction 5/71

• The operation performs on linear structure –


– Traversal – Processing each element in the list
– Search – Finding the location of the element based on given
value or the record with a given key
– Insertion – Adding a new element to the list
– Deletion – Removing an element from the list
– Sorting – arranging the element in some type of order
– Merging – Combining to lists into a single list

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

• A linear array is a list of a finite number of homogeneous


data elements (i.e. data elements of the same type).
• Length Calculation:
» Length = UB-LB+1
– Example: A[10], LB = 0, UB = 9, Length = 9-0+1 = 10

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
8/56

Representation of Linear Arrays in


Memory

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Representation of Linear Arrays
in Memory 9/71

• LOC(LA[k])=address of the element LA[K] of the array LA


• Base(LA) = Base address of LA
• Formula:
– LOC(LA[K]) = Base(LA)+w(K – lower bound)
• Where w is the number of words per memory for the array LA

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

Traversing Linear Arrays

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

Inserting and Deleting

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Inserting 21/71

Insert Ford into 3rd Position

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Inserting 22/71

Insert Ford into 3rd Position

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Inserting 23/71

Insert Ford into 3rd Position

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Inserting 24/71

Insert Ford into 3rd Position

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Inserting 25/71

Insert Ford into 3rd Position

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

Delete element at position 2

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Deleting 29/71

Delete element at position 2

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Deleting 30/71

Delete element at position 2

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Deleting 31/71

Delete element at position 2

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Deleting 32/71

Delete element at position 2

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Deleting 33/71

Delete element at position 2

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Deleting 34/71

Delete element at position 2

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

Sorting: Bubble Sort

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

Small values move up


direction like bubble

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

Small values move up


direction like bubble

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

Small values move up


direction like bubble

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

Small values move up


direction like bubble

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

• Outer Loop continues n-1 times


• Inner Loop depends on outer loop.
– 1st time, Inner Loop continue n-1 times
– 2nd time, Inner Loop continue n-2 times
– .
– .
– n-1 th time, Inner loop continue 1 time
• Then we can write,
f(n) = (n-1)+(n-2)+. . . +1
= 1+2+…+(n-1)+n-n
= {n(n+1)/2}-n
= n(n-1)/2
= O(n2)

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

1 10 LOC Search ITEM = 40


2 20
3 30
4 40
N 5 50 =✗
6 40
7
8

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

Final Location, LOC = 4

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

Worst Case Complexity: f(n) = n+1 = O(n)

Average Case Complexity: f(n) = 1.(1/n)+2.(1/n)+ . . . +n(1/n) + (n+1).0

Last Element is inserted which is not in Data list, So probability = 0.

f(n) = (1+2+3+ . . . +n)/n = (n+1)/2

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Searching: Binary Search 64/71

Consider, DATA is sorted

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

Consider, DATA is sorted

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

Consider, DATA is sorted

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

Consider, DATA is sorted

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

Let, 1st step, data size will be (n/2)


2nd step, data size will be (n/2)/2 = (n/22)
3rd step, data size will be = (n/23)

Let After k steps, data size will be 1

That is, (n/2k) = 1


n = 2k
log2n = log22k
K = log2n

The complexity is k i.e. log2n (worst Case and Average Case)

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Searching: Binary Search
(Limitation) 70/71

• The algorithm requires two conditions –


1. The list must be sorted
2. One must have access to the middle element in any sublist.
• This means that one must essentially use a sorted array
to hold the data
• But keeping data in a sorted array is very expensive when
there are many insertions and deletions (array
limitation).
• In such situation, one may use a different data structure
such as a linked list or a binary search tree, to store the
data.

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
71/71

END

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers

You might also like