CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING
Mar 30, 2015
CSI 1306
ALGORITHMS - PART 4LIST PROCESSING
ListsSometimes a problem deals with a list of values We represent such a list with a single name, and
use subscripts to indicate the individual members of the list
For example, if X is a list of size N, then the individual members are:
X1 , X2 , X3 ,………., XN
Lists
Always include the size of the list as part of the information for the algorithm – remember storage? How much room must the
computer set aside for our list?
– there are techniques for working with lists of unknown length, but they are beyond the scope of this course.
– either you will know the length of the list or you will be able to calculate it
Algorithm 4.1
Write an algorithm that returns the Kth element of a given list L, with N numbers
– NAME: FindK– GIVENS: L, N, K
• Change: None– RESULTS: Val– INTERMEDIATES: None– DEFINITION: Val := FindK(L, N, K)– -------------------------– METHOD:
Get L, NGet K
Let Val = LK
Give Val
Algorithm 4.2
Write an algorithm that replaces the Kth element`s value of a given list L (with N numbers) with the given value M– NAME: ChangeK– GIVENS: L, N, K, M
• Change: L– RESULTS: None– INTERMEDIATES: None– DEFINITION: ChangeK(L, N, K, M)– -------------------------– METHOD:
Get L, NGet KGet M
Let LK = M
Give L
Trace 4.1
Trace Algorithm 4.2 with a list L of 5 elements (1, 8, 3, 6, 2) where K is 2 and M is 20
METHOD:(1) Get L, N(2) Get K(3) Get M
(4) Let LK = M
(5) Give L
Ln L N K M 1 (1,8,3,6,2) 5 2 2 3 20 4 (1,20,3,6,2) 5 output (1,20,3,6,2)
Algorithm 4.3
Given a list, L, of N numbers, where N is odd, find the middle number in the list
– NAME: Middle– GIVENS: L, N
• Change: None– RESULTS: Mid– INTERMEDIATES: Loc– DEFINITION: Mid := Middle(L, N)– -------------------------– METHOD:
• Get L, N
• Let Loc = N div 2 + 1• Let Mid = Lloc
• Give Mid
Trace 4.2
Trace Algorithm 4.3 with a list L of 5 elements (1, 8, 3, 6, 2)
METHOD:(1) Get L, N(2) Let Loc = N div 2 + 1(3) Let Mid = Lloc
(4) Give Mid
LN L N Loc Mid
1 (1,8,3,6,2) 5
2 3
3 3
4 output 3
List Processing
Remember that we represent a list with a single name and use subscripts to indicate the individual members of the list
Processing a list usually involves a loop in which we use the list subscript variable as the loop control variable
Frequently, we also use Boolean operators in our loop tests– The next slide reviews use of these operators
List Processing
Assume that the Boolean variables X, Y and Z are set to the following values:X = True, Y = False, Z = True
The order of precedence for Boolean operators is Not, And, Or
What is the result of each of the following expressions:(X and Y) or Z = TRUE(True or Y) and False = FALSEX or True and not Y or Z = TRUEnot(X and not Z or False and (True and Y)) = TRUEY and Y or not Z = FALSE
Algorithm 4.4
Given a list X of N numbers, find the sum of the numbers in X
Analysis– Basic Algorithm
• SUM• Let Total = 0• Let Total = Total + ????
– Total = X1 + X2 + …….. + XN – Total = Total + XI
• Let I go from 1 to N in a loop; then we will add each X I
• Loop control variable is the list subscript variable
Algorithm 4.4
Given a list X of N numbers, find the sum of the numbers in X– Name:SUMLIST
– Given: X, N
• Change: None
– Result: Total
– Intermediate: I
– Definition
• Total := SUMLIST(X,N)
Method
Get X, N
Let Total = 0
Let I = 1
Loop When (I <= N)
Let Total = Total + XI
Let I = I + 1
Finish Loop
Give Total
Trace 4.3
Trace algorithm 4.4 with the list (3,7,5). N is 3.
(1) Get X, N
(2) Let Total = 0
(3) Let I = 1
(4) Loop When (I <= N)
(5) Let Total = Total + XI
(6) Let I = I + 1
(7) Finish Loop
(8) Give Total
LN X N I Total Test 1 (3, 7, 5) 3 2 0 3 1
4 (1 <= 3)5 36 2 4 (2 <= 3) 5 106 3
4 (3 <= 3)5 156 44 (4 <= 3)
8 Output 15
Algorithm 4.5
Given a list X of N numbers, determine whether the given value V occurs in the list X.
Analysis– Basic Algorithm
• SEARCH
• Let Found = False
• Does XI = V?
– Stop the index at this point
» FOUND at XI
– I to process the list
Algorithm 4.5
Given a list X of N numbers, determine whether the given value V occurs in the list X.Name: SEARCHX
Given: X, N, V
Change: None
Result: Found
Intermediate: I
Definition:
Found := SEARCHX(X,N,V)
Method Get X, N Get V Let Found = False Let I = 1 Loop If (XI = V) Let Found = True Else Let I = I + 1
Finish Loop When (I >N) or (Found)
Give Found
Trace 4.4
Trace algorithm 4.5 with the list (3,7,5) and with a value of 7 for V
(1) Get X, N(2) Get V(3) Let Found = False(4) Let I = 1(5) Loop(6) If (XI = V)(7) Let Found = True(8) Else(9) Let I = I + 1
(10) Finish Loop When (I >N) or Found
(11) Give Found
LN X N V I Found Test 1 (3,7,5) 3 2 7 3 False 4 1 6 (3 = 7) 9 2 10 (2>3) or (F)
6 (7 = 7) 7 True10 (2>3) or (T) 11 Output True
Algorithm 4.6
Given a list X of N positive numbers, find the maximum number and its position in X
Analysis– Basic Algorithm
• MAX
• Let Max = -1
• (XI > Max)?
– I for list processing• Must track not only the Max, but also the location of Max (the
value of I)
Algorithm 4.6
Given a list X of N positive numbers, find the maximum number and its position in X
– Name: MAXLIST
– Given: X, N
• Change:None
– Result: Max, Loc
– Intermediate: I
– Definition:
– (Loc,Max) := MAXLIST(X,N)
Method Get X, N Let Max = -1 Let I = 1
Loop When (I <= N) If (XI > Max) Let Max = XI
Let Loc = I Let I = I + 1 Finish Loop
Give MaxGive Loc
Trace 4.5 Trace algorithm 4.6 with the list (8,3,25,9)
(1) Get X, N(2) Let Max = -1(3) Let I = 1
(4) Loop When (I <= N)(5) If (XI > Max)(6) Let Max = XI
(7) Let Loc = I (8) Let I = I + 1(9) Finish Loop
(10) Give Max(11) Give Loc
LN X N I MAX LOC TEST
1,2,3 (8,3,25,9) 4 1 -1
4 (1 <= 4)
5 (8 > -1)
6 8
7 1
8 2
4 (2 <= 4)
5 (3 > 8)
8 3
4 (3 <= 4)
5 (25>8)
6 25
7 3
8 4
4 (4 <= 4)
5 (9>25)
8 5
4 (5 <= 4)
10 Output 25
11 Output 3
Algorithm 4.7
Given a list X of N numbers, find the number of times zero occurs in the list
Analysis– Basic Algorithm
• COUNT
• Let Count = 0
• XI = 0?
• Let Count = Count + 1 if a match
– I for list processing
Algorithm 4.7
Given a list X of N numbers, find the number of times zero occurs in the list
– Name: NUM0
– Given: X, N
• Change: None
– Result: Count
– Intermediate: I
– Definition:
– Count := NUM0(X,N)
Method
Get X, N
Let Count = 0
Let I = 1
Loop When (I <= N)
If (XI = 0)
Let Count = Count + 1
Let I = I + 1
Finish Loop When
Give Count
Algorithm 4.8
Given a list X of N numbers, see if X contains any two numbers which are the same
Analysis– Basic Algorithm
• Variant of SEARCH• Compare each XI with every other element of the list• Loop within a Loop
– For each XI
» Search for XI
• Search only until one match is found– Stop the Indexes at the locations
Algorithm 4.8
Given a list X of N numbers, see if X contains any two numbers which are the same– Name: DUPLICATE– Given: X, N
• Change: None– Result: Double– Intermediate: I, Test– Definition:– Double := DUPLICATE(X,N)
Method Get X, N Let Double = False Let Test = 1 Loop Let I = Test + 1 Loop If (XTest = XI) Let Double = True Else Let I = I + 1 Finish Loop When (I >N) or (Double) If (Not (Double)) Let Test = Test + 1 Finish Loop When (Test = N) or (Double)
Give Double
Trace 4.6 Trace algorithm 4.8 with the list
(2,8,7,8)(1) Get X, N(2) Let Double = False(3) Let Test = 1(4) Loop(5) Let I = Test +1(6) Loop(7) If (XTest = XI)(8) Let Double = True(9) Else(10) Let I = I + 1(11) Finish Loop When (I >N) or (Double)(12) If (Not(Double))(13) Let Test = Test + 1(14) Finish Loop When (Test = N) or (Double)
(15) Give Double
LN X N Double Test I TEST1,2,3 (2,8,7,8) 4 False 1 5 2 7 (2=8)10 311 (3>4)or(F) 7 (2=7)10 411 (4>4)or(F) 7 (2=8)10 511 (5>4)or(F)12 Not (F)13 214 (2=4)or(F) 5 3 7 (8=7)10 411 (4>4)or(F) 7 (8=8) 8 True11 (5>4)or(T)12 Not (T)14 (3=4)or(T)15 Output True
Grouped Lists
If we want to process multiple, related lists (as in a database), we must maintain the order relationship between elements in each list
E.g. Student grades– Name - Midterm - Final
– Ann - 100 - 30
– Bob - 75 - 28
– Cathy - 60 - 90
– Dave - 40 - 80
Grouped Lists
We have three lists– Name = (Ann, Bob, Cathy, Dave)
– Midterm = (100, 75, 60, 40)
– Final = (30, 28, 90, 80)
Since Ann is Name1, then her midterm mark would be Midterm1 and her final mark would be Final1
We can also create a new list from other lists.
Algorithm 4.9
Given two lists, Midterm and Final, of N numbers each, calculate a Final grade of 75% Final and 25% Midterm
– Name: FINDGRADE
– Given: M, F, N
• Change: None
– Result: G
– Intermediate: I
– Definition:
– G := FINDGRADE(M,F,N)
Method
Get M, F, N
Let I = 1
Loop When (I <= N)
Let GI = 0.75*FI + 0.25*MI
Let I = I + 1 Finish Loop
Give G
Trace 4.7
Trace algorithm 4.9 with the grouped list defined previously
Method
(1) Get M, F, N
(2) Let I = 1
(3) Loop When (I <= N)
(4) Let GI = 0.75*FI + 0.25*MI
(5) Let I = I + 1(6) Finish Loop
(7) Give G
F = (30, 28, 90, 80)
M = (100, 75, 60, 40)
LN I N G Test
1,2 1 4 (??,??,??,??)
3 (1 <= 4)
4 (48,??,??,??)
5 2
3 (2 <= 4)
4 (48,40,??,??)
5 3
3 (3 <= 4)
4 (48,40,83,??)
5 4
3 (4 <= 4)
4 (48,40,83,70)
5 5
3 (5 <= 4)
7 Output (48,40,83,70)
Algorithm 4.10
Given a set of lists, Name and Grade, determine who received the lowest grade.– Name: WORST
– Given: Name,G, N
• Change: None
– Result: LName
– Intermediate:
• Loc, Min, I
– Definition:
– Lname :=WORST(Name,G,N)
Method Get Name, G, N Let Min = 101 Let I = 1 Loop When (I <= N)
If (GI < Min)
Let Min = GI
Let Loc = I
Let I = I + 1 Finish Loop
Let LName = NameLoc
Give LName
Trace 4.8
Trace algorithm 4.10 with the grouped list defined previously
(1) Get Name, G, N(2) Let Min = 101(3) Let I = 1(4) Loop When (I <= N)(5) If (GI < Min)(6) Let Min = GI
(7) Let Loc = I
(8) Let I = I + 1(9) Finish Loop(10) Let LName = NameLoc
(11) Give LName
Name = (Ann, Bob, Cathy, Dave)G = (48,40,83,70)
LN N I LOC MIN Lname TEST1,2,3 4 1 101 4 (1 <= 4) 5 (48<101) 6 48 7 1 8 2
4 (2<=4) 5 (40<48) 6 40 7 2 8 3
4 (3<=4) 5 (83<40) 8 4
4 (4<=4) 5 (70<40) 8 5 4 (5<=4)
10 Bob 11 Output Bob
Additional Material
Flow Charts
Algorithm 4.1NAME: FindK
GIVENS: L, N, K
Change: None
RESULTS: Val
INTERMEDIATES: None
DEFINITION:
Val := FindK(L, N, K)
StartFINDK
Get L,NGet K
Let Val = LK
Give Val
FinishFINDK
Algorithm 4.2
NAME: ChangeK
GIVENS: L, N, K, M
Change: L
RESULTS: None
INTERMEDIATES: None
DEFINITION:
ChangeK(L, N, K, M)
StartCHANGEK
Get L,NGet KGet M
Let LK =M
Give L
FinishCHANGEK
Algorithm 4.3NAME: Middle
GIVENS: L, N
Change: None
RESULTS: Mid
INTERMEDIATES: Loc
DEFINITION:
Mid := Middle(L, N)
StartMIDDLE
Get L,N
Let Loc = N div 2 + 1Let Mid = LLoc
Give Mid
FinishMIDDLE
Algorithm 4.4Name:SUMLIST
Given: X, N
Change: None
Result: Total
Intermediate: I
Definition
Total := SUMLIST(X,N)
StartSUMLIST
Get X, N
Let Total = 0Let I = 1
(I <= N)
Let Total = Total + XI
Let I = I + 1
Give Total
FinishSUMLIST
Y
N
Algorithm 4.5
Name: SEARCHX
Given: X, N, V
Change: None
Result: Found
Intermediate: I
Definition:
Found := SEARCHX(X,N,V)
StartSEARCHX
Get X, NGet V
Let Found = FalseLet I = 1
If(XI = V)
Let Found = True Let I = I + 1
(I > N) or(Found)
Give Found
FinishSEARCHX
N
Y
N
Y
Algorithm 4.6
Name: MAXLIST
Given: X, N
Change:None
Result: Max, Loc
Intermediate: I
Definition:
(Loc,Max) := MAXLIST(X,N)
StartMAXLIST
Get X,N
Let Max = -1Let I = 1
(I <= N)
Let Max = XI Let Loc = I
Give MaxGive Loc
FinishMAXLIST
Y
N
If (XI > Max)
Let I = I + 1
Y
N
Algorithm 4.7
Name: NUM0
Given: X, N
Change: None
Result: Count
Intermediate: I
Definition:
Count := NUM0(X,N)
StartNUM0
Get X,N
Let Count = 0Let I = 1
(I <= N)
Let Count = Count + 1
Give Count
FinishNUM0
Y
N
If (XI = 0)
Let I = I + 1
Y
N
Algorithm 4.8
Name: DUPLICATE
Given: X, N
Change: None
Result: Double
Intermediate: I, Test
Definition:
Double := DUPLICATE(X,N)
StartDUPLICATE
Get X,N
Let Double = FalseLet Test = 1
Give Double
FinishDUPLICATE
Let Double = True Let I = I + 1
(I > N) or (Double)
If (XTest = XI )
Let I = Test + 1
If (Not (Double))
Let Test = Test + 1
(Test = N) or (Double)
N
Y
N
Y
Y
N
Y
N
Algorithm 4.9
Name: FINDGRADE
Given: M, F, N
Change: None
Result: G
Intermediate: I
Definition:
G := FINDGRADE(M,F,N)
StartFINDGRADE
Get M,F,N
Let I = 1
(I <= N)
Let GI = 75%FI + 25%MI
Let I = I + 1
Give G
FinishFINDGRADE
Y
N
Algorithm 4.10
Name: WORST
Given: Name,G, N
Change: None
Result: LName
Intermediate:
Loc, Min, I
Definition:
Lname :=WORST(Name,G,N)
StartWORST
Get Name, G, N
Let Min = 101Let I = 1
(I <= N)
Let Min = GI
Let Loc = I
Give LName
FinishWORST
Y
N
If (GI < Min)
Let I = I + 1
Y
N
Let LName = NameLoc
NSD
Algorithm 4.1NAME: FindK
GIVENS: L, N, K
Change: None
RESULTS: Val
INTERMEDIATES: None
DEFINITION:
Val := FindK(L, N, K)
Get L,NGet KLet Val = LK
Give Val
Algorithm 4.2
NAME: ChangeK
GIVENS: L, N, K, M
Change: L
RESULTS: None
INTERMEDIATES: None
DEFINITION:
ChangeK(L, N, K, M)
Get L, NGet K
Get M
Let LK = M
Give L
Algorithm 4.3NAME: Middle
GIVENS: L, N
Change: None
RESULTS: Mid
INTERMEDIATES: Loc
DEFINITION:
Mid := Middle(L, N)
Get L, NLet Loc = N div 2 + 1Let Mid = LLoc
Give Mid
Algorithm 4.4Name:SUMLIST
Given: X, N
Change: None
Result: Total
Intermediate: I
Definition
Total := SUMLIST(X,N)
While (I <=N)
Let Total = Total + XI
Let I = I + 1
Get X, NLet Total = 0
Let I = 1
Give Total
Algorithm 4.5
Name: SEARCHX
Given: X, N, V
Change: None
Result: Found
Intermediate: I
Definition:
Found := SEARCHX(X,N,V)
Y N
Let Found = True Let I = I + 1
Let I = 1
Let Found = False
Get VGet X, N
Give FoundUntil (I > N) or (Found)
If (XI = V)
Algorithm 4.6
Name: MAXLIST
Given: X, N
Change:None
Result: Max, Loc
Intermediate: I
Definition:
(Loc,Max) := MAXLIST(X,N)
Y NLet Max = XI
Let Loc = I
While (I <= N)
Give MaxGive Loc
Get X, NLet Max = -1
Let I = 1
If (XI > Max)
Do Nothing
Let I = I + 1
Algorithm 4.7
Name: NUM0
Given: X, N
Change: None
Result: Count
Intermediate: I
Definition:
Count := NUM0(X,N)
Y N
Let Count = Count + 1 Do Nothing
Give CountLet I = I + 1
If (XI = 0)
While (I <= N)
Let Count = 0Get X, N
Let I = 1
Algorithm 4.8
Name: DUPLICATE
Given: X, N
Change: None
Result: Double
Intermediate: I, Test
Definition:
Double := DUPLICATE(X,N)Y N
Let Double = True Let I = I + 1
NDo Nothing
Let I = Test + 1
Give Double
If (XTest = XI)
Until (I > N) or (Double)If (Not(Double))
YLet Test = Test + 1Until (Test = N) or (Double)
Get X, NLet Double = False
Let Test = 1
Algorithm 4.9
Name: FINDGRADE
Given: M, F, N
Change: None
Result: G
Intermediate: I
Definition:
G := FINDGRADE(M,F,N)
While (I <= N)
Let GI = 75%FI + 25% MI
Let I = I + 1
Get M, F, NLet I = 1
Give G
Algorithm 4.10
Name: WORST
Given: Name,G, N
Change: None
Result: LName
Intermediate:
Loc, Min, I
Definition:
Lname :=WORST(Name,G,N)
Y NLet Min = GI
Let Loc = I
Get Name, G, NLet Min = 101
Let I = 1
If (GI < Min)
While (I <= N)
Let LName = NameLoc
Give Lname
Do Nothing
Let I = I + 1
Homework
Write an algorithm to find the maximum value in a list of positive numbers. Each entry in the list is unique (ie. There are no duplicate numbers)
Write an algorithm that fills a list with the first 50 multiples of 7. (ie. 7, 14, 21…350)
Write an algorithm to populate a list with names. The list will continue to grow until the user indicates it is time to stop.
Rewrite Algorithm 4.6, such that the variable Max is not used at all:
– Name: MAXLIST
– Given: X, N
• Change:None
– Result: Loc
– Intermediate: I
– Definition:
– Loc := MAXLIST(X,N)