CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING
Lists Sometimes 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: X 1, X 2, X 3,………., X N
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, N Get K Let Val = L K 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, N Get K Get M Let L K = 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 L K = M (5) Give L Ln L N K M 1 (1,8,3,6,2) (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 Let Mid = L loc 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 (3) Let Mid = L loc (4) Give Mid LN L N Loc Mid 1 (1,8,3,6,2) 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 = FALSE X or True and not Y or Z = TRUE not(X and not Z or False and (True and Y)) = TRUE Y 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 = X 1 + X 2 + …….. + X N –Total = Total + X I 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 + X I 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 + X I (6) Let I = I + 1 (7) Finish Loop (8) Give Total LN X N I Total Test 1 (3, 7, 5) (1 <= 3) (2 <= 3) (3 <= 3) (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 X I = V? –Stop the index at this point »FOUND at X I –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 (X I = 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 (X I = 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) False (3 = 7) (2>3) or (F) 6 (7 = 7) 7 True 10 (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 (X I > 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 (X I > Max) Let Max = X I Let Loc = I Let I = I + 1 Finish Loop Give Max Give 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 (X I > Max) (6) Let Max = X I (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) (1 <= 4) 5 (8 > -1) (2 <= 4) 5 (3 > 8) (3 <= 4) 5 (25>8) (4 <= 4) 5 (9>25) (5 <= 4) 10 Output 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 X I = 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 (X I = 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 X I with every other element of the list Loop within a Loop –For each X I »Search for X I 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 (X Test = X I ) 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 (X Test = X I ) (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 TEST 1,2,3 (2,8,7,8) 4 False (2=8) (3>4)or(F) 7 (2=7) (4>4)or(F) 7 (2=8) (5>4)or(F) 12 Not (F) (2=4)or(F) (8=7) (4>4)or(F) 7 (8=8) 8 True 11 (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 –Bob –Cathy –Dave
Grouped Lists We have three lists –Name = (Ann, Bob, Cathy, Dave) –Midterm = (100, 75, 60, 40) –Final = (30, 28, 90, 80) Since Ann is Name 1, then her midterm mark would be Midterm 1 and her final mark would be Final 1 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 G I = 0.75*F I *M I 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 G I = 0.75*F I *M I (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,??,??,??) (2 <= 4) 4 (48,40,??,??) (3 <= 4) 4 (48,40,83,??) (4 <= 4) 4 (48,40,83,70) (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 (G I < Min) Let Min = G I Let Loc = I Let I = I + 1 Finish Loop Let LName = Name Loc 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 (G I < Min) (6) Let Min = G I (7) Let Loc = I (8) Let I = I + 1 (9) Finish Loop (10) Let LName = Name Loc (11) Give LName Name = (Ann, Bob, Cathy, Dave) G = (48,40,83,70) LN N I LOC MIN Lname TEST 1,2, (1 <= 4) 5 (48<101) (2<=4) 5 (40<48) (3<=4) 5 (83<40) (4<=4) 5 (70<40) (5<=4) 10 Bob 11 Output Bob
Additional Material
Flow Charts
Algorithm 4.1 NAME: FindK GIVENS: L, N, K Change: None RESULTS: Val INTERMEDIATES: None DEFINITION: Val := FindK(L, N, K)
Algorithm 4.2 NAME: ChangeK GIVENS: L, N, K, M Change: L RESULTS: None INTERMEDIATES: None DEFINITION: ChangeK(L, N, K, M)
Algorithm 4.3 NAME: Middle GIVENS: L, N Change: None RESULTS: Mid INTERMEDIATES: Loc DEFINITION: Mid := Middle(L, N)
Algorithm 4.4 Name:SUMLIST Given: X, N Change: None Result: Total Intermediate: I Definition Total := SUMLIST(X,N)
Algorithm 4.5 Name: SEARCHX Given: X, N, V Change: None Result: Found Intermediate: I Definition: Found := SEARCHX(X,N,V)
Algorithm 4.6 Name: MAXLIST Given: X, N Change:None Result: Max, Loc Intermediate: I Definition: (Loc,Max) := MAXLIST(X,N)
Algorithm 4.7 Name: NUM0 Given: X, N Change: None Result: Count Intermediate: I Definition: Count := NUM0(X,N)
Algorithm 4.8 Name: DUPLICATE Given: X, N Change: None Result: Double Intermediate: I, Test Definition: Double := DUPLICATE(X,N)
Algorithm 4.9 Name: FINDGRADE Given: M, F, N Change: None Result: G Intermediate: I Definition: G := FINDGRADE(M,F,N)
Algorithm 4.10 Name: WORST Given: Name,G, N Change: None Result: LName Intermediate: Loc, Min, I Definition: Lname :=WORST(Name,G,N)
NSD
Algorithm 4.1 NAME: FindK GIVENS: L, N, K Change: None RESULTS: Val INTERMEDIATES: None DEFINITION: Val := FindK(L, N, K)
Algorithm 4.2 NAME: ChangeK GIVENS: L, N, K, M Change: L RESULTS: None INTERMEDIATES: None DEFINITION: ChangeK(L, N, K, M)
Algorithm 4.3 NAME: Middle GIVENS: L, N Change: None RESULTS: Mid INTERMEDIATES: Loc DEFINITION: Mid := Middle(L, N)
Algorithm 4.4 Name:SUMLIST Given: X, N Change: None Result: Total Intermediate: I Definition Total := SUMLIST(X,N)
Algorithm 4.5 Name: SEARCHX Given: X, N, V Change: None Result: Found Intermediate: I Definition: Found := SEARCHX(X,N,V)
Algorithm 4.6 Name: MAXLIST Given: X, N Change:None Result: Max, Loc Intermediate: I Definition: (Loc,Max) := MAXLIST(X,N)
Algorithm 4.7 Name: NUM0 Given: X, N Change: None Result: Count Intermediate: I Definition: Count := NUM0(X,N)
Algorithm 4.8 Name: DUPLICATE Given: X, N Change: None Result: Double Intermediate: I, Test Definition: Double := DUPLICATE(X,N)
Algorithm 4.9 Name: FINDGRADE Given: M, F, N Change: None Result: G Intermediate: I Definition: G := FINDGRADE(M,F,N)
Algorithm 4.10 Name: WORST Given: Name,G, N Change: None Result: LName Intermediate: Loc, Min, I Definition: Lname :=WORST(Name,G,N)
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)