Skip to content
Snippets Groups Projects
Commit f5c33605 authored by paul.albuquer's avatar paul.albuquer
Browse files

added source code for binary trees only

parent 6ecbf7fc
Branches
No related tags found
No related merge requests found
Showing
with 1218 additions and 0 deletions
with Ada.Text_Io; use Ada.Text_Io;
with Ada.Unchecked_Deallocation;
package body Arbre_Binaire is
procedure Liberer is new Ada.Unchecked_Deallocation(T_Noeud,T_Arbre);
procedure Position(A : in T_Arbre;
Cle : in Integer;
Nd,Parent : in out T_Arbre) is
begin
if A = null then
raise ARBRE_VIDE;
end if;
while Nd.Cle /= Cle loop
if Cle < Nd.Cle then
exit when Nd.Gauche = null;
Parent := Nd;
Nd := Nd.Gauche;
else
exit when Nd.Droite = null;
Parent := Nd;
Nd := Nd.Droite;
end if;
end loop;
end Position;
procedure Insert(A : in out T_Arbre; Cle : in Integer) is
Parent : T_Arbre := null;
Nd : T_Arbre := A;
begin
Position(A,Cle,Nd,Parent);
if Cle < Nd.Cle then
Nd.Gauche := new T_Noeud'(Cle,null,null);
elsif Cle > Nd.Cle then
Nd.Droite := new T_Noeud'(Cle,null,null);
else
raise CLE_PRESENTE;
end if;
exception
when ARBRE_VIDE => A := new T_Noeud'(Cle,null,null);
end Insert;
procedure Delete(A : in out T_Arbre; Cle : in Integer) is
Parent, Tmp : T_Arbre := null;
Nd : T_Arbre := A;
begin
Position(A,Cle,Nd,Parent);
if Cle /= Nd.Cle then
raise CLE_ABSENTE;
end if;
if Nd.Gauche /= null then
Parent := Nd;
Tmp := Nd.Gauche;
Position(Nd.Gauche,Cle,Tmp,Parent);
elsif Nd.Droite /= null then
Parent := Nd;
Tmp := Nd.Droite;
Position(Nd.Droite,Cle,Tmp,Parent);
end if;
Nd.Cle := Tmp.Cle;
if Parent = null then
A := null;
elsif Parent.Gauche = Tmp then
Parent.Gauche := Tmp.Gauche;
else
Parent.Droite := Tmp.Droite;
end if;
Liberer(Tmp);
end Delete;
function Search(A : T_Arbre; Cle : Integer) return T_Arbre is
Parent : T_Arbre := null;
Nd : T_Arbre := A;
begin
Position(A,Cle,Nd,Parent);
if Cle /= Nd.Cle then
raise CLE_ABSENTE;
end if;
return Nd;
end Search;
end Arbre_Binaire;
package Arbre_Binaire is
type T_Arbre is private;
-- Fonctionnalités d'un arbre binaire ordonné (fonctions et procédures)
procedure Insert(A : in out T_Arbre; Cle : in Integer);
procedure Delete(A : in out T_Arbre; Cle : in Integer);
function Search(A : T_Arbre; Cle : Integer) return T_Arbre;
-- Exceptions
ARBRE_VIDE : exception;
CLE_ABSENTE : exception;
CLE_PRESENTE : exception;
private
-- Implémentation de la structure d'arbre binaire
type T_Noeud;
type T_Arbre is access T_Noeud;
type T_Noeud is
record
Cle : T_Cle;
Gauche : T_Arbre := null;
Droite : T_Arbre := null;
end record;
end Arbre_Binaire;
package Arbre_Binaire is
type T_Arbre is private;
-- Fonctionnalités d'un arbre binaire ordonné (fonctions et procédures)
procedure Insert(A : in out T_Arbre; Cle : in Integer);
procedure Delete(A : in out T_Arbre; Cle : in Integer);
function Search(A : T_Arbre; Cle : Integer) return T_Arbre;
-- Exceptions
ARBRE_VIDE : exception;
CLE_ABSENTE : exception;
CLE_PRESENTE : exception;
private
-- Implémentation de la structure d'arbre binaire
type T_Noeud;
type T_Arbre is access T_Noeud;
type T_Noeud is
record
Cle : T_Cle;
Gauche : T_Arbre := null;
Droite : T_Arbre := null;
end record;
end Arbre_Binaire;
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include "arbre_binaire.h"
static node* position(arbre tree,int cle) {
node* crt = tree;
if (NULL != tree) {
do {
if (cle < crt->key) {
if (NULL == crt->gauche) {
break;
}
crt = crt->gauche;
} else if (cle > crt->key) {
if (NULL == crt->droite) {
break;
}
crt = crt->droite;
}
} while (crt->key != cle);
}
return crt;
}
bool arbre_insert(arbre* tree,int cle) {
if (NULL == *tree) {
*tree = calloc(1,sizeof(node));
(*tree)->key = cle;
} else {
node* nd = position(*tree,cle);
if (cle < nd->key) {
nd->gauche = calloc(1,sizeof(node));
nd->gauche->key = cle;
} else if (cle > nd->key) {
nd->droite = calloc(1,sizeof(node));
nd->droite->key = cle;
} else {
return false;
}
}
return true;
}
static node* parent(arbre tree,node* nd) {
assert(NULL != tree && NULL != nd);
node* parent = NULL;
if (nd != tree) {
node* crt = tree;
int cle = nd->key;
do {
parent = crt;
if (cle < crt->key) {
crt = crt->gauche;
} else if (cle > crt->key) {
crt = crt->droite;
}
} while (crt != nd);
}
return parent;
}
bool arbre_delete(arbre* tree,int cle) {
node* nd = position(*tree,cle);
if (NULL == nd || cle != nd->key) {
return false;
}
// terminal node
if (NULL == nd->gauche && NULL == nd->droite) {
node* nd_parent = parent(*tree,nd);
if (NULL == nd_parent) { // single node tree
*tree = NULL;
} else if (nd == nd_parent->gauche) {
nd_parent->gauche = NULL;
} else if (nd == nd_parent->droite) {
nd_parent->droite = NULL;
}
free(nd);
return true;
}
if (NULL != nd->gauche) {
node* child = position(nd->gauche,cle);
int val = child->key;
if (NULL == nd->gauche->droite) {
nd->gauche = child->gauche;
free(child);
} else {
bool res = arbre_delete(tree,child->key);
}
nd->key = val;
return true;
}
if (NULL != nd->droite) {
node* child = position(nd->droite,cle);
int val = child->key;
if (NULL == nd->droite->gauche) {
nd->droite = child->droite;
free(child);
} else {
bool res = arbre_delete(tree,child->key);
}
nd->key = val;
return true;
}
}
void arbre_print(arbre tree,int N) {
if (N <= 0) {
N = 1;
}
if (NULL != tree) {
arbre_print(tree->droite,N+1);
for (int i=0;i<N;i++) {
printf(" ");
}
printf("%d\n",tree->key);
arbre_print(tree->gauche,N+1);
}
}
bool arbre_search(arbre tree,int cle) {
node* nd = position(tree,cle);
return (NULL != nd && cle == nd->key);
}
#ifndef ARBRE_BINAIRE_H
#define ARBRE_BINAIRE_H
// Structure pour un arbre binaire
typedef int cle;
typedef struct _node {
cle key;
struct _node* gauche;
struct _node* droite;
} node;
typedef node* arbre;
// Fonctionnalités pour un arbre binaire ordonné
bool arbre_search(arbre tree,int cle);
bool arbre_insert(arbre* tree,int cle);
bool arbre_delete(arbre* tree,int cle);
void arbre_print(arbre tree,int N);
#endif
with Ada.Text_Io; use Ada.Text_Io;
with Ada.Unchecked_Deallocation;
package body Arbre_Binaire is
procedure Liberer is new Ada.Unchecked_Deallocation(T_Noeud,T_Arbre);
procedure Position(A : in T_Arbre;
Cle : in Integer;
Nd,Parent : in out T_Arbre) is
begin
if A = null then
raise ARBRE_VIDE;
end if;
while Nd.Cle /= Cle loop
if Cle < Nd.Cle then
exit when Nd.Gauche = null;
Parent := Nd;
Nd := Nd.Gauche;
else
exit when Nd.Droite = null;
Parent := Nd;
Nd := Nd.Droite;
end if;
end loop;
end Position;
procedure Insert(A : in out T_Arbre; Cle : in Integer) is
Parent : T_Arbre := null;
Nd : T_Arbre := A;
begin
Position(A,Cle,Nd,Parent);
if Cle < Nd.Cle then
Nd.Gauche := new T_Noeud'(Cle,null,null);
elsif Cle > Nd.Cle then
Nd.Droite := new T_Noeud'(Cle,null,null);
else
raise CLE_PRESENTE;
end if;
exception
when ARBRE_VIDE => A := new T_Noeud'(Cle,null,null);
end Insert;
procedure Delete(A : in out T_Arbre; Cle : in Integer) is
Parent, Tmp : T_Arbre := null;
Nd : T_Arbre := A;
begin
Position(A,Cle,Nd,Parent);
if Cle /= Nd.Cle then
raise CLE_ABSENTE;
end if;
if Nd.Gauche /= null then
-- à compléter
null;
elsif Nd.Droite /= null then
-- à compléter
null;
end if;
Nd.Cle := Tmp.Cle;
if Parent = null then
-- à compléter
null;
elsif Parent.Gauche = Tmp then
-- à compléter
null;
else
-- à compléter
null;
end if;
Liberer(Tmp);
end Delete;
function Search(A : T_Arbre; Cle : Integer) return T_Arbre is
Parent : T_Arbre := null;
Nd : T_Arbre := A;
begin
Position(A,Cle,Nd,Parent);
if Cle /= Nd.Cle then
raise CLE_ABSENTE;
end if;
return Nd;
end Search;
end Arbre_Binaire;
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include <math.h>
#include "bin_tree.h"
static node* position(arbre tree,cle c) {
node* crt = tree;
if (NULL != crt) {
while (c != crt->key
&& NULL != crt->child[(c > crt->key)]) {
crt = crt->child[(c > crt->key)];
}
}
return crt;
}
bool arbre_is_empty(arbre tree) {
return NULL == tree;
}
int arbre_depth(arbre tree) {
if (arbre_is_empty(tree)) {
return 0;
} else {
return 1+fmax(arbre_depth(tree->child[0]),arbre_depth(tree->child[1]));
}
}
int arbre_size(arbre tree) {
if (arbre_is_empty(tree)) {
return 0;
} else {
return 1+arbre_size(tree->child[0])+arbre_size(tree->child[1]);
}
}
static node* create_node(int val) {
node* nd = calloc(1,sizeof(node));
nd->key = val;
return nd;
}
arbre arbre_insert(arbre tree,cle c) {
if (arbre_is_empty(tree)) {
tree = create_node(c);
} else {
node* nd = position(tree,c);
nd->child[(c > nd->key)] = create_node(c);
}
return tree;
}
static node* parent(arbre tree,node* nd) {
assert(!arbre_is_empty(tree) && NULL != nd);
node* parent = NULL;
if (nd != tree) {
node* crt = tree;
cle c = nd->key;
do {
parent = crt;
if (c != crt->key) {
crt = crt->child[(c > crt->key)];
}
} while (crt != nd);
}
return parent;
}
bool arbre_search(arbre tree,cle c) {
node* nd = position(tree,c);
return (NULL != nd && c == nd->key);
}
bool arbre_delete(arbre* tree,cle c) {
node* nd = position(*tree,c);
if (NULL == nd || c != nd->key) {
return false;
}
// noeud terminal ou avec 1 enfant
if (arbre_is_empty(nd->child[0]) || arbre_is_empty(nd->child[1])) {
node* nd_parent = parent(*tree,nd);
if (NULL == nd_parent) { // nd est la racine
*tree = nd->child[(NULL != nd->child[1])];
} else {
nd_parent->child[(nd == nd_parent->child[1])]
= nd->child[(NULL != nd->child[1])];
}
free(nd);
} else { // noeud interne (2 enfants)
node* next = position(nd->child[1],c); //next a 0 ou 1 enfant
cle tmp = next->key;
bool res = arbre_delete(tree,tmp);
nd->key = tmp;
}
return true;
}
bool arbre_delete_bis(arbre* tree,cle c) {
node* nd = position(*tree,c);
if (NULL == nd || c != nd->key) {
return false;
}
// terminal node
if (arbre_is_empty(nd->child[0]) && arbre_is_empty(nd->child[1])) {
node* nd_parent = parent(*tree,nd);
if (NULL == nd_parent) { // single node tree
*tree = NULL;
} else {
nd_parent->child[(nd == nd_parent->child[1])] = NULL;
}
free(nd);
return true;
}
for (int ind=0;ind<2;ind++) {
if (!arbre_is_empty(nd->child[ind])) {
node* next = position(nd->child[ind],c);
int val = next->key;
if (NULL == nd->child[ind]->child[ind^1]) {
nd->child[ind] = next->child[ind];
free(next);
} else {
bool res = arbre_delete(tree,next->key);
}
nd->key = val;
return true;
}
}
}
void arbre_print(arbre tree,int N) {
if (N <= 0) {
N = 1;
}
if (NULL != tree) {
arbre_print(tree->child[1],N+1);
for (int i=0;i<N;i++) {
printf(" ");
}
printf("%d\n",tree->key);
arbre_print(tree->child[0],N+1);
}
}
#ifndef BIN_TREE_H
#define BIN_TREE_H
// Structure pour un arbre binaire
typedef int cle;
typedef struct _node {
cle key;
struct _node* child[2];
} node;
typedef node* arbre;
// Fonctionnalités pour un arbre binaire ordonné
bool arbre_is_empty(arbre tree);
bool arbre_search(arbre tree,cle c);
bool arbre_insert(arbre* tree,cle c);
bool arbre_delete(arbre* tree,cle c);
void arbre_print(arbre tree,int N);
int arbre_depth(arbre tree);
int arbre_size(arbre tree);
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "bin_tree.h"
void main() {
bool b;
int val;
arbre tree = NULL;
arbre_print(tree,1);
do {
printf("insert val = ");
scanf("%d",&val);
b = arbre_insert_rec(&tree,val);
arbre_print(tree,1);
} while (b);
node* nd;
do {
printf("delete val = ");
scanf("%d",&val);
b = arbre_delete(&tree,val);
arbre_print(tree,1);
} while (NULL != tree);
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "bin_tree_rec.h"
void main() {
int val;
arbre tree = NULL;
arbre_print(tree,1);
do {
printf("insert val = ");
scanf("%d",&val);
if (arbre_search(tree,val)) break;
tree = arbre_insert(tree,val);
arbre_print(tree,1);
} while (true);
do {
printf("find val = ");
scanf("%d",&val);
if (!arbre_search(tree,val)) {
printf("not found\n");
break;
}
printf("found\n");
arbre_print(tree,1);
} while (true);
node* nd;
do {
printf("delete val = ");
scanf("%d",&val);
tree = arbre_delete(tree,val);
arbre_print(tree,1);
} while (!arbre_is_empty(tree));
}
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include "bin_tree.h"
static node* position(arbre tree,int cle) {
node* crt = tree;
if (NULL != crt) {
while (cle != crt->key
&& NULL != crt->child[(cle > crt->key)]) {
crt = crt->child[(cle > crt->key)];
}
}
return crt;
}
static node* parent(arbre tree,node* nd) {
assert(NULL != tree && NULL != nd);
node* parent = NULL;
int cle = nd->key;
if (nd != tree) {
node* crt = tree;
do {
//à compléter
} while (crt != nd);
}
return parent;
}
int arbre_depth(arbre tree) {
if (NULL == tree) {
return 0;
} else {
return 1+fmax(arbre_depth(tree->child[0]),arbre_depth(tree->child[1]));
}
}
int arbre_size(arbre tree) {
//à compléter
return 0;
}
bool arbre_insert(arbre* tree,int cle) {
if (NULL == *tree) {
*tree = calloc(1,sizeof(node));
(*tree)->key = cle;
} else {
node* nd = position(*tree,cle);
if (cle != nd->key) {
//à compléter
} else {
return false;
}
}
return true;
}
bool arbre_delete(arbre* tree,int cle) {
node* nd = position(*tree,cle);
if (NULL == nd || cle != nd->key) {
return false;
}
// noeud terminal
if (NULL == nd->child[0] && NULL == nd->child[1]) {
node* nd_parent = parent(*tree,nd);
// à compléter:
// considérer les cas de l'arbre à un seul/plusieurs noeuds
free(nd);
return true;
}
// noeud interne et récursion
for (int ind=0;ind<2;ind++) {
if (NULL != nd->child[ind]) {
node* next = position(nd->child[ind],cle);
int val = next->key;
if (NULL == nd->child[ind]->child[ind^1]) {
nd->child[ind] = next->child[ind];
free(next);
} else {
bool res = arbre_delete(tree,next->key);
}
nd->key = val;
return true;
}
}
}
void arbre_print(arbre tree,int N) {
if (N <= 0) {
N = 1;
}
if (NULL != tree) {
arbre_print(tree->child[1],N+1);
for (int i=0;i<N;i++) {
printf(" ");
}
printf("%d\n",tree->key);
arbre_print(tree->child[0],N+1);
}
}
bool arbre_search(arbre tree,int cle) {
// à compléter
return true;
}
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include <math.h>
#include "bin_tree_rec.h"
static node* create_node(int val);
static node* position(arbre tree,cle c);
bool arbre_is_empty(arbre tree) {
return NULL == tree;
}
int arbre_depth(arbre tree) {
if (arbre_is_empty(tree)) {
return 0;
} else {
return 1+fmax(arbre_depth(tree->child[0]),arbre_depth(tree->child[1]));
}
}
int arbre_size(arbre tree) {
if (arbre_is_empty(tree)) {
return 0;
} else {
return 1+arbre_size(tree->child[0])+arbre_size(tree->child[1]);
}
}
arbre arbre_insert(arbre tree,cle c) {
if (arbre_is_empty(tree)) {
return create_node(c);
}
node* nd = position(tree,c);
nd->child[(c > nd->key)] = create_node(c);
return tree;
}
bool arbre_search(arbre tree,cle c) {
node* nd = position(tree,c);
return (NULL != nd && c == nd->key);
}
arbre arbre_delete(arbre tree,cle c) {
if (!arbre_is_empty(tree)) {
if (c != tree->key) {
tree->child[(c > tree->key)] = arbre_delete(tree->child[(c > tree->key)],c);
} else if (!arbre_is_empty(tree->child[0])
&& !arbre_is_empty(tree->child[1])) { // noeud interne (2 enfants)
node* next = position(tree->child[1],c);
tree->key = next->key;
tree->child[1] = arbre_delete(tree->child[1],next->key);
} else {
node* nd = tree;
tree = tree->child[!arbre_is_empty(tree->child[1])];
free(nd);
}
}
return tree;
}
void arbre_print(arbre tree,int N) {
if (N <= 0) {
N = 1;
}
if (NULL != tree) {
arbre_print(tree->child[1],N+1);
for (int i=0;i<N;i++) {
printf(" ");
}
printf("%d\n",tree->key);
arbre_print(tree->child[0],N+1);
}
}
static node* create_node(int val) {
node* nd = calloc(1,sizeof(node));
nd->key = val;
return nd;
}
static node* position(arbre tree,cle c) {
if (!arbre_is_empty(tree)) {
if (c != tree->key && !arbre_is_empty(tree->child[(c > tree->key)])) {
return position(tree->child[(c > tree->key)],c);
}
}
return tree;
}
#ifndef BIN_TREE_H
#define BIN_TREE_H
// Structure pour un arbre binaire
typedef int cle;
typedef struct _node {
cle key;
struct _node* child[2];
} node;
typedef node* arbre;
// Fonctionnalités pour un arbre binaire ordonné
bool arbre_is_empty(arbre tree);
bool arbre_search(arbre tree,cle c);
arbre arbre_insert(arbre tree,cle c);
arbre arbre_delete(arbre tree,cle c);
void arbre_print(arbre tree,int N);
int arbre_depth(arbre tree);
int arbre_size(arbre tree);
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "bin_tree.h"
void main() {
bool b;
int val;
arbre tree = NULL;
arbre_print(tree,1);
do {
printf("insert val = ");
scanf("%d",&val);
b = arbre_insert(&tree,val);
arbre_print(tree,1);
} while (b);
node* nd;
do {
printf("delete val = ");
scanf("%d",&val);
b = arbre_delete(&tree,val);
arbre_print(tree,1);
} while (NULL != tree);
}
procedure Tree_Insert is
type T_Noeud;
type T_Arbre is access T_Noeud;
type T_Noeud is record
Info : Integer;
Sag : T_Arbre;
Sad : T_Arbre;
end record;
procedure Insert(A : in out T_Arbre;
I : in Integer) is
Crt : T_Arbre := A;
begin
if A = null then
A := new T_Noeud'(I,null,null);
else
while I /= Crt.Info loop
if I > Crt.Info then
if Crt.Sag = null then
Crt.Sag := new T_Noeud'(I,null,null);
end if;
Crt := Crt.Sag;
elsif I < Crt.Info then
if Crt.Sad = null then
Crt.Sad := new T_Noeud'(I,null,null);
end if;
Crt := Crt.Sad;
end if;
end loop;
end if;
end Insert;
begin
null;
end Tree_Insert;
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef int cle;
typedef struct _node {
cle key;
struct _node* gauche;
struct _node* droite;
} node;
typedef node* arbre;
arbre search(cle X,arbre tree) {
bool success = false;
arbre courant = tree;
while (NULL != courant && !success) {
if (courant->key > X) {
courant = courant->gauche;
} else if (courant->key < X){
courant = courant->droite;
} else {
success = true;
}
}
return courant;
}
void main() {
arbre tree;
arbre t = search(15,tree);
}
CC:=gcc
# SAN:=-fsanitize=address
CFLAGS:=-Wall -Wextra -pedantic -g $(SAN)
LDFLAGS:=-lm $(SAN)
all: quicksort quicksort_part
quicksort: quicksort.c
$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
@echo $@ >> .gitignore
quicksort_part: quicksort_part.c
$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
@echo $@ >> .gitignore
.PHONY: clean all
clean:
rm -f *.o quicksort_part quicksort .gitignore
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
void print(int size,int tab[size]) {
for (int i=0;i<size;i++) {
printf("%d ",tab[i]);
}
}
void random_tab(int size,int tab[size],int inf,int sup) {
assert(sup > inf);
for (int i=0;i<size;i++) {
tab[i] = inf+rand()%(sup-inf);
}
}
void swap(int* p_a,int* p_b) {
int tmp = *p_a;
*p_a = *p_b;
*p_b = tmp;
}
int partition(int size,int array[size],int first,int last) {
int pivot = array[last];
int i = first-1,j = last;
do {
do {
i++;
} while (array[i] < pivot && i<j);
do {
j--;
} while(array[j] > pivot && i<j);
if (j>i) {
swap(&array[i],&array[j]);
}
} while (j > i);
swap(&array[i],&array[last]);
return i;
}
void quicksort(int size,int array[size],int first,int last) {
if (first < last) {
int midpoint = partition(size,array,first,last);
if (first < midpoint-1) {
quicksort(size,array,first,midpoint-1);
}
if (midpoint+1 < last) {
quicksort(size,array,midpoint+1,last);
}
}
}
void test_ordre(int size,int array[size]) {
for (int i=0;i<size-1;i++) {
if (array[i] > array[i+1]) {
printf("erreur");
return;
}
}
printf("ok");
}
int main(int argc,char** argv) {
if (2 != argc) {
return 1;
}
int size = atoi(argv[1]);
int seed = atoi(argv[2]);
srand(seed);
int* res = malloc(size*sizeof(int));
for (int k=0;k<20;k++) {
random_tab(size,res,0,100);
print(size,res);
printf("\n");
quicksort(size,res,0,size-1);
print(size,res);
test_ordre(size,res);
printf("\n================\n");
}
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void print(int size,int tab[size]) {
for (int i=0;i<size;i++) {
printf("%d ",tab[i]);
}
printf("\n");
}
void random_tab(int size,int tab[size],int inf,int sup) {
for (int i=0;i<size;i++) {
tab[i] = inf+rand()%(sup-inf);
}
}
void swap(int* p_a,int* p_b) {
int tmp = *p_a;
*p_a = *p_b;
*p_b = tmp;
}
// Partition du tableau <array> autour d'une valeur pivot:
// compléter le code
int partition(int size,int array[size],int first,int last) {
int pivot = array[last];
int i = first-1,j = last;
do {
// à compléter pour <i>: do {...} while (...);
// à compléter pour <j>: do {...} while (...);
// à compléter: échanger cases <i> et <j> du tableau <array>
} while (j > i);
// à compléter: échanger cases <i> et <last> du tableau <array>
return i;
}
// Tri rapide récursif
void quicksort(int size,int array[size],int first,int last) {
if (first < last) {
int midpoint = partition(size,array,first,last);
if (first < midpoint-1) {
quicksort(size,array,first,midpoint-1);
}
if (midpoint+1 < last) {
quicksort(size,array,midpoint+1,last);
}
}
}
// Test si le tableau <array> est ordonné croissant
void test_ordre(int size,int array[size]) {
for (int i=0;i<size-1;i++) {
if (array[i] > array[i+1]) {
printf("erreur");
return;
}
}
printf("ok");
}
int main(int argc,char** argv) {
if (2 != argc) {
return 1;
}
int size = atoi(argv[1]);
int seed = atoi(argv[2]);
srand(seed);
int* res = (int*)malloc(size*sizeof(int));
for (int k=0;k<20;k++) {
random_tab(size,res,0,100);
print(size,res);
quicksort(size,res,0,size-1);
print(size,res);
test_ordre(size,res);
printf("\n================\n");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment