0% found this document useful (0 votes)
735 views13 pages

UTM 2 Lab APA ORIGINAL

The document describes a laboratory report analyzing sorting algorithms. It implements Quicksort, Mergesort, and Bubblesort, calculating the number of iterations and runtime for each. Tables and graphs are constructed comparing the runtime and iterations. Quicksort is found to be the fastest algorithm, while Bubblesort is the slowest. The conclusion states Quicksort has more iterations but is still the quickest, and Bubblesort is the slowest of the three algorithms analyzed.

Uploaded by

Maxim Tincu
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)
735 views13 pages

UTM 2 Lab APA ORIGINAL

The document describes a laboratory report analyzing sorting algorithms. It implements Quicksort, Mergesort, and Bubblesort, calculating the number of iterations and runtime for each. Tables and graphs are constructed comparing the runtime and iterations. Quicksort is found to be the fastest algorithm, while Bubblesort is the slowest. The conclusion states Quicksort has more iterations but is still the quickest, and Bubblesort is the slowest of the three algorithms analyzed.

Uploaded by

Maxim Tincu
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/ 13

REPUBLIC OF MOLDOVA MINISTERUL EDUCATIEI

UNIVERSITATEA TEHNICA A MOLDOVEI


Catedra de Calculatoare

RAPORT
Lucrarea de Laborator 2
Analiza si Metode de Sortare

A efectuat:
A verificat :

gr. TI-142
lector superior

Chisinau 2015

Comanda Artur
Bagrin Veronica

Lucrarea de Laborator 2

Sarcina lucrarii:
1. De aplicat in cod algoritmele de sortare : Mergesort ,
Quicksort si Bubblesort.
2. De aflat numarul de iteratii la fiecare sortare.
3. De introdus timpul de executare in cod.
4. De construit tabel dupa timpul de executare si numarul de
iteratii.
5. De construit un graf dupa tabel.

Codul programului:
#define _CRT_SECURE_NO_WARNINGS
#include<conio.h>
#include<iostream>
#include<time.h>
#include <vector>
using namespace std;
int it;
const int n = 100;
int tab[n];
// Quicksort .
int partition(int InitialTablou[], int top, int bottom)
{
int x = InitialTablou[top];
int i = top - 1;
int j = bottom + 1;
int temp;
do
{
do
{
j--;
} while (x < InitialTablou[j]);
do
{

i++;
} while (x > InitialTablou[i]);

if (i < j)
{
temp = InitialTablou[i];
InitialTablou[i] = InitialTablou[j];
InitialTablou[j] = temp;
}
} while (i < j);
it = i + j;
return j;

Chisinau 2015

void quicksort(int InitialTablou[], int top, int bottom)


{
int middle;
if (top < bottom)
{
middle = partition(InitialTablou, top, bottom);
quicksort(InitialTablou, top, middle);
quicksort(InitialTablou, middle + 1, bottom);
}
}
void AfisareTablou(int InitialTablou[], int size)
{
int i;
for (i = 0; i < size; i++)
{
cout << InitialTablou[i] << ' ';
}
cout << "\n\n";
}

int main()
{
int initialTablou[1000];
int n = 50;
rand();
int marime = n;
for (int i = 0;i < marime; i++)
tab[i] = rand() % 1000 - 100;
cout << "Tabloul nesortat:" << endl;
AfisareTablou(tab, marime);
clock_t start, end;
start = clock();
quicksort(tab, 0,marime-1);
cout << endl;
cout << endl;
cout << "Tabloul sortat prin metoda quiksort este:" << endl;
AfisareTablou(tab, marime );

cout << endl;


printf("\nTimpul de executie al algoritmului Quicksort este:: %.8f
", ((double)(clock() - start)) / CLOCKS_PER_SEC);
end = clock();
cout << endl;
cout<<"Numarul de iteratii : " <<it<< endl;
_getch();

Chisinau 2015

return 0;

#define _CRT_SECURE_NO_WARNINGS
#include<conio.h>
#include<iostream>
#include<time.h>

using namespace std;


const int n = 100;
int tab[n];
int it;
//MergeSort
void MS( int aux1,int aux2,int aux3, int aux4)
{
int i, j;
for (j = aux1;j <= aux2;j++)
for (i = aux3;i <= aux4;i++)
if (tab[i]<tab[j])
{
tab[i] += tab[j];
tab[j] = tab[i] - tab[j];
tab[i] -= tab[j];
it = i + j;
}

}
void AF(int tab[], int size)
{
int i;

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


{
cout << tab[i] << ' ';
}

cout << "\n\n";


}
void insert(int i, int j)
{
if (tab[i]>tab[j])
{
tab[i] += tab[j];
tab[j] = tab[i] - tab[j];
tab[i] -= tab[j];
}
}

Chisinau 2015

int MergeSort(int i, int j)


{
if (j - i <= 1)insert(i, j);
else
{
MergeSort(i, (i + j) / 2);
MergeSort(1 + (i + j) / 2, j);
MS(i, (i + j) / 2, 1 + (i + j) / 2, j);
}
return tab;
}
int main()
{
int initialTablou[10000];
int n = 50, array[10000];
rand();
int marime = n;
for (int i = 0;i < marime; i++)
tab[i] = rand() % 1000 - 100;
{

clock_t start, end;\


start = clock();
for (int i = 0; i < n; i++)
MergeSort(0, n - 1);
start = clock();
cout << "Tabloul sortat prin metoda MergeSort este:" << endl;
MergeSort(0, n - 1);
AF(tab, marime);
cout << "Numarul de iteratii : "<<it << endl;

cout << endl;


printf("Timpul de executie al algoritmului MergeSort este:
%.8f\n", ((double)(clock() - start)) / CLOCKS_PER_SEC);
}

_getch();
return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include<conio.h>
#include<iostream>
#include<time.h>
#include <vector>

Chisinau 2015

using namespace std;


int it;
const int n = 100;
int tab[n];

void bubble_sort(int iarr[], int num) {


int i, j=0, k, temp;
bool swapped = true;
while (swapped) {
swapped = false;
j++;
for (i = 0; i < num-j; i++) {
if (iarr[i] > iarr[i + 1]) {
temp = iarr[i];
iarr[i] = iarr[i + 1];
iarr[i + 1] = temp;
swapped = true;
}
}
}
cout << endl;
it = ((i)*(j));

}
void AfisareTablou(int iarr[], int size)
{
int i;
for (i = 0; i < size; i++)
{
cout << iarr[i] << ' ';

}
cout << "\n\n";

int main()
{
int initialTablou[1000];
int n = 50;
rand();

Chisinau 2015

int marime = n;
for (int i = 0;i < marime; i++)
tab[i] = rand() % 1000 - 100;
cout << "Tabloul nesortat:" << endl;
AfisareTablou(tab, marime);
clock_t start, end;
start = clock();
bubble_sort(tab , marime );
cout << endl;
cout << endl;
cout << "Tabloul sortat prin metoda bubble sort este:" << endl;
AfisareTablou(tab, marime);

cout << endl;


printf("\nTimpul de executie al algoritmului bubble sort este:: %.8f ", ((double)
(clock() - start)) / CLOCKS_PER_SEC);
end = clock();
cout << endl;
cout << "Numarul de iteratii : " << it << endl;
_getch();

_getch();
return 0;
}

Rezultatul afisarii:

Metoda QuickSort :

N=10

Chisinau 2015

N=20

N=30

Chisinau 2015

N=40

N=50

Metoda MergeSort:
N=10

Chisinau 2015

N=20

N=30

Chisinau 2015

N=40

N=50

Metoda BubbleSort

Chisinau 2015

N=10

N=20

N=30

Chisinau 2015

N=40

N=50

Concluzie:
Efectund lucrarea dat am implementat algoritmi de
sortare, quicksort , mergesort si bubblesort, bazai pe
analizind timpul de executie al algoritmilor dai. Aceti
algoritmi au un timp de execuie diferit, de aceea este
important de a alege algoritmul cel mai eficient, adic
algoritmul care are un timp de execuie mic,se observa :
1.Din cele analizate se vede ca quicksort este un algoritm
mai rapid,la quick sort se efectueaza mai multe iteratii.
2.Dar se observa ca dupa analiza si executare cel mai incet
lucreaza sortarea dupa bubblesort.

Chisinau 2015

You might also like