0% found this document useful (0 votes)
138 views15 pages

Laborator SDA 2

1. The document describes a lab report on sorting algorithms in C that was completed by student Daniel Iepuras of the Computer Science department at the Moldova Technical University. 2. The lab tasks involved writing programs to sort arrays, display the sorted array, and calculate the number of steps, iterations, and execution time using different sorting algorithms. 3. The results show that Shell sort and Quicksort were the fastest algorithms when sorting 5000 random elements, while Bubble sort and Selection sort took the most time and steps.

Uploaded by

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

Laborator SDA 2

1. The document describes a lab report on sorting algorithms in C that was completed by student Daniel Iepuras of the Computer Science department at the Moldova Technical University. 2. The lab tasks involved writing programs to sort arrays, display the sorted array, and calculate the number of steps, iterations, and execution time using different sorting algorithms. 3. The results show that Shell sort and Quicksort were the fastest algorithms when sorting 5000 random elements, while Bubble sort and Selection sort took the most time and steps.

Uploaded by

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

Ministerul Educaţiei culturii si cercetarii Republicii Moldova

Universitatea Tehnică a Moldovei


Facultatea Calculatoare, Informatică şi Microelectronică.
Departamentul Ingineria Software și Automatică

Raport la

Lucrare de laborator Nr.2


la Structuri de date si Algoritmi

A efectuat: st. gr. TI-174 Iepuras Daniel

A verificat: lect.asist. V.Lazu

Chişinău, 2018
Sarcini de lucru: De elaborat programme in c care sa afiseze tabloul sortat ,
numarul de total de pasi si iteratii, timpul de executie prin algoritmii de sortare
studiati

Listingul programului:
#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <time.h>

clock_t t_start, t_stop;

float sec;

int nrp1,nrp2,nrp3,nrp4,nrp5;

//Functia pentru implementarea BUBBLE-SORT

int BUBBLESORT(int a[],int n)

int swap(int *xp, int *yp)

int aux=*xp;

*xp=*yp;

*yp=aux;

int i,j;

int temp;

for(i=0;i<n-1;i++){

for(j=0;j<n-i-1;j++){
if(a[j]>a[j+1])

swap(&a[j], &a[j+1]);

nrp1++;}}

//Functia de afisare a tabloului

int AFISAREBUBBLE(int a[],int n)

int i;

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

printf("%d ",a[i]);

printf("\n");

//Functia INSERTSORT

int INSERTSORT(int a[], int n)

int i,aux,j;

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

aux=a[i];

j=i-1;

//Se muta elementele a[0..i-1] mai mari decat aux pe o pozitie inainte de la pozitia lor actuala

while(j>=0 && a[j]>aux)

a[j+1] = a[j];

j=j-1;
}

a[j+1]=aux;

nrp2++;

//Functia de afisare a tabloului

int AFISAREINSERT(int a[],int n)

int i;

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

printf("%d ",a[i]);

printf("\n");

//Functia pentru implementarea SELECTOSORT

int SELECTSORT(int a[],int n)

int swap(int *xp, int *yp)

int aux=*xp;

*xp=*yp;

*yp=aux;

int i, j, min;

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

{
//Gasim elementul minim in tabloul nesortat

min=i;

for(j=i+1;j<n;j++){

if(a[j]<a[min])

min=j;

//Interschimbam elementul minim gasit cu primul element

swap(&a[min], &a[i]);

nrp3++;}}

//Functia de afisare a tabloului

int AFISARE_SELECT_SORT(int a[],int n)

int i;

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

printf("%d ",a[i]);

printf("\n");

int SHELLSORT(int a[],int n)

int i,j,k,aux;

for(i=n/2;i>0;i=i/2)//for(i=n/2;i>0;i/=2)

for(j=i;j<n;j++)
{

for(k=j-i;k>=0;k=k-i)

if(a[k+i]>=a[k])

break;

else

aux=a[k];

a[k]=a[k+i];

a[k+i]=aux;

nrp4++; }

//Functia de afisare a tabloului

int AFISARE_SHELL_SORT(int a[],int n)

int i;

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

printf("%d ",a[i]);

printf("\n");

int QUICKSORT(int a[],int inf, int sup)

int i,j;

int t,x;
x=a[inf];

i=inf;

j=sup;

while(i<j)

while(a[i]<=x && i<sup)

i++;

while(a[j]>x)

j--;

if(i<j)

t=a[i];

a[i]=a[j];

a[j]=t;

if(j>inf)

a[inf]=a[j];

a[j]=x;

nrp5++;}

if(inf<j-1)QUICKSORT(a,inf,j-1);

if(j+1<sup)QUICKSORT(a,sup,j+1);

int AFISAREQUICKSORT (int a[], int n)

int i;

for(i=0;i<n;i++)
printf("%d ",a[i]);

printf("\n");

//Programul principal pentru testarea funtiilor de mai sus

int main()

int k=0;

printf("\t\t\t LABORATORUL NR 2 LA SDA\n");

printf("\t\t\t'ALGORITMI DE SORTARE\n");

Menu:

Menu1:

printf("\t1.BUBBLE-SORT\n");

printf("\t2.INSERT-SORT\n");

printf("\t3.SELECT-SORT\n");

printf("\t4.SHELL-SORT\n");

printf("\t5.QUICK-SORT\n");

scanf("%d",&k);

if(k==1)

int a[5000],n=5000,i;

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

a[i]=rand()%500;

t_start= clock();

BUBBLESORT(a,n);

t_stop= clock();

sec=((float)(t_stop-t_start))/CLOCKS_PER_SEC;

printf("Tabloul sortat:\n");
AFISAREBUBBLE(a,n);

printf("\n");

printf("\nTabloul a fost executat timp de %.4f secunde",sec);

printf("\n");

printf("\nUtilizand %d pasi",nrp1);

printf("\n");

goto Menu1;

if(k==2)

int a[5000],n=5000,i;

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

a[i]=rand()%500;

t_start=clock();

INSERTSORT(a,n);

t_stop=clock();

sec = ((float)(t_stop - t_start)) / CLOCKS_PER_SEC;

printf("Tabloul sortat:\n");

AFISAREINSERT(a,n);

printf("\n");

printf("\nTabloul a fost executat timp de %.4f secunde",sec);

printf("\n");

printf("\nUtilizand %d pasi",nrp2);

printf("\n");

goto Menu1;

if (k==3)

int a[5000],n=5000,i;
for(i=0;i<n;i++)

a[i]=rand()%500;

t_start=clock();

SELECTSORT(a,n);

t_stop=clock();

sec = ((float)(t_stop - t_start)) / CLOCKS_PER_SEC;

printf("Tabloul sortat:\n");

AFISARE_SELECT_SORT(a,n);

printf("\n");

printf("\nTabloul a fost executat in %.4f secunde",sec);

printf("\n");

printf("\nUtilizand %d pasi",nrp3);

printf("\n");

goto Menu1;

if(k==4)

int a[5000],n=5000,i;

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

a[i]=rand()%500;

t_start=clock();

SHELLSORT(a,n);

t_stop=clock();

sec = ((float)(t_stop - t_start)) / CLOCKS_PER_SEC;

printf("Tabloul sortat:\n");

AFISARE_SHELL_SORT(a,n);

printf("\n");

printf("\nTabloul a fost executat timp de %.4f secunde",sec);

printf("\n");
printf("\nUtilizand %d pasi",nrp4);

printf("\n");

goto Menu1;

if(k==5)

int a[5000],n=5000,i;

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

a[i]=rand()%500;

t_start=clock();

QUICKSORT(a,0,n-1);

t_stop=clock();

printf("\nTabloul sortat:");

AFISAREQUICKSORT(a,n);

printf("\n");

printf("\nTabloul a fost executat timp de %.4f secunde",sec);

printf("\n");

printf("\nUtilizand %d pasi ",nrp5);

printf("\n");

goto Menu1;

return 0;

}
Date afisate :
Algoritm de sortare a bulelor :

Algoritm de sortare prin insertie:


Algoritm de sortare prin selectie:

Algoritm de sortare Shell:


Algoritm de sortare Quicksort :

Rezultate obtinute 5000 elemente :

Algoritm Timp Pasi


1.Bulelor 0.1300sec 12497500
2.Insertiei 0.0270 sec 4999
3.Selectiei 0.1620 sec 12497500
4.Shell 0.0010 sec 52057
5.QuikSort 0.0000 sec 43117

Concluzie:

In aceasta lucrare de laborator am capatat noi cunostinte despre algortimii de


sortare BUBBLE-SORT,INSERT-SORT, SELECT-SORT,SHELL-SORT,QUCIK-SORT si am
capatat experienta in aplicarea acestora . Am aflat rapiditatea fiecarui algoritm
cu ajutorul functiilor clock_t, t_start, t_stop , care se afla in biblioteca <time.h>.
De asemenea am ajuns la concluzia ca Shell-sort si Quick-sort sunt cele mai
rapide algoritme de sortare.

You might also like