Pmscs 623p Lecture 4
Pmscs 623p Lecture 4
Pmscs 623p Lecture 4
Algorithm
(PMSCS 623P)
Lecture 4
Linear Arrays
• A linear array is a list of a finite number of n
homogeneous data elements ( that is data elements of the
same type) such that.
– The elements are of the arrays are referenced
respectively by an index set consisting of n
consecutive numbers.
– The elements of the arrays are stored respectively
in successive memory locations.
22
Linear Arrays
• The number n of elements is called the length or size
of the array.
• The index set consists of the integer 0,1, 2, …n-1
• Length or the number of data elements of the array
can be obtained from the index set by
Length = UB – LB + 1
where
UB is the largest index called the upper bound and
LB is the smallest index called the lower bound of the arrays
33
Linear
• Element of an array A may
Arrays
be denoted by
– Subscript notation A1, A2, , …. , An
– Parenthesis notation A(1), A(2), …. , A(n)
– Bracket notation A[1], A[2], ….. , A[n]
• The number K in A[K] is called subscript or an index and A[K]
is called a subscripted variable.
44
Declaring and Initializing
Arrays
Initializing Arrays
int ar[5];
ar[0]=10; ar[1]=20;
5
Representation of Linear Array in Memory
16
0
Representation of Linear Array in Memory
7
Representation of Linear Array in Memory
8
Example 1
200
Find the address for LA[16] Each
201 LA[0]
element of the array occupy 2
byte 202
203 LA[1]
204
LA[2]
205
206
LA[3]
207
11
The Search Problem
• Given an array A storing n numbers, and a
target number v, locate the position in A
(if it exists) where A[i] = v
• Example
– Input: A = {3,8,2,15,99,52}, v = 99
– Output: position 4
• Notes
– Array positions (indexes) go from 0..n-1
– When the target does not exist in the array, return an
undefined position, such as -1
12
Algorithm 1: Linear Search
Algorithm LinearSearch(A,v):
Input: An array A of n numbers, search
target v
Output: position i where A[i]=v, -1 if not
found
for i 0 to n - 1 do
if A[i] = v then
return i
return -1
13
Linear Search time complexity
• Worst-case: O( n )
– If v does not exist or if v is the last element in the
array
• Best-case: O( 1 )
– When the target element v is the first element in the
array
• Average-case: O( n )
– e.g., if the target element is somewhere in the middle
of the array, the for-loop will iterate n/2 times. Still
O( n )
14
Binary Search (recursive)
Binary Search (iteration)
16
Retrieving an Item from Sorted List
• Find 84
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Retrieving an Item from Sorted List
• Find 84
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
first last
• Step 1
Retrieving an Item from Sorted List
• Find 84
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
• Step 1
Retrieving an Item from Sorted List
• Find 84
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
• Step 1
Retrieving an Item from Sorted List
• Find 84
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
first last
• Step 2
Retrieving an Item from Sorted List
• Find 84
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
• Step 2
Retrieving an Item from Sorted List
• Find 84
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
• Step 2
Retrieving an Item from Sorted List
• Find 84
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
first last
• Step 3
Retrieving an Item from Sorted List
• Find 84
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
first last
mid
• Step 3 =(8+10)/2
Retrieving an Item from Sorted List
• Find 84
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
first last
mid
• Step 3 =(8+10)/2
Retrieving an Item from Sorted List
• Find 84
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
first last
• Step 4
Retrieving an Item from Sorted List
• Find 84
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
first last
mid
=(10+10)/2
• Step 4
• 84 found at the midpoint
Retrieving an Item from Sorted List
• Find 73
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Retrieving an Item from Sorted List
• Find 73
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
first last
• Step 1
Retrieving an Item from Sorted List
• Find 73
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
• Step 1
Retrieving an Item from Sorted List
• Find 73
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
• Find 73
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
first last
• Step 2
Retrieving an Item from Sorted List
• Find 73
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
• Step 2
Retrieving an Item from Sorted List
• Find 73
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
• Step 2
Retrieving an Item from Sorted List
• Find 73
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
first last
• Step 3
Retrieving an Item from Sorted List
• Find 73
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
first last
mid
• Step 3 =(8+10)/2
Retrieving an Item from Sorted List
• Find 73
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
first last
mid
• Step 3 =(8+10)/2
Retrieving an Item from Sorted List
• Find 73
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
first last
• Step 4
Retrieving an Item from Sorted List
• Find 73
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
first last
• Step 4
mid
=(10+10)/2
Retrieving an Item from Sorted List
• Find 73
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
• Step 5
last first
That shows the worst case is when you reach N/2x where x is such that 2x
= N . Now since mathematically worst case is when the value of
Data Structures
Complexity of the Binary Search Algorithm
The Complexity of the Binary Search algorithm is measured by the
maximum (worst case) number of Comparisons it performs for
searching operations. The searched array is divided by 2 for each
comparison/iteration. Therefore, we require at most f(n) comparisons
to locate ITEM:
2f(n) > n or equivalently,
log2 n f(n) = +1
Approximately, equal to log2n.
Example:
If a given sorted array contains 1024 elements, then the maximum
number of comparisons required is:
log2(1024) = 10 (only 10 comparisons are
enough)
46
Data Structures
Limitations of the Binary Search Algorithm
47
Data Structures
Insertion into Arrays
Inserting an element somewhere in the middle of the array require that each
subsequent element be moved downward to new locations to accommodate the new
element and keep the order of the other elements.
Algorithm 4.2: (Inserting into a linear array)
Here LA is linear array with N elements and K is a positive integer
such that K<=N. This algorithm inserts an element ITEM into the K-th
position in LA.
Step 1. Set J:=N
Step 2. Repeat Steps 3 and 4 while J>=K
Step 3. Set LA [J+1]: =LA [J]
Step 4. Set J:=J-1
[End of step 2 loop]
Step 5. Set LA [K]: =ITEM
Step 6. Set N:=N+1
Step 7. Exit
48
Data Structures
Deletion of elements from linear arrays
Deleting an element somewhere in the middle of the array requires
that each subsequent element be moved one location upward in order to “fill
up” the array.
49
"Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-wise
comparisons and swapping
1 2 3 4 5 6
77 42 35 12 101 5
"Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-wise
comparisons and swapping
1 2 3 4 5 6
42 Swap 42
77 77 35 12 101 5
"Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-wise
comparisons and swapping
1 2 3 4 5 6
42 7735 Swap 35
77 12 101 5
"Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-wise
comparisons and swapping
1 2 3 4 5 6
42 35 12 Swap 12
77 77 101 5
"Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-wise
comparisons and swapping
1 2 3 4 5 6
42 35 12 77 101 5
No need to swap
"Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-wise
comparisons and swapping
1 2 3 4 5 6
42 35 12 77 5 Swap 101
101 5
"Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-wise
comparisons and swapping
1 2 3 4 5 6
42 35 12 77 5 101
12
6 12
6 22
14 22
14
8 8
17
22 17
22
6 12 14
8 14
8 17 22
6 8
12 12
8 14 17 22
6 8 12 14 17 22
Try one!
15 3 11 19 4 7
Data Structures
Bubble Sort
Suppose the following numbers are stored in an array A:
A: 32, 51, 27, 85, 66, 23, 13, 57
Apply the bubble sort to the array A. Show each pass separately.
Pass 1: (a) Compare A1 and A2. Since 32< 51, the list is not altered.
A: 32, 51, 27, 85, 66, 23, 13, 57
(b) Compare A2 and A3. Since 51>27, interchange 51 and 27.
A: 32, 27, 51, 85, 66, 23, 13, 57
(c) Compare A3 and A4. Since 51<85, the list is not altered.
A: 32, 27, 51, 85, 66, 23, 13, 57
(d) Compare A4 and A5. Since 85>66, interchange 85 and 66.
A: 32, 27, 51, 66, 85, 23, 13, 57
61
Data Structures
Bubble Sort
Suppose the following numbers are stored in an array A:
A: 32, 51, 27, 85, 66, 23, 13, 57
Apply the bubble sort to the array A. Show each pass separately.
Pass 1: (e) Compare A5 and A6. Since 85>23, interchange 85 and 23.
A: 32, 27, 51, 66, 23, 85, 13, 57
(f) Compare A6 and A7. Since 85>13, interchange 85and 13.
A: 32, 27, 51, 66, 23, 13, 85, 57
(g) Compare A7and A8. Since 85>57, interchange 85 and 57.
A: 32, 27, 51, 66, 23, 13, 57, 85
62
Data Structures
Bubble Sort
Suppose the following numbers are stored in an array A:
A: 32, 51, 27, 85, 66, 23, 13, 57
Apply the bubble sort to the array A. Show each pass separately.
63
Data Structures
Bubble Sort
Suppose the following numbers are stored in an array A:
A: 32, 51, 27, 85, 66, 23, 13, 57
Apply the bubble sort to the array A. Show each pass separately.
64
Data Structures
Bubble Sort
Suppose the following numbers are stored in an array A:
A: 32, 51, 27, 85, 66, 23, 13, 57
Apply the bubble sort to the array A. Show each pass separately.
65
Data Structures
Bubble Sort
Suppose the following numbers are stored in an array A:
A: 32, 51, 27, 85, 66, 23, 13, 57
Apply the bubble sort to the array A. Show each pass separately.
66
Data Structures
Bubble Sort
Suppose the following numbers are stored in an array A:
A: 32, 51, 27, 85, 66, 23, 13, 57
Apply the bubble sort to the array A. Show each pass separately.
67
Data Structures
Bubble Sort
Suppose the following numbers are stored in an array A:
A: 32, 51, 27, 85, 66, 23, 13, 57
Apply the bubble sort to the array A. Show each pass separately.
68
Data Structures
Bubble Sort
Suppose the following numbers are stored in an array A:
A: 32, 51, 27, 85, 66, 23, 13, 57
Apply the bubble sort to the array A. Show each pass separately.
69
Data Structures
Algorithm 4.4: (Bubble Sort) BUBBLE (DATA, N)
Here DATA is an Array with N elements. This algorithm sorts the
elements in DATA.
70
Data Structures
Complexity of the bubble sort algorithm
71
Data Structures
Solved Problem 4.9: Using the bubble sort algorithm, find the number C of
comparisons and the number D of interchanges which alphabetize the n=6
letters in PEOPLE.
Pass 1: P E O P L E, E P O P L E, EOPPLE
E O P P L E, E O P L P E, EOP
LEP
Pass 2: E O P L E P, E O P L E P, EOPLEP
E O L P E P, EOLEPP
Pass 3: E O L E P P, E O L E P P, ELOEPP
ELEOPP
Pass 4: E L E O P P, E L E O P P, EEL
OPP
Pass 5: E E L O P P, EELOPP
72
Data Structures
Solved Problem 4.9: Using the bubble sort algorithm, find the number C of
comparisons and the number D of interchanges which alphabetize the n=6
letters in PEOPLE.
73
Data Structures
Solved Problem 4.11: Suppose multidimensional arrays A and B are declared
using
A(-2:2, 2:22) and B(1:8, -5:5, -10;5)
(a) Find the length of each dimension and the number of elements in A
and B.
(b) Consider the element B[3, 3, 3] in B . Find the effective indices E 1, E2,
E3 and the address of the element, assuming Base(B)=400 and there are w=4
words per memory location.
Solution:
(a) Length = UB – LB +1
Hence, the lengths of the dimensions of A are:
L1 = 2-(-2)+1=5 and L2 = 22 -2+1=21
Accordingly, A has 5.21=105 elements.
74
Data Structures
Solved Problem 4.11: Suppose multidimensional arrays A and B are declared
using
A(-2:2, 2:22) and B(1:8, -5:5, -10;5)
(b) Consider the element B[3, 3, 3] in B . Find the effective indices E 1, E2,
E3 and the address of the element, assuming Base(B)=400 and there are w=4
words per memory location.
Solution:
(b) The effective index Ei is obtained from Ei=ki – LB, where ki is the
given
index and LB is the lower bound . Hence,
E1=3-1=2 E2=3-(-5)=8 E3=3-(-10)=13
The address depends on whether the programming language stores B in
row-major order or column-major order. Assuming B is the stored in column–
major order, we get:
E3 L2 =13.11=143 E3 L2 + E2 =143+8=151
(E3 L2 + E2)L1 =151.8=1208 (E3 L2 + E2)L1 +E1 =1208+2=1210
Therefore, LOC(B[3,3,3])=400+4(1210)=400+4840=5240
75