Présentation
• P.C. Jean-Eric Pin, Directeur de recherches en informatique au CNRS (LIAFA)
• T.D.
Laurent Viennot
(INRIA)Louis Granboulan
(ENS)• Courrier électronique
Jean-Eric.Pin@liafa.jussieu.fr
Louis.Granboulan@ens.fr Laurent.Viennot@inria.fr
• Web
http://liafa.jussieu.fr/~jep
http://www.dmi.ens.fr/~granboul/
http://menetou.inria.fr/~viennot
• Recherche
J.-E. Pin : Automates finis.
1 | 1 1 | 0
1 2
0 | 1 0 | 0
0 | 0 0 1 | 1
La multiplication par 3 pour les nombres binaires écrits à l'envers :
11 x 3 = 33
110100 --> 100001
Automates
Plan des P.C.
Algorithmes
• Tris
• Hachage
• Arbres
• Graphes
• Algorithmes complémentaires (polynômes, compression, etc.) Programmation
• Récursivité
• Java
Plan
• Structure d'un programme
• Commentaires
• Mots-clés
• Identificateurs
• Types de base
• Règles de priorité
• Déclarations de constantes, de types et de variables
• Expressions et instructions
• Entrées et sorties
• Tris
• Conseils de la semaine
Structure d'un programme Java
Un programme Java est constitué d'un ou plusieurs fichiers dont le nom est terminé par .java :
tri.java arbre.java
Structure générale :
• Bibliothèques utilisées
• Classes
• Données (attributs)
• Méthodes
Fichier CarreMagique.java
• Bibliothèques
import java.io.*;
import java.lang.*;
import java.util.*;
• Classes
public class CarreMagique {// Attributs
final static int N = 100;
static int a[][] = new int[N][N];
static int n;
// Méthodes
static void Init(int n) { . . . }
static void Magique(int n) { . . . }
}
Méthode main
public static void main(String args[]) {
n = Lire();
Init(n);
Magique(n);
Imprimer(n);
}
C'est la méthode automatiquement appelée par Java. String args[] est le paramètre obligatoire de la méthode main
Commentaires
/* Un commentaire sur plusieurs
lignes */
// Une seule ligne // de commentaires
/*--- * Liste.java Jean-Eric Pin
*--- */
Commentaires spéciaux
/**
* @see Table.java
* @author Jean-Eric Pin * @version 1.05b
* @param Nom
* @return Numéro de tél.
* @exception Pas trouvé
* @deprecated La méthode M est
* obsolète.
*/
Les commentaires commençant par /** sont utilisés pour créer une
documentation automatique en html (le langage du Web)
Mots-clés
abstract boolean break
byte case catch
char class const
continue default do
double else extends
false final finally
float for goto
if implements import
instanceof int interface
long native new
package private protected
public return short
static super switch
synchronized this throw throws transient true
try void volatile
while
false null true
Mots-réservés
10011101 (binaire) -->
9D (hexadécimal) -->
157 (décimal) Un octet = 8 bits
Adressage 16 bits, 32 bits, 64 bits Table ASCII (8 bits)
Unicode (16 bits) : en principe, les caractères de tous les alphabets devraient être acceptés. On code par
\uhhhh
où chaque h est un chiffre hexadécimal.
Exemple : \u2122 correspond à ™
Système binaire
Identificateurs
Suite de lettres et de chiffres. Le
premier caractère doit être une lettre.
• Corrects:
EstValide est_valide Matrice3x3 Code5417
FaDiese Do
• Correct mais risqué:
çà $n
• Incorrects:
N! Fa#
Deux-Deux
Deux+Deux
2Fois do (réservé)
• Java distingue majuscules et minuscules
Les types primitifs
• Le type booléen
boolean true ou false (1 bit)
• Le type caractère
char Unicode codé sur 2 octets
• Les types entier
byte 1 octet [-128, 127]
short 2 octets [-32768, 32767]
int 4 octets
[-2147483648, 2147483647]
long 8 octets [-9.1018 , 9.1018]
• Les types réel
float simple précision (4 octets) double double précision (8 octets)
Opérateurs arithmétiques - x, +x : signe
+, -, * : addition, soustraction, multiplication
Attention ! Pour les entiers, calculé modulo la portée du type. Pour le type byte, 100 + 100 == 72 ...
x / y : division. Division entière pour les entiers, -7/2 == -3
x % y : "modulo", reste de la
division entière de x par y (x et y entiers positifs). Règle :
(x/y) * y + x%y == x Donc -7 % 2 == -1
Opérateurs de comparaison == != <= < > =>
Attention au == ...
Opérateurs logiques
&& || !
ET OU NON
Par définition, une expression logique vaut 1 si elle est vraie et 0 sinon.
L'évaluation se fait de gauche à droite et cesse dès que le
résultat est connu
Opérateurs logiques
e = (i == 0) && ((j >= 0) || (k < j))
Si i = 1, on a e = false et les tests j >= 0 et k < j ne sont même pas effectués.
Attention à l'ordre !
Correct
if ((i >= 0) && (i < t.taille) &&
(t[i] != 0)) ...
Incorrect
if ((t[i] != 0) && (i >= 0) &&
(i < t.taille)) ...
Constantes
• Booléens true, false
• Entiers
29 décimal 035 octal (base 8) 0x1D, 0X1d héxadécimal
• Entiers longs: terminés par l ou L
100000000L
• Réels
180. double 180.f float
1.8e2 double 1.8e2f float .18e3 double .18e3f float
final static int N = 10;
Constantes (suite) Caractères : 'c' + spéciaux :
\n newline \t tabulation
\b backspace \r return
\f form feed \\ backslash
\' apostrophe \" guillemets
\ooo Nombre octal
\uhhhh Nombre hexadécimal
Chaînes de caractères :
"une chaîne\n"
Séquence d'instructions {
instruction1;
instruction2;
instruction3;
}
Exemple
if ((n % 2) != 0) n = 3 * n + 1 else
n = n / 2;
if ((n % 2) != 0) {
n = 2 * n + 1;
imprime(n);
} else
n = n / 2;
If else if (expression)
SuiteInstructions1;
else
SuiteInstructions2;
int max(int i, int j) {
if (i < j)
max = j;
else max = i;
}
Ambiguïté du "if then else"
Comment comprendre :
if b1 if b2 instruction1 else instruction2;
if b1 { if b2
instruction1;
}else
instruction2;
Version 2 corrigée :
if b1 if b2
instruction1;
elseinstruction2;
if b1 if b2
instruction1;
elseinstruction2;
Version 1 Version 2
If else if ...
if (expression)
SuiteInstructions1;
else if
SuiteInstructions2;
else if
SuiteInstructions3;
else if
SuiteInstructions4;
else
SuiteInstructions5;
Tris
Ranger dans l'ordre croissant (au sens large) une suite finie d'entiers
a[0], …, a[N-1]
import java.io.*;
import java.lang.*;
import java.util.*;
public class Tri {
static void Initialisation();
static void Echanger(int i, int j);
static void Impression();
static void TriSelection();
static void TriBulle();
static void TriInsertion();
static void TriShell();
public static void main(String args[]) }
final static int N = 10;
static int[] a = new int[N];
// Le tableau à trier
static void Initialisation() { for (int i = 0; i < N; ++i)
a[i] =(int)(Math.random() * 128);
}
static void Echanger(int i, int j) {
int c = a[i];
a[i] = a[j];
a[j] = c;
}
Tri.java
static void Impression() {
for (int i = 0; i < N; ++i)
System.out.print(" " + a[i] + " ");
System.out.println("");
}
public static void main(String args[]) {
Initialisation(); // On lit le tableau Impression(); // et on l'affiche TriSelection(); // On trie
Impression(); // On affiche le résultat TriBulle();
Impression();
TriInsertion();
TriShell();
Impression();
}
Tri.java
Tri par sélection
static void TriSelection() {
int i, j, min;
for (i = 0; i < N - 1; ++i) { min = i;
for (j = i+1; j < N; ++j) if (a[j] < a[min])
min = j;
Echanger(i, min);
} }
• On recherche le plus petit élément
t[m]
de la suite
t[0] ... t[N-1]et on l'échange avec
t[0]• On recherche le plus petit élément
t[m]
de la suite
t[1] ... t[N-1]et on
l'échange avec
t[1], etc
↑ i
18 10 25 9 3 11 13 23 8
↑ i
↓ min
3 3 10 25 9 18 11 13 23 8
↑ i
↓ min
3 3 8 25 9 18 11 13 23 10
↑ i
↓ min
3 3 8 9 10 18 11 13 23 25
↑ i
↓ min
↑ i
↓ min
3 3 8 9 10 11 18 13 23 25
↑ i
↓ min
3 3 8 9 10 11 13 18 23 25
↑ i
↓ min
↑ i
↓ min
3 3 8 9 10 11 13 18 23 25 3 3 8 9 25 18 11 13 23 10
3 3 8 9 10 11 13 18 23 25 3
Tri par sélection
Tri bulle
• On parcourt la suite
t[0] ... t[N-1]en échangeant les éléments consécutifs
t[j-1] > t[j]• On parcourt la suite
t[0] ... t[N-2]en échangeant les éléments consécutifs
t[j-1] > t[j], etc.
static void TriBulle() {
for (int i = N-1; i >= 0; --i) for (int j = 1; j <= i; ++j)
if (a[j-1] > a[j])
Echanger(j-1, j);
}
10 18 9 3 11 13 23 8 25
↑ i
3 10 9 3 11 13 18 8 23 25
↑ i
3 9 3 10 11 13 8 18 23 25
↑ i
3 3 9 10 8 11 13 18 23 25
↑ i
↑ i
3 3 9 8 10 11 13 18 23 25
↑ i
3 3 8 9 10 11 13 18 23 25
↑ i
↑ i
3 3 8 9 10 11 13 18 23 25 3 3 9 10 11 8 13 18 23 25
3 3 8 9 10 11 13 18 23 25 3
Tri bulle
Tri par insertion
static void TriInsertion() { int i, j, v;
for (i = 1; i < N; ++i) { v = a[i];
j = i;
while (j > 0 && a[j-1] > v) { a[j] = a[j-1];
} --j;
a[j] = v;
} }
• On suppose les i-1 premiers
éléments triés et on insère le i-ème
à sa place parmi les i premiers
18 10 25 9 3 11 13 23 8
↑ i
3 10 18 25 9 3 11 13 23 8
↑ i
3 10 18 25 9 3 11 13 23 8
↑ i
3 3 9 10 18 25 11 13 23 8
↑ i
↑ i
3 3 9 10 11 18 25 13 23 8
↑ i
3 3 9 10 11 13 18 25 23 8
↑ i
3 3 8 9 10 11 13 18 23 25 3 9 10 18 25 3 11 13 23 8
↑ i 3 3 9 10 11 13 18 23 25 8 3
Tri par insertion