Title: CS1001%20Lecture%2020
1CS1001 Lecture 20
- Quizzes 3 and 4 review
- Application of Arrays
- Stacks (continuation)
- Sorting
2Quiz 3
Write a function that, given an integer N,
calculate N! N! is N ( N - 1 ) ( N - 2 )
... 1 INTEGER FUNCTION Factorial(N) INTEGER
INTENT(IN) N INTEGER product product 1 DO
I 2 , N product product I END DO factorial
product END FUNCTION Factorial C M!/(R!
(M-R)!) gt in Fortran C Factorial(M) / (
Factorial(R)Factorial(M-R) )
product 1 loop I product 2 2 3 6 4 24 5 120
etc.
3QUIZ 4, 1
1.) What will be displayed when the following
code segment executes? INTEGER i, j INTEGER,
DIMENSION (10) Table DO i 1, 10 Table(i)
i1 END DO DO i 1, 4 j i 2 1 print
, i, j, Table(j) END DO
First DO loop i Table(i) 1 2 2 3 3 4 4 5 5 6 6 7
7 8 8 9 9 10 10 11
Second DO loop i j Table(j) 1 3 4 2 5 6 3 7 8 4 9
10
4Quiz 4, 2
Assume that Nums is an array of 100 integers, and
that a program has read in values for all 100
array elements. Write a fragment of Fortran code
that will calculate and display the number of
elements in the array Nums that contain the value
-1.
Count 0 DO I 1,100 IF (Nums (I) .EQ. 1)
Count Count 1 END DO PRINT , Count
5Stack
- A data structure used to implement
Last-in-first-out (LIFO) processes - e.g. stack of trays in the cafeteria
- Two operations
- Push
- Pop
- Some design issues
- How long (or deep) should the stack be (i.e.,
the dimension of the stack)? - When push, is there room in array?
- When pop, is the stack empty ?
Top
Stack(top)
Stack(4)
Stack(3)
Stack(2)
Stack(1)
Stack
6More practice with Arrays
- Sorting - arranging the elements in a list so
that they are in ascending or descending order - Consider simple selection sort e.g., sort a list
of integers in ascending - list represented in an one-dimensional array, say
n integer numbers x(1), x(2), ...,x(n). Modify
this list so the numbers are in ascending order - find the smallest number and put it in the first
position - find the second smallest number and put it in the
second position - etc.
- after n-1 times through, the list is in ascending
order
7Selection Sort
First time x(1) 67 x(2) 33 x(3) 21 x(4) 84 x(5) 4
9 x(6) 50 x(7) 75
Third time x(1) 21 x(2) 33 x(3) 67 x(4) 84 x(5) 4
9 x(6) 50 x(7) 75
Starting x(1) 67 x(2) 33 x(3) 21 x(4) 84 x(5) 49
x(6) 50 x(7) 75
Second time x(1) 21 x(2) 33 x(3) 67 x(4) 84 x(5)
49 x(6) 50 x(7) 75
Fourth time x(1) 21 x(2) 33 x(3) 49 x(4) 84 x(5)
67 x(6) 50 x(7) 75
Fifth time x(1) 21 x(2) 33 x(3) 49 x(4) 50 x(5) 6
7 x(6) 84 x(7) 75
Sixth time x(1) 21 x(2) 33 x(3) 49 x(4) 50 x(5) 6
7 x(6) 84 x(7) 75
Finish x(1) 21 x(2) 33 x(3) 49 x(4) 50 x(5) 67 x(
6) 75 x(7) 84
8SUBROUTINE SelectionSort(Item) INTEGER,
DIMENSION(), INTENT(INOUT) Item INTEGER
NumItems, SmallestItem, I INTEGER, DIMENSION(1)
MINLOC_array NumItems SIZE(Item) DO I
1, NumItems - 1 ! Find smallest item in the
sublist ! Item(I), ..., Item(NumItems)
SmallestItem MINVAL(Item(INumItems))
MINLOC_array MINLOC(Item(INumItems))
LocationSmallest (I - 1) MINLOC_array(1)
! Interchange smallest item with Item(I)
at ! beginning of sublist
Item(LocationSmallest) Item(I) Item(I)
SmallestItem END DO END SUBROUTINE
SelectionSort
9Bubble Sort
First time 67 33 21 84 49 50 75
33 21 67 84 49 50 75
Starting x(1) 67 x(2) 33 x(3) 21 x(4) 84 x(5) 49
x(6) 50 x(7) 75
33 67 21 84 49 50 75
33 21 67 84 49 50 75
33 21 67 49 84 50 75
33 21 67 49 50 84 75
33 21 67 49 50 75 84
21 33 49 50 67 75 84
Second time
33 21 67 49 50 75 84
21 33 67 49 50 75 84
21 33 67 49 50 75 84
21 33 49 67 50 75 84
21 33 49 50 67 75 84
Finish x(1) 21 x(2) 33 x(3) 49 x(4) 50 x(5) 67 x(
6) 75 x(7) 84
10SUBROUTINE BubbleSort(Item) INTEGER,
DIMENSION(), INTENT(INOUT) Item INTEGER
NumPairs, LastSwap, I, Temp NumPairs
SIZE(Item) - 1 DO IF (NumPairs 0)
EXIT ! If no more pairs to check, terminate
repetition ! Otherwise scan the sublist of the
first NumPairs pairs ! in the list,
interchanging items that are out of order
LastSwap 1 DO I 1, NumPairs IF
(Item(I) gt Item(I1)) THEN ! Items out
of order -- interchange them Temp
Item(I) Item(I) Item(I1)
Item(I1) Temp ! Record position of
last swap LastSwap I END IF
END DO NumPairs LastSwap - 1 END
DO END SUBROUTINE BubbleSort