Lab2 sdmp
Click here to load reader
-
Upload
igor-moraru -
Category
Internet
-
view
58 -
download
1
Transcript of Lab2 sdmp
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 prodoljenia\n";
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 << "\n\n\t ~ ~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: