s erie d exercices 1
play

S erie dexercices #1 IFT2030 17 janvier 2006 1 1 Simula % Jean - PDF document

S erie dexercices #1 IFT2030 17 janvier 2006 1 1 Simula % Jean Vaucher % http://www.iro.umontreal.ca/~vaucher/Simula/Software/containers.sim ref(Container) procedure SORT; begin Boolean procedure precede(E1,E2); ref(element) E1, E2;


  1. S´ erie d’exercices #1 IFT2030 17 janvier 2006 1

  2. 1 Simula % Jean Vaucher % http://www.iro.umontreal.ca/~vaucher/Simula/Software/containers.sim ref(Container) procedure SORT; begin Boolean procedure precede(E1,E2); ref(element) E1, E2; precede := E1.key < E2.key; if numElements > 1 then Quicksort(1, numElements, precede); Sort :- Owner; end; ref(Container) procedure ORDER(precede); Boolean procedureprecede; begin if numElements > 1 then quicksort(1,numElements, precede ); Order :- Owner; end; procedure Quicksort(L,R,precede); integer L,R; Boolean procedure precede; begin integer i,j; ref(element) x, Mid; Mid :- A ( (L+R) // 2 ); j := R; i := L; while i < j do begin while precede( a(i), Mid ) do i := i+1; while precede( Mid , a(j)) do j := j-1; if i<=j then begin x :- A(i); A(i) :- A(j); a(j) :- x; i := i+1; j := j-1; end ; end; if L < j then QuickSort(L,j,precede); if i < R then QuickSort(i,R,precede); end; 2

  3. 2 Fortran C http://www.fortran.com/fortran/quick_sort1.f C C From HDK@psuvm.psu.edu Thu Dec 8 15:27:16 MST 1994 C C The following was converted from Algol recursive to Fortran iterative C by a colleague at Penn State (a long time ago - Fortran 66, please C excuse the GoTo’s). The following code also corrects a bug in the C Quicksort algorithm published in the ACM (see Algorithm 402, CACM, C Sept. 1970, pp 563-567; also you younger folks who weren’t born at C that time might find interesting the history of the Quicksort C algorithm beginning with the original published in CACM, July 1961, C pp 321-322, Algorithm 64). Note that the following algorithm sorts C integer data; actual data is not moved but sort is affected by sorting C a companion index array (see leading comments). The data type being C sorted can be changed by changing one line; see comments after C declarations and subsequent one regarding comparisons(Fortran C 77 takes care of character comparisons of course, so that comment C is merely historical from the days when we had to write character C compare subprograms, usually in assembler language for a specific C mainframe platform at that time). But the following algorithm is C good, still one of the best available. SUBROUTINE QSORTI (ORD,N,A) C C==============SORTS THE ARRAY A(I),I=1,2,...,N BY PUTTING THE C ASCENDING ORDER VECTOR IN ORD. THAT IS ASCENDING ORDERED A C IS A(ORD(I)),I=1,2,...,N; DESCENDING ORDER A IS A(ORD(N-I+1)), C I=1,2,...,N . THIS SORT RUNS IN TIME PROPORTIONAL TO N LOG N . C C C ACM QUICKSORT - ALGORITHM #402 - IMPLEMENTED IN FORTRAN 66 BY C WILLIAM H. VERITY, WHV@PSUVM.PSU.EDU C CENTER FOR ACADEMIC COMPUTING C THE PENNSYLVANIA STATE UNIVERSITY C UNIVERSITY PARK, PA. 16802 C IMPLICIT INTEGER (A-Z) C DIMENSION ORD(N),POPLST(2,20) INTEGER X,XX,Z,ZZ,Y C C TO SORT DIFFERENT INPUT TYPES, CHANGE THE FOLLOWING C SPECIFICATION STATEMENTS; FOR EXAMPLE, FOR FORTRAN CHARACTER C USE THE FOLLOWING: CHARACTER *(*) A(N) C INTEGER A(N) C 3

  4. NDEEP=0 U1=N L1=1 DO 1 I=1,N 1 ORD(I)=I 2 IF (U1.LE.L1) RETURN C 3 L=L1 U=U1 C C PART C 4 P=L Q=U C FOR CHARACTER SORTS, THE FOLLOWING 3 STATEMENTS WOULD BECOME C X = ORD(P) C Z = ORD(Q) C IF (A(X) .LE. A(Z)) GO TO 2 C C WHERE "CLE" IS A LOGICAL FUNCTION WHICH RETURNS "TRUE" IF THE C FIRST ARGUMENT IS LESS THAN OR EQUAL TO THE SECOND, BASED ON "LEN" C CHARACTERS. C X=A(ORD(P)) Z=A(ORD(Q)) IF (X.LE.Z) GO TO 5 Y=X X=Z Z=Y YP=ORD(P) ORD(P)=ORD(Q) ORD(Q)=YP 5 IF (U-L.LE.1) GO TO 15 XX=X IX=P ZZ=Z IZ=Q C C LEFT C 6 P=P+1 IF (P.GE.Q) GO TO 7 X=A(ORD(P)) IF (X.GE.XX) GO TO 8 GO TO 6 7 P=Q-1 GO TO 13 C C RIGHT C 4

  5. 8 Q=Q-1 IF (Q.LE.P) GO TO 9 Z=A(ORD(Q)) IF (Z.LE.ZZ) GO TO 10 GO TO 8 9 Q=P P=P-1 Z=X X=A(ORD(P)) C C DIST C 10 IF (X.LE.Z) GO TO 11 Y=X X=Z Z=Y IP=ORD(P) ORD(P)=ORD(Q) ORD(Q)=IP 11 IF (X.LE.XX) GO TO 12 XX=X IX=P 12 IF (Z.GE.ZZ) GO TO 6 ZZ=Z IZ=Q GO TO 6 C C OUT C 13 CONTINUE IF (.NOT.(P.NE.IX.AND.X.NE.XX)) GO TO 14 IP=ORD(P) ORD(P)=ORD(IX) ORD(IX)=IP 14 CONTINUE IF (.NOT.(Q.NE.IZ.AND.Z.NE.ZZ)) GO TO 15 IQ=ORD(Q) ORD(Q)=ORD(IZ) ORD(IZ)=IQ 15 CONTINUE IF (U-Q.LE.P-L) GO TO 16 L1=L U1=P-1 L=Q+1 GO TO 17 16 U1=U L1=Q+1 U=P-1 17 CONTINUE IF (U1.LE.L1) GO TO 18 5

  6. C C START RECURSIVE CALL C NDEEP=NDEEP+1 POPLST(1,NDEEP)=U POPLST(2,NDEEP)=L GO TO 3 18 IF (U.GT.L) GO TO 4 C C POP BACK UP IN THE RECURSION LIST C IF (NDEEP.EQ.0) GO TO 2 U=POPLST(1,NDEEP) L=POPLST(2,NDEEP) NDEEP=NDEEP-1 GO TO 18 C C END SORT C END QSORT C END 6

  7. 3 Forth \ http://www.tinyboot.com/quiksort.txt anew wilsort \ ---------------------------------------------------------- \ Wil Baden’s sorter \ Set PRECEDES for different datatypes or sort order. DEFER PRECEDES ’ < IS PRECEDES \ For sorting character strings in increasing order: : SPRECEDES ( addr addr -- flag ) >R COUNT R> COUNT COMPARE 0< ; : IPRECEDES ( addr addr -- flag ) < ; ’ SPRECEDES IS PRECEDES : EXCHANGE ( addr_1 addr_2 -- ) DUP @ >R OVER @ SWAP ! R> SWAP ! ; : -CELL ( -- n ) -1 CELLS ; : CELL- ( addr -- addr’ ) 1 CELLS - ; : PARTITION ( lo hi -- lo_1 hi_1 lo_2 hi_2 ) 2DUP OVER - 2/ -CELL AND + @ >R ( R: median) 2DUP BEGIN ( lo_1 hi_2 lo_2 hi_1) SWAP BEGIN DUP @ R@ PRECEDES WHILE CELL+ REPEAT SWAP BEGIN R@ OVER @ PRECEDES WHILE CELL- REPEAT 2DUP > NOT IF 2DUP EXCHANGE >R CELL+ R> CELL- THEN 2DUP > UNTIL ( lo_1 hi_2 lo_2 hi_1) R>DROP SWAP ROT ( lo_1 hi_1 lo_2 hi_2) ; : QSORT ( lo hi -- ) PARTITION ( lo_1 hi_1 lo_2 hi_2) 2OVER 2OVER - + ( . . . . lo_1 hi_1+lo_2-hi_2) < IF 2SWAP THEN ( lo_1 hi_1 lo_2 hi_2) 2DUP < IF RECURSE ELSE 2DROP THEN 2DUP < IF RECURSE ELSE 2DROP THEN ; : SORT ( addr n -- ) DUP 2 < IF 2DROP EXIT THEN 1- CELLS OVER + ( addr addr+{n-1}cells) QSORT ( ) ; \ ---------------------------------------------------------- 7

  8. 4 Algol 68 CO Lifted from the Algol 68 Genie documentation at http://www.xs4all.nl/~jmvdveer/algol.html CO BEGIN MODE NODE = STRUCT (INT k, TREE smaller, larger), TREE = REF NODE; TREE empty tree = NIL; PROC add = (REF TREE root, INT k) VOID: IF root IS empty tree THEN root := HEAP NODE := (k, NIL, NIL) ELSE IF k < k OF root THEN add (smaller OF root, k) ELSE add (larger OF root, k) FI FI; PROC write = (TREE root) VOID: IF root ISNT NIL THEN write (smaller OF root); print ((whole (k OF root, 0), " ")); write (larger OF root) FI; TREE root := empty tree; WHILE INT n = read int; n > 0 DO add (root, n) OD; write (root) END CO $Id: sort.a68,v 1.1 2006/01/17 02:55:31 haguenad Exp $ CO 8

  9. 5 Java /* Code by Manuel Hermenegildo, http://clip.dia.fi.upm.es/Projects/ParForce/Final_review/slides/intro/node4.html */ static void sort(int a[], int lo0, int hi0) { int lo = lo0; int hi = hi0; if (lo >= hi) { return; } int mid = a[(lo + hi) / 2]; while (lo < hi) { while (lo<hi && a[lo] < mid) { lo++; } while (lo<hi && a[hi] >= mid) { hi--; } if (lo < hi) { int T = a[lo]; a[lo] = a[hi]; a[hi] = T; } } if (hi < lo) { int T = hi; hi = lo; lo = T; } sort(a, lo0, lo); sort(a, lo == lo0 ? lo+1 : lo, hi0); } static void sort(int a[]) { sort(a, 0, a.length-1); } 9

  10. 6 Haskell sort :: [Int] -> [Int] sort [] = [] sort (h:t) = sort [x | x <- t, x < h] ++ [h] ++ sort [x | x <- t, x > h] 10

  11. 7 C Expliquer en quoi la norme ANSI C laisse ind´ efinie la s´ emantique des parties de programmes suivantes : 1. x % y 2. short x; x = 20000; x = x + x; 3. int x = 0; f(x, x, x, x = 1); 4. f(1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2) $Id: demo01.tex,v 1.2 2006/01/17 16:25:07 haguenad Exp $ 11

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend