Sunteți pe pagina 1din 13

Ministerul Educaiei al Republicii Moldova

Universitatea Tehnic a Moldovei


Facultatea Calculatoare, Informatic i Microelectronic
Catedra Informatica Aplicat

RAPORT
Lucrare de laborator Nr.3
la disciplina:Structuri de Date i Algoritmi
Tema: Algoritmi de sortare

A efectuat:

st. gr. SI-131


A.Spatari

A verificat:

dr., conf.univ.
L.Stadler

Chiinu 2014

Lucrare de laborator Nr.3


Scopul lucrrii: Acumularea deprinderilor practice de elaborare i programare a
alogoritmelor de calcul ramificat. Deprinderi de scriere i testare a programelor n
limbajul C.
Formularea condiii problemei (sarcina de lucru): De elaborat un program care
va afisa timpul de executie si numarul de permutari necesare fiecarui algoritm de
sortare : Bubble , Insertion,Selection,Shell,Quick.
Mersul lucrrii:
Noiuni principale din teorie i metode folosite:
Programarea calculatoarelor reprezint partea component a informaticii
(tiinei despre calculatoare), care studieaz i analizeaz diferite metode i
tehnici de elaborare i aplicare a algoritmilor i a programelor pentru rezolvarea a
diferitelor probleme la calculator.
Un algoritm de sortare este un algoritm care pune elementele unei liste ntr-o
anumit ordine. Cel mai des ntlnite sunt ordinea numeric i ordinea
lexicografic.

Schema logic a algoritmului:


1

Bubble

i=0;i<size;i=i+1

Start

16

25

15

8
t=clock()

k=0;k<size;k=k+1

s.nr_pr[0]=count;

t=clock()

j=0;j<size-1;j=j+1

2
srand(time(NULL))

9
Insert(array)

arrb[k]=arr[k]

17

26

16

s.nr_it[0]=cntit;

Quicks(array)

cntit=cntit+1

i=0;i<size;i=i+1

17
nu

18

da

27

t=clock()-t
10
44

array[i]=
cntit=0
rand()%1000

12

tm=((double)t)
cncom=cncom+1
/CLOCKS_PER_SEC
;

19

28

18
11

s.nr_com[0]=cncom+c
ount;

t=clock()-t

arrb[j] > arrb[j+1]

tm=((double)t)
/CLOCKS_PER_SEC
;

temp=arrb[j]

count=0

Iesire

29

19
13

t=clock()

s.t_ex[0]=tm*1000;

s.t_ex[0]=tm*1000;
arrb[j]=arrb[j+1]

6
cncom=0
20

30
14

t=clock()

Bubble(array)

Tipul Sortarii
Nr de iteratii
Nr de permutari
Timp de executie

arrb[j+1]=temp

21

Select(array)

t=clock()-t

15

count=count+1

31

i=0;i<5;i=i+1
22

23

9
s.t_ex[0]=tm*1000;

t=clock()

%s %10d %10d
%10.0f
milisecunde

tm=((double)t)
/CLOCKS_PER_SEC
;
24

10

32

t=clock()-t

tm=((double)t)
/CLOCKS_PER_SEC
;

s.t_ex[0]=tm*1000;

33

Nr de comparatii
pentru Bubble sort

11

Shell(array)
34
12

system(pause)

t=clock()-t
35
13

tm=((double)t)
/CLOCKS_PER_SEC
;

Stop

14

s.t_ex[0]=tm*1000;

da

nu

nu

da

nu

da

nu

da

11
11

Insert
Shell
Select
Quick

15

nus.nr_pr[3]=count
6

2
2

2 2 cntit=0
k=0;k<size;k=k+1

16

cntit=cnt
k=0;k<size;k=k+1

s.nr_it[3]=cntit
7

10

min=i

33
arrb[k]=arr[k]
33 count=0

Iesire

nu 44

11

arr[i] = arr[j]

j=i+1;j<size;j=j+1

k=0;k<size;k=k+1
count=0
4

temp = arr[i]

17

count=cit
arrse[k]=arr[k]

(i < left) && (arr[i] < pivot)

count=0

4
5
5

da

i=0;i<size;i=i+1
i <= j

da

12

arr[j] = temp

cntit=cntit+1

arrs[k]=arr[k]
5 cntit=0
cntit=0
5

13

i=i+1
arrse[j]<
10

i=0;i<size;i=i+1
6

arrse[min]

i=size/2;i>0;i=i/2

nu

i=i+1

14

11

14

s.nr_pr[1]=count

min=j

j=j-1

j=i-1;j>=0;j=j-1
7
j=i;j<size;j=j+1

da

(j > right) && (arr[j] > pivot)

15

15

s.nr_it[1]=cntit

8
8

cntit=cntit+1

12

k=j-1;k>=0;k=k-i

count=count+1

min!=i

16

17

13

Iesire

7
s.nr_pr[2]=count
9
j=j-1
arri[j]>
9

count=count+1

16

arri[j+1]

cntit=cntit+1

s.nr_pr[4]=count

18
10

s.nr_it[2]=cntit

tmp=arri[j]
14

10
arrs[k+i]>=arrs[k]
19

17

s.nr_it[4]=cntit
temp=arrse[i]

11

cntit=cntit+1

Iesire

da

11

arri[j]=arri[j+1]
tmp=arrs[k]

15

i <= j
arrse[i]=arrse[min]
18

nu

12
12

arri[j+1]=tmp
arrs[k]=arrs[k+i]

nu

da

13
21

i < left

16

nuarrse[min]=temp
19
right

da

<j

13

