0% found this document useful (0 votes)
51 views5 pages

Travail Pratique nr.2: Algorithmes de Tri Et de Recherche

The document is a student paper in French that analyzes sorting algorithms. It compares bubble sort, insertion sort, and shaker sort on arrays of data. For bubble sort and shaker sort, shaker sort has fewer comparisons and is faster. For insertion sort and shaker sort on the same data, insertion sort has fewer comparisons and is more efficient. In conclusion, the document finds that insertion sort is generally the fastest sorting method analyzed.

Uploaded by

AlexandruBotnaru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views5 pages

Travail Pratique nr.2: Algorithmes de Tri Et de Recherche

The document is a student paper in French that analyzes sorting algorithms. It compares bubble sort, insertion sort, and shaker sort on arrays of data. For bubble sort and shaker sort, shaker sort has fewer comparisons and is faster. For insertion sort and shaker sort on the same data, insertion sort has fewer comparisons and is more efficient. In conclusion, the document finds that insertion sort is generally the fastest sorting method analyzed.

Uploaded by

AlexandruBotnaru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Ministère de l'éducation de la République de Moldova

Université technique de Moldavie


Faculté d’Ordinateur, Informatique et Microélectronique
Filière francophone “Informatique”

SDA

Travail pratique nr.2


Thème:
Algorithmes de tri et de recherche
Effectué par l’étudiant(e) de gr FI-191 : Botnaru Alexandru

Vérifié par le professeur : Rusu Viorel

Chișinau 2020
#include <stdio.h>
void swap(int *a, int *b){
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void mbulelor(int x[],int n){
int i,j,c=0,m=0;

for (i = 0; i < n; ++i)


{
for (j = i + 1; j < n; ++j)
{
c++;
if (x[i] < x[j]) {
swap(&x[i],&x[j]);
m++;
}
}
}
printf("\nTabloul A sortat prin Bubble sort:\n");
for (i = 0; i < n; ++i)
{
printf("% d ", x[i]);
}
printf("\nNumarul de comparari = %d si permutari= %d",c,m);
}

void shakersort(int x[],int n){


int p, i,c=0,m=0;
for (p = 1; p <= n / 2; p++) {
for (i = p - 1; i < n - p; i++) {
c++;
if (x[i] < x[i + 1]) {
swap(&x[i], &x[i + 1]);
m++;
}
}
for (i = n - p - 1; i >= p; i--) {
c++;
if (x[i] > x[i - 1]) {
swap(&x[i], &x[i - 1]);
m++;
}
}
}
printf("\nTabloul B sortat prin Shaker sort:\n");
for (i = 0; i < n; ++i)
printf("% d ", x[i]);

printf("\nNumarul de comparari = %d si permutari= %d",c,m);


}
int main() {
int A[30],B[30];
int i,n;

printf("Numarul de elemente:\n");
scanf("%d", &n);
printf("Introduceti elementele tabloului A \n");
for (i = 0; i < n; ++i)
scanf("%d", &A[i]);

printf("Tabloul A:");
for(i=0;i<n;i++)
printf("%4d",A[i]);

mbulelor(A,n);

printf("\nIntroduceti elementele tabloului B \n");


for (i = 0; i < n; ++i)
scanf("%d", &B[i]);

printf("Tabloul B:");
for(i=0;i<n;i++)
printf("%4d",B[i]);

shakersort(B,n);
return 0;
}

#include <stdio.h>
void swap(int *a, int *b){
int temp;
temp = *a;
*a = *b;
*b = temp;
}

void shakersort(int x[],int n){


int p, i,c=0,m=0;
for (p = 1; p <= n / 2; p++) {
for (i = p - 1; i < n - p; i++) {
c++;
if (x[i] > x[i + 1]) {
swap(&x[i], &x[i + 1]);
m++;
}
}

for (i = n - p - 1; i >= p; i--) {


c++;
if (x[i] < x[i - 1]) {
swap(&x[i], &x[i - 1]);
m++;
}
}
}
printf("\nTabloul B sortat prin Shaker sort:\n");
for (i = 0; i < n; ++i)
printf("% d ", x[i]);

printf("\nNumarul de comparari = %d si permutari= %d",c,m);


}

void insertionsort(int x[],int n){


int i,j,c=0,m=0;
for(i=1;i<=n-1;i++){
j=i;
c++;
while (j>0 && x[j-1]>x[j]){
swap(&x[j],&x[j-1]);
j--;
m++;
}
}
printf("\nTabloul A sortat prin metoda insertiei:\n");
for (i = 0; i < n; ++i)
printf("% d ", x[i]);
printf("\nNumarul de comparari = %d si permutari= %d",c,m);

int main() {
int A[30],B[30];
int i,n;

printf("Numarul de elemente:\n");
scanf("%d", &n);
printf("Introduceti elementele tabloului A \n");
for (i = 0; i < n; ++i)
scanf("%d", &A[i]);

printf("Tabloul A:");
for(i=0;i<n;i++)
printf("%4d",A[i]);

insertionsort(A,n);

printf("\nIntroduceti elementele tabloului B \n");


for (i = 0; i < n; ++i)
scanf("%d", &B[i]);

printf("Tabloul B:");
for(i=0;i<n;i++)
printf("%4d",B[i]);

shakersort(B,n);
return 0;
}
Resultat:
On peut voir que le nombre de comparations est mieux dans le cas de le tri
par insertion,que le tri par la methode Shaker,dans le meme tableaux de donnees.
Ca signifie que le tri par insertion est une method de tri plus rapide est effective
que le Shaker sort,avec une complexite tres bonne.

Conclusions:
Dans ce travail pratique on a etudie le methods de tri,specialement le tri par
bulle,tri par insertion et Shakersort.
Dans la premiere problemme on a utilise le Shakersort et le tri par bulle,pour trier
un tableau de n donnees dans l’ordre décroissant et on a vu que le nombre des
comparaisons et de permutations était égal,mais le Shaker sort est un peu plus
rapide parcequ'il exécute un tri dans chaque direction à chaque passe le long de la
liste à trier.Dans la deuxieme problemme on a utilise le tri par insertion et le
Shaker sort et on a vu que le tri par insertion est un methode de tri plus bonne que
le Shaker sort et tri par bulle.

You might also like