Presentation is loading. Please wait.

Presentation is loading. Please wait.

18 April, 2000 CS1001 Lecture 20 Quizzes 3 and 4 review Application of Arrays –Stacks (continuation) –Sorting.

Similar presentations


Presentation on theme: "18 April, 2000 CS1001 Lecture 20 Quizzes 3 and 4 review Application of Arrays –Stacks (continuation) –Sorting."— Presentation transcript:

1 18 April, 2000 CS1001 Lecture 20 Quizzes 3 and 4 review Application of Arrays –Stacks (continuation) –Sorting

2 18 April, 2000 Quiz 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)!) ==> in Fortran: C = Factorial(M) / ( Factorial(R)*Factorial(M-R) ) product = 1 loop:Iproduct2 36 424 5120 etc.

3 18 April, 2000 QUIZ 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) = i+1 END DO DO i = 1, 4 j = i * 2 + 1 print *, i, j, Table(j) END DO First DO loop: iTable(i) 12 23 34 45 56 67 78 89 910 1011 Second DO loop: ijTable(j) 134 256 378 4910

4 18 April, 2000 Quiz 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

5 18 April, 2000 Stack A data structure used to implement Last-in-first- out (LIFO) processes –e.g.: stack of trays in the cafeteria Top : Stack Stack(1) Stack(top) Stack(4) Stack(3) Stack(2) : 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 ?

6 18 April, 2000 More 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

7 18 April, 2000 Selection Sort Starting: x(1)67 x(2)33 x(3)21 x(4)84 x(5)49 x(6)50 x(7)75 First time: 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 Third 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)67 x(6)84 x(7)75 Sixth time: x(1)21 x(2)33 x(3)49 x(4)50 x(5)67 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

8 18 April, 2000 SUBROUTINE 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(I:NumItems)) MINLOC_array = MINLOC(Item(I:NumItems)) 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

9 18 April, 2000 Bubble Sort Starting: x(1)67 x(2)33 x(3)21 x(4)84 x(5)49 x(6)50 x(7)75 First time: 67 33 21 84 49 50 75 33 67 21 84 49 50 75 33 21 67 84 49 50 75 Second time: Finish: x(1)21 x(2)33 x(3)49 x(4)50 x(5)67 x(6)75 x(7)84 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 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 21 33 49 50 67 75 84

10 18 April, 2000 SUBROUTINE 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) > Item(I+1)) THEN ! Items out of order -- interchange them Temp = Item(I) Item(I) = Item(I+1) Item(I+1) = Temp ! Record position of last swap LastSwap = I END IF END DO NumPairs = LastSwap - 1 END DO END SUBROUTINE BubbleSort


Download ppt "18 April, 2000 CS1001 Lecture 20 Quizzes 3 and 4 review Application of Arrays –Stacks (continuation) –Sorting."

Similar presentations


Ads by Google