count=count+1
arrs[k+i]=tmp
20
22

14

Quicks
(arr,right,j,count,cntit)

Quicks
(arr,i,left,count,cntit)

count=count+1

23

Textul programului
in limbajul C
Iesire
#include<stdio.h>
#include<stdlib.h>

#include<time.h>
#include "struct.c"
#define size 5000
void Bubble(int arr[size]);
void Insert(int arr[size]);
void Select(int arr[size]);
void Shell(int arr[size]);
void Quicks(int array[size], int right, int left,int cnt,int cit);
int i,j,k;
sort s;
int main(){
char *sorts[] = {"Bubble Sort", "Insertion Sort","Selection Sort","Shell Sort", "Quick Sort"};
double tm;
int array[size];
srand (time(NULL));
for(i=0;i<size;i++)
array[i]=rand()%1000;
clock_t t;
t=clock();
Bubble(array);
t=clock()-t;
tm=((double)t)/CLOCKS_PER_SEC;
s.t_ex[0]=tm*1000;
t=clock();
Shell(array);
t=clock()-t;
tm=((double)t)/CLOCKS_PER_SEC;
s.t_ex[3]=tm*1000;

t=clock();
Insert(array);
t=clock()-t;
tm=((double)t)/CLOCKS_PER_SEC;
s.t_ex[1]=tm*1000;
t=clock();
Select(array);
t=clock()-t;
tm=((double)t)/CLOCKS_PER_SEC;
s.t_ex[2]=tm*1000;
t=clock();
Quicks(array,0,size-1,0,0);
t=clock()-t;
tm=((double)t)/CLOCKS_PER_SEC;
s.t_ex[4]=tm*1000;

printf("Tipul Sortarii\t\tNr de iteratii\tNr de permutari\t\tTimp de executie\n\n");


for(i=0;i<5;i++)
printf("%s\t\t%10d\t%10d\t%10.0f milisecunde\n",sorts[i],s.nr_it[i],s.nr_pr[i],s.t_ex[i]);
printf("\nNr de comparatii pentru Bubble sort : %d ",s.nr_com[0]);
printf("\n\n");
system("pause");
return 0;
}
void Bubble(int arr[size]){
int arrb[size];
int temp,count,cntit,cncom;
for(k=0; k<size; k++)
arrb[k]=arr[k];
cntit=0;
count=0;
cncom=0;
for(i=0; i<size ; i++)
{
for(j=0; j<(size-1)-i; j++)
{
cntit++;
if( arrb[j] > arrb[j+1] )
{
temp=arrb[j];
arrb[j]=arrb[j+1];
arrb[j+1]=temp;
count++;
}
else cncom++;
}
}
s.nr_pr[0]=count;
s.nr_it[0]=cntit;
s.nr_com[0]=cncom+count;
}
void Shell(int arr[size]){
int arrs[size],tmp,l,count,cntit;
cntit=0;
count=0;
for(k=0; k<size ; k++)
arrs[k]=arr[k];
for(i=size/2; i>0; i=i/2)
{
for(j=i; j<size; j++)
{
for(k=j-i; k>=0; k=k-i)
{

cntit++;
if(arrs[k+i]>=arrs[k])
break;
else
{
tmp=arrs[k];
arrs[k]=arrs[k+i];
arrs[k+i]=tmp;
count++;
}
}
}
}
s.nr_pr[3]=count;
s.nr_it[3]=cntit;
}
void Insert(int arr[size]){
int arri[size],tmp,count,cntit;
for(k=0; k<size ; k++)
arri[k]=arr[k];
count=0;
cntit=0;
for(i=0; i<size; i++)
{
for(j=i-1; j>=0; j--)
{
cntit++;
if(arri[j]>arri[j+1])
{
tmp=arri[j];
arri[j]=arri[j+1];
arri[j+1]=tmp;
count++;
}
else
break;
}
}
s.nr_pr[1]=count;
s.nr_it[1]=cntit;
}
void Select(int arr[size]){
int arrse[size],min,temp,count,cntit;
for(k=0; k<size ; k++)
arrse[k]=arr[k];
count=0;
cntit=0;
for(i=0; i<size; i++)
{
min=i;

10

for(j=i+1; j<size; j++)


cntit++;
if(arrse[j]<arrse[min])
min=j;
if(min!=i)
count++;
temp=arrse[i];
arrse[i]=arrse[min];
arrse[min]=temp;
}
s.nr_pr[2]=count;
s.nr_it[2]=cntit;
}
void Quicks(int arr[size], int right, int left,int cnt,int cit)
{
int count,temp,cntit;
int i = right;
int j = left;
int pivot = arr[(i + j) / 2];
count=cnt;
cntit=cit;
do
{
while ( (i < left) && (arr[i] < pivot) ) i++;
while ( (j > right) && (arr[j] > pivot) ) j--;
cntit++;
if (i <= j)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
count++;
}
s.nr_pr[4]=count;
s.nr_it[4]=cntit;
} while (i <= j);
if (right < j)
Quicks(arr, right, j,count,cntit);
if (i < left)
Quicks(arr, i, left,count,cntit);
}
struct tagsort{
int nr_it[5];
int nr_pr[5];
int nr_com[5];
double t_ex[5];
};
typedef struct tagsort sort;

Rezultatele obtinute:
11

Analiza rezultatelor si concluzii:


1. Au fost obinute unele deprinderi pentru scriere unui program n limbajul C.
2.Am obinut experiena in lucru cu structurile.
3.Am cptat experiena n utilizarea algoritmilor de sortare i funciilor recursive.
Bibliografie:
1. ..

12