SlideShare a Scribd company logo
1 of 11
Download to read offline
Universitatea de Stat din Republica Moldova
Facultatea de Matematica si Informatica
Specialitatea Informatica(ȘE)
Laboratorul 1
la SDMP.
Tema: Algoritmi de căutare.
Scris de:Moraru Igor,grupa I21
Verificat de: VERIF. LECTOR,MAGISTRU IN INFORM BITCOVSCHI LUCIA
Chișinău 2014
Considerați teoretice
Prin structura de date abstractă (SDA), sau pur şi simplu structura de date
(SD), vom înţelege o totalitate de reguli şi restricţii ce reflectă
legăturile existente între părţile separate de date şi prin care părţile
acestea se unesc într-un singur tot.
Sortarea este operaţia de aranjare a elementelor unui vector după valoarea
cheilor, pentru care este definită relaţia de ordine și datorită careea se
poate efectua mai rapid cautarea unui element.
Varianta 4.
Să se implementeze cîteva metode de sortare şi să se efectueze sortarea
după cîmpul
cheie din fişierul textual, iar pentru fiecare metodă de sortare să se
efectueze
următoarele:
- Să se analizeze complexitatea teoretică şi complexitatea practică a
metodelor de sortare
- De descris algoritmul metodei de sortare pe paşi.
LISTINGUL
#include <conio.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <dos.h>
//*********************abstract class ELEM
using namespace std;
class elem {
public:
virtual int fscanf_el(FILE * f)=0;
virtual int show(const char * opening, const char * ending)=0;
virtual int free()=0;
int operator >(elem &) {
error("Error should overide operator ">"!n");
return 0;
}
int operator <(elem &) {
error("Error should overide operator "<"!n");
return 0;
}
int operator >=(elem &) {
error("Error should overide operator ">="!n");
return 0;
}
int operator <=(elem &) {
error("Error should overide operator "<="!n");
return 0;
}
int operator ==(elem &) {
error("Error should overide operator "=="!n");
return 0;
}
int operator !=(elem &) {
error("Error should overide operator "!="!n");
return 0;
}
protected:
void error(char * message) {
cout << message;
cout << "press any key to fin....n";
getch();
exit(1);
}
};
//******************************class Carte
class Carte
{
public:
int id;
char Nume_A[25];
char Prenume[25];
char Titlu_C[30];
float pret;
Carte()
{
id=0;
Nume_A[0]='0';
Prenume[0]='0';
Titlu_C[0]='0';
pret=0.0;
};
void setFirma(char* f) {
strcpy(firma,f);
}
void setModel(char* n) {
strcpy(model, n);
}
void setRam(int a) {
ram=a;
}
void setHdd(int i) {
hdd = i;
}
void setOs(char* i) {
strcpy(os,i);
}
char* getFirma() {
return firma;
}
char* getModel() {
return model;
}
int getRam() {
return ram;
}
int getHdd() {
return hdd;
}
char* getOs() {
return os;
}
int fscanf_el(FILE * f) {
return fscanf(f,"%s%s %i %i %s
%f",firma,model,&ram,&hdd,os,&pret);
}
void virtual show() {
cout<<firma<<" "<<model<<" "<<ram<<" "<<hdd<<"
"<<os<<" "<<pret<<endl;
}
virtual int free()
{
return model[0]=='0';
}
int operator > (Product& e2)
{
return strcmp(model, e2.model)>0 ? 1 : 0;
}
int operator < (Product& e2)
{
return strcmp(model, e2.model)<0 ? 1 : 0;
}
/*
int operator <=(Product &e2) {
return (this->code <= e2.code);
}
int operator >=(Product &e2) {
return (this->code >= e2.code);
}
*/
int operator ==(Product &e2) {
return strcmp(model, e2.model)==0 ? 1 : 0;
}
int operator !=(Product &e2) {
return strcmp(model, e2.model)!=0 ? 1 : 0;
}
};
template<class el> class tabel {
protected:
int n;
el t[50];
public:
tabel() {
n = 0;
}
tabel(char * file);
void sort();
void shell_sort();
void quickSort(int l, int r);
void selectsort(int l, int r);
void insertsort(int i, int j);
void downheap(long k, long n);
void heapsort(int l, int r);
void show();
protected:
void error(char *message) {
cout << message;
cout << "press any key to fin....n";
getch();
exit(1);
}
};
template<class el>
tabel<el>::tabel(char * file) {
FILE *pf;
pf = fopen(file, "rt");
n = 0;
while (!feof(pf))
if (t[n].fscanf_el(pf) > 0)
n++;
fclose(pf);
}
template<class el> void tabel<el>::show() {
for (int i = 0; i < n; i++) {
if (i % 20 == 0 && i > 0) {
cout << "nNajmite liubuiu klavishu dlea prodoljenian";
getch();
}
cout << (i + 1) << ". ";
t[i].show();
}
}
template<class el>
void tabel<el>::sort() {
int j, count = 0;
el aux;
for (int i = 0; i < n - 1; i++)
for (j = i; j < n; j++) {
if (strcmp(t[i].getModel(),t[j].getModel())>0)
{
aux = t[i];
t[i] = t[j];
t[j] = aux;
count++;
}
}
cout << "Slojnosti sortirovki BUBBLESORT= " << n * n << endl;
getch();
cout << "Koli4estvo perestanovok i sravnenii= " << j << endl;
}
template<class el>
void tabel<el>::selectsort(int l, int r) {
el tmp;
// struct time t1, t2;
int contor;
for (int i = 0; i < r; ++i) // i - nomer tekuschego shaga
{
int pos = i;
tmp = t[i];
for (int j = i + 1; j < r; ++j) // cikl vybora naimen'shego
elementa
{
if (t[j] < tmp) {
pos = j;
tmp = t[j];
}
}
t[pos] = t[i];
t[i] = tmp; // menyaem mestami naimen'shiy s a[i]
contor=i-1;
}
cout << "nSlojnosti sortirovki SELECTSORT= " << ((r * (r - 1)) / 2)
<< endl;
getch();
cout << "Koli4estvo perestanovok i sravnenii= " << (contor) << endl;
getch();
}
template<class el>
void tabel<el>::insertsort(int l, int r) {
el temp;
for (int i = 1, j; i < r; ++i) //cikl prohodov, i - nomer prohoda
{
temp = t[i];
for (j = i - 1; j >= 0 && t[j] > temp; --j) // poisk mesta elementa
v gotovoy posledovatel'nosti
t[j + 1] = t[j]; // sdvigaem element napravo, poka ne doshli
t[j + 1] = temp; // mesto naydeno, vstavit' element
}
cout << "nSlojnosti sortirovki INSERTSORT= " << ((r * (r - 1)) / 4)
<< endl;
getch();
}
template<class el>
void tabel<el>::quickSort(int l, int r)
{
el temp;
int i,x,j = r;i=l;
x=(i+j)/2;
while (i <= j){
///while (strcmp(t[i].getModel(), t[x])>0)
if(strcmp(t[x].getModel(),this->t[i].getModel())<0)
{ i++;}
//while (t[j].getModel() > x)
if (strcmp(t[x].getModel(),this->t[j].getModel())>0)
{ j--;}
if (i <= j) {
temp = t[i];
t[i] = t[j];
t[j] = temp;
i++;
j--;
}
}
if (i < r)
quickSort(i, r);
if (l < j)
quickSort(l, j);
}
template<class el>
void tabel<el>::downheap(long k, long n) {
// procedura proseivaniya sleduyuschego elementa
// Do procedury: a[k+1]...a[n] - piramida
// Posle: a[k]...a[n] - piramida
el new_elem;
long child;
new_elem = t[k];
while (k <= n / 2) // poka u a[k] est' deti
{
child = 2 * k;
if (child < n && t[child] < t[child + 1]) // vybiraem bol'shego
syna
child++;
if (new_elem > t[child])
break;
// inache
t[k] = t[child]; // perenosim syna naverh
k = child;
}
t[k] = new_elem;
}
template<class el>
void tabel<el>::heapsort(int l, int r) {
long i;
el temp;
// stroim piramidu
for (i = r / 2 - 1; i >= 0; --i)
downheap(i, r - 1);
// teper' a[0]...a[size-1] piramida
for (i = r - 1; i > 0; --i) {
// menyaem pervyy s poslednim
temp = t[i];
t[i] = t[0];
t[0] = temp;
// vosstanavlivaem piramidal'nost' a[0]...a[i-1]
downheap(0, i - 1);
}
cout << "nSlojnosti sortirovki HEAPSORT= " << (r * (log(r) /
log(2.0)))
<< endl;
getch();
cout << "Koli4estvo perestanovok i sravnenii= " << (r - 1) * 2 <<
endl;
getch();
}
int main() {
tabel<Product> gr("D:b.txt");
// struct time t1, t2;
Product pl;
char ch = 'n';
int id, i,r=50;
while (ch != 'y') {
cout << "n Soderjimoe faila: n";
gr.show();
cout << "nnt ~ ~Rezulitat sortirovki ~ ~" << endl;
getch();
// gettime(&t1);
// delay(50);
//gr.selectsort(0, 50);
//gr.insertsort(0,50);
/*
gr.quickSort(0, 50);
cout<<"nSlojnosti sortirovki QUICKSORT= "<<
(r*(log(r)/log(2.0))) <<endl;
getch();
cout<<"Koli4estvo perestanovok i sravnenii= "<<r<<endl;
getch();
*/
gr.heapsort(0,50);
//gr.sort();
// gettime(&t2);
// cout << "Vremea sortirovki: " << t2.ti_sec - t1.ti_sec <<
"secund "
// << t2.ti_hund - t1.ti_hund << "ms" << endl;
getch();
gr.show();
cout << "n Zavershiti? (Y/N) :";
ch = getch();
cout << endl;
}
}
Rezultatul:

More Related Content

What's hot

Bai tap java
Bai tap javaBai tap java
Bai tap javaMan Bat
 
Cours de C++, en français, 2002 - Cours 1.3
Cours de C++, en français, 2002 - Cours 1.3Cours de C++, en français, 2002 - Cours 1.3
Cours de C++, en français, 2002 - Cours 1.3Laurent BUNIET
 
Шаблоны проектирования 2
Шаблоны проектирования 2Шаблоны проектирования 2
Шаблоны проектирования 2Constantin Kichinsky
 
EJEMPLOS DESARROLLADOS
EJEMPLOS DESARROLLADOSEJEMPLOS DESARROLLADOS
EJEMPLOS DESARROLLADOSDarwin Durand
 
Java весна 2013 лекция 6
Java весна 2013 лекция 6Java весна 2013 лекция 6
Java весна 2013 лекция 6Technopark
 
Toan pt.de021.2011
Toan pt.de021.2011Toan pt.de021.2011
Toan pt.de021.2011BẢO Hí
 
Toan pt.de022.2011
Toan pt.de022.2011Toan pt.de022.2011
Toan pt.de022.2011BẢO Hí
 
Cg lab cse-vii
Cg lab cse-viiCg lab cse-vii
Cg lab cse-viisajjan93
 
Writeup ctf online idsecconf 2017
Writeup ctf online idsecconf 2017Writeup ctf online idsecconf 2017
Writeup ctf online idsecconf 2017idsecconf
 

What's hot (13)

Bai tap java
Bai tap javaBai tap java
Bai tap java
 
Cours de C++, en français, 2002 - Cours 1.3
Cours de C++, en français, 2002 - Cours 1.3Cours de C++, en français, 2002 - Cours 1.3
Cours de C++, en français, 2002 - Cours 1.3
 
Шаблоны проектирования 2
Шаблоны проектирования 2Шаблоны проектирования 2
Шаблоны проектирования 2
 
EJEMPLOS DESARROLLADOS
EJEMPLOS DESARROLLADOSEJEMPLOS DESARROLLADOS
EJEMPLOS DESARROLLADOS
 
Rut
RutRut
Rut
 
Java весна 2013 лекция 6
Java весна 2013 лекция 6Java весна 2013 лекция 6
Java весна 2013 лекция 6
 
Programs
ProgramsPrograms
Programs
 
20150415 csharp6.0
20150415 csharp6.020150415 csharp6.0
20150415 csharp6.0
 
Toan pt.de021.2011
Toan pt.de021.2011Toan pt.de021.2011
Toan pt.de021.2011
 
Toan pt.de022.2011
Toan pt.de022.2011Toan pt.de022.2011
Toan pt.de022.2011
 
Cg lab cse-vii
Cg lab cse-viiCg lab cse-vii
Cg lab cse-vii
 
Writeup ctf online idsecconf 2017
Writeup ctf online idsecconf 2017Writeup ctf online idsecconf 2017
Writeup ctf online idsecconf 2017
 
Sis quiz
Sis quizSis quiz
Sis quiz
 

Viewers also liked

Accessing soft layer
Accessing soft layerAccessing soft layer
Accessing soft layercarlito dizon
 
Idol power point
Idol power pointIdol power point
Idol power pointjsrcc
 
Statistical process control training
Statistical process control trainingStatistical process control training
Statistical process control trainingbilal183
 
Supply chain of Dabba Walas
Supply chain of Dabba WalasSupply chain of Dabba Walas
Supply chain of Dabba Walaskevin Richard
 
Negotiable Instruments Act 1881
Negotiable Instruments Act 1881Negotiable Instruments Act 1881
Negotiable Instruments Act 1881kevin Richard
 
Statistical process control technique with example - xbar chart and R chart
Statistical process control technique with example - xbar chart and R chartStatistical process control technique with example - xbar chart and R chart
Statistical process control technique with example - xbar chart and R chartkevin Richard
 
Indian contract act - Communication of offer and acceptance
Indian contract act - Communication of offer and acceptanceIndian contract act - Communication of offer and acceptance
Indian contract act - Communication of offer and acceptancekevin Richard
 
TRIPs agreement - WTO
TRIPs agreement - WTOTRIPs agreement - WTO
TRIPs agreement - WTOkevin Richard
 

Viewers also liked (16)

Accessing soft layer
Accessing soft layerAccessing soft layer
Accessing soft layer
 
Idol power point
Idol power pointIdol power point
Idol power point
 
Tema 7
Tema 7Tema 7
Tema 7
 
Ratio analiz
Ratio analizRatio analiz
Ratio analiz
 
Tema 3
Tema 3Tema 3
Tema 3
 
Tema 6
Tema 6Tema 6
Tema 6
 
Statistical process control training
Statistical process control trainingStatistical process control training
Statistical process control training
 
Tema 4
Tema 4Tema 4
Tema 4
 
Services marketing
Services marketingServices marketing
Services marketing
 
Supply chain of Dabba Walas
Supply chain of Dabba WalasSupply chain of Dabba Walas
Supply chain of Dabba Walas
 
Negotiable Instruments Act 1881
Negotiable Instruments Act 1881Negotiable Instruments Act 1881
Negotiable Instruments Act 1881
 
Statistical process control technique with example - xbar chart and R chart
Statistical process control technique with example - xbar chart and R chartStatistical process control technique with example - xbar chart and R chart
Statistical process control technique with example - xbar chart and R chart
 
ANG ALAMAT NG MAYA
ANG ALAMAT NG MAYA ANG ALAMAT NG MAYA
ANG ALAMAT NG MAYA
 
Indian contract act - Communication of offer and acceptance
Indian contract act - Communication of offer and acceptanceIndian contract act - Communication of offer and acceptance
Indian contract act - Communication of offer and acceptance
 
Si PAGONG (PABULA)
Si PAGONG (PABULA)Si PAGONG (PABULA)
Si PAGONG (PABULA)
 
TRIPs agreement - WTO
TRIPs agreement - WTOTRIPs agreement - WTO
TRIPs agreement - WTO
 

Lab2 sdmp

  • 1. Universitatea de Stat din Republica Moldova Facultatea de Matematica si Informatica Specialitatea Informatica(ȘE) Laboratorul 1 la SDMP. Tema: Algoritmi de căutare. Scris de:Moraru Igor,grupa I21 Verificat de: VERIF. LECTOR,MAGISTRU IN INFORM BITCOVSCHI LUCIA Chișinău 2014
  • 2. Considerați teoretice Prin structura de date abstractă (SDA), sau pur şi simplu structura de date (SD), vom înţelege o totalitate de reguli şi restricţii ce reflectă legăturile existente între părţile separate de date şi prin care părţile acestea se unesc într-un singur tot. Sortarea este operaţia de aranjare a elementelor unui vector după valoarea cheilor, pentru care este definită relaţia de ordine și datorită careea se poate efectua mai rapid cautarea unui element. Varianta 4. Să se implementeze cîteva metode de sortare şi să se efectueze sortarea după cîmpul cheie din fişierul textual, iar pentru fiecare metodă de sortare să se efectueze următoarele: - Să se analizeze complexitatea teoretică şi complexitatea practică a metodelor de sortare - De descris algoritmul metodei de sortare pe paşi. LISTINGUL #include <conio.h> #include <stdio.h> #include <iostream> #include <string.h> #include <math.h> #include <stdlib.h> #include <time.h> #include <dos.h> //*********************abstract class ELEM using namespace std; class elem { public: virtual int fscanf_el(FILE * f)=0; virtual int show(const char * opening, const char * ending)=0; virtual int free()=0; int operator >(elem &) { error("Error should overide operator ">"!n");
  • 3. return 0; } int operator <(elem &) { error("Error should overide operator "<"!n"); return 0; } int operator >=(elem &) { error("Error should overide operator ">="!n"); return 0; } int operator <=(elem &) { error("Error should overide operator "<="!n"); return 0; } int operator ==(elem &) { error("Error should overide operator "=="!n"); return 0; } int operator !=(elem &) { error("Error should overide operator "!="!n"); return 0; } protected: void error(char * message) { cout << message; cout << "press any key to fin....n"; getch(); exit(1); } }; //******************************class Carte class Carte { public: int id; char Nume_A[25]; char Prenume[25]; char Titlu_C[30]; float pret; Carte() {
  • 4. id=0; Nume_A[0]='0'; Prenume[0]='0'; Titlu_C[0]='0'; pret=0.0; }; void setFirma(char* f) { strcpy(firma,f); } void setModel(char* n) { strcpy(model, n); } void setRam(int a) { ram=a; } void setHdd(int i) { hdd = i; } void setOs(char* i) { strcpy(os,i); } char* getFirma() { return firma; } char* getModel() { return model; } int getRam() { return ram; } int getHdd() { return hdd; } char* getOs() { return os; } int fscanf_el(FILE * f) { return fscanf(f,"%s%s %i %i %s %f",firma,model,&ram,&hdd,os,&pret); } void virtual show() {
  • 5. cout<<firma<<" "<<model<<" "<<ram<<" "<<hdd<<" "<<os<<" "<<pret<<endl; } virtual int free() { return model[0]=='0'; } int operator > (Product& e2) { return strcmp(model, e2.model)>0 ? 1 : 0; } int operator < (Product& e2) { return strcmp(model, e2.model)<0 ? 1 : 0; } /* int operator <=(Product &e2) { return (this->code <= e2.code); } int operator >=(Product &e2) { return (this->code >= e2.code); } */ int operator ==(Product &e2) { return strcmp(model, e2.model)==0 ? 1 : 0; } int operator !=(Product &e2) { return strcmp(model, e2.model)!=0 ? 1 : 0; } }; template<class el> class tabel { protected: int n; el t[50]; public: tabel() { n = 0; } tabel(char * file); void sort();
  • 6. void shell_sort(); void quickSort(int l, int r); void selectsort(int l, int r); void insertsort(int i, int j); void downheap(long k, long n); void heapsort(int l, int r); void show(); protected: void error(char *message) { cout << message; cout << "press any key to fin....n"; getch(); exit(1); } }; template<class el> tabel<el>::tabel(char * file) { FILE *pf; pf = fopen(file, "rt"); n = 0; while (!feof(pf)) if (t[n].fscanf_el(pf) > 0) n++; fclose(pf); } template<class el> void tabel<el>::show() { for (int i = 0; i < n; i++) { if (i % 20 == 0 && i > 0) { cout << "nNajmite liubuiu klavishu dlea prodoljenian"; getch(); } cout << (i + 1) << ". "; t[i].show(); } } template<class el> void tabel<el>::sort() { int j, count = 0;
  • 7. el aux; for (int i = 0; i < n - 1; i++) for (j = i; j < n; j++) { if (strcmp(t[i].getModel(),t[j].getModel())>0) { aux = t[i]; t[i] = t[j]; t[j] = aux; count++; } } cout << "Slojnosti sortirovki BUBBLESORT= " << n * n << endl; getch(); cout << "Koli4estvo perestanovok i sravnenii= " << j << endl; } template<class el> void tabel<el>::selectsort(int l, int r) { el tmp; // struct time t1, t2; int contor; for (int i = 0; i < r; ++i) // i - nomer tekuschego shaga { int pos = i; tmp = t[i]; for (int j = i + 1; j < r; ++j) // cikl vybora naimen'shego elementa { if (t[j] < tmp) { pos = j; tmp = t[j]; } } t[pos] = t[i]; t[i] = tmp; // menyaem mestami naimen'shiy s a[i] contor=i-1; } cout << "nSlojnosti sortirovki SELECTSORT= " << ((r * (r - 1)) / 2) << endl; getch(); cout << "Koli4estvo perestanovok i sravnenii= " << (contor) << endl; getch();
  • 8. } template<class el> void tabel<el>::insertsort(int l, int r) { el temp; for (int i = 1, j; i < r; ++i) //cikl prohodov, i - nomer prohoda { temp = t[i]; for (j = i - 1; j >= 0 && t[j] > temp; --j) // poisk mesta elementa v gotovoy posledovatel'nosti t[j + 1] = t[j]; // sdvigaem element napravo, poka ne doshli t[j + 1] = temp; // mesto naydeno, vstavit' element } cout << "nSlojnosti sortirovki INSERTSORT= " << ((r * (r - 1)) / 4) << endl; getch(); } template<class el> void tabel<el>::quickSort(int l, int r) { el temp; int i,x,j = r;i=l; x=(i+j)/2; while (i <= j){ ///while (strcmp(t[i].getModel(), t[x])>0) if(strcmp(t[x].getModel(),this->t[i].getModel())<0) { i++;} //while (t[j].getModel() > x) if (strcmp(t[x].getModel(),this->t[j].getModel())>0) { j--;} if (i <= j) { temp = t[i]; t[i] = t[j]; t[j] = temp; i++; j--; } } if (i < r)
  • 9. quickSort(i, r); if (l < j) quickSort(l, j); } template<class el> void tabel<el>::downheap(long k, long n) { // procedura proseivaniya sleduyuschego elementa // Do procedury: a[k+1]...a[n] - piramida // Posle: a[k]...a[n] - piramida el new_elem; long child; new_elem = t[k]; while (k <= n / 2) // poka u a[k] est' deti { child = 2 * k; if (child < n && t[child] < t[child + 1]) // vybiraem bol'shego syna child++; if (new_elem > t[child]) break; // inache t[k] = t[child]; // perenosim syna naverh k = child; } t[k] = new_elem; } template<class el> void tabel<el>::heapsort(int l, int r) { long i; el temp; // stroim piramidu for (i = r / 2 - 1; i >= 0; --i) downheap(i, r - 1); // teper' a[0]...a[size-1] piramida for (i = r - 1; i > 0; --i) {
  • 10. // menyaem pervyy s poslednim temp = t[i]; t[i] = t[0]; t[0] = temp; // vosstanavlivaem piramidal'nost' a[0]...a[i-1] downheap(0, i - 1); } cout << "nSlojnosti sortirovki HEAPSORT= " << (r * (log(r) / log(2.0))) << endl; getch(); cout << "Koli4estvo perestanovok i sravnenii= " << (r - 1) * 2 << endl; getch(); } int main() { tabel<Product> gr("D:b.txt"); // struct time t1, t2; Product pl; char ch = 'n'; int id, i,r=50; while (ch != 'y') { cout << "n Soderjimoe faila: n"; gr.show(); cout << "nnt ~ ~Rezulitat sortirovki ~ ~" << endl; getch(); // gettime(&t1); // delay(50); //gr.selectsort(0, 50); //gr.insertsort(0,50); /* gr.quickSort(0, 50); cout<<"nSlojnosti sortirovki QUICKSORT= "<< (r*(log(r)/log(2.0))) <<endl; getch(); cout<<"Koli4estvo perestanovok i sravnenii= "<<r<<endl; getch(); */ gr.heapsort(0,50); //gr.sort(); // gettime(&t2);
  • 11. // cout << "Vremea sortirovki: " << t2.ti_sec - t1.ti_sec << "secund " // << t2.ti_hund - t1.ti_hund << "ms" << endl; getch(); gr.show(); cout << "n Zavershiti? (Y/N) :"; ch = getch(); cout << endl; } } Rezultatul: