You are on page 1of 11

Dr.

Agni Dika

Analiza e algoritmeve
Nj problem i caktuar mund t zgjidhet duke shfrytzuar algoritme t
ndryshme. Por, gjat ksaj shtrohet pyetja se cila sht zgjidhja optimale e
problemit, cili sht algorimi m i mir dhe si prcaktohet ai? Duhet patur kujdes
kur zgjedhet algoritmi m i mir sepse kjo sht dika relative. Ajo ka llogaritet
si opcion optimal pr nj algoritm nuk do t thot se vlen gjat zgjidhjes s
problemeve t ndryshm. Prandaj, gjithnj duhet t pyetemi se ka sht m e
mir pr zgjidhje t nj problemi t caktuar, a sht shpejtsia e ekzekutimit apo
hasira memoruese q nevojitet pr ekzekutim t tij.
Koha e ekzekutimit varet nga:

Shpejtsia e kompjuterit,
Gjuha programuese e cila prdoret,
T dhnat q sortohen dhe si ato jan organizuar,
Efikasiteti i kompajlerit gjat krijimit t programit ekzekutiv etj.

Sortimet
1. Selection sort - vers. 1
// Selection sort1
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
void Vektori(int A[],int b);
void SelectionSort1(int Z[],int n);
int const maxi=100000;
int main()
{
double Koha;
int B[maxi];
int Z[10]={10000,15000,25000,30000,45000,50000,65000,75000,90000, 100000};
// int Z[10]={100,150,250,300,450,500,650,750,900,1000};
int i,m;
clock_t ta,tb;
srand(time( NULL));
cout << "\nKonstanta CLOCKS_PER_SEC: "
<< CLOCKS_PER_SEC << "\n\n";
// Konstante me vlere 1000.
cout << "\nRezultatet pr Selection Sort1:\n\n";
cout << " Nr
Antar
Koha(s)\n";
cout << "---------------------------\n";
for(i=0; i<10;i++)
{
m = Z[i];
Vektori(B, m );
ta = clock();
SelectionSort1(B, m );
tb = clock();
Koha=(tb - ta)/(double)CLOCKS_PER_SEC;

cout <<
<<
<<
<<

setw(3) << i
setw(11) << m
setw(12) << fixed << setprecision(4) << Koha
endl;

}
cout << "---------------------------\n\n";
cout << "Kujdes, tabela me vlera ndryshon gjat cdo riekzekutimi\n";
cout << "Kur vektori ka pak elemente, kolona e fundit nuk do te thot se sht rrit
se\n";
cout << "Kjo ndrlidhet me ate se sa jan t pasortuar elementet\n\n";
return 0;
}
void Vektori(int A[],int b )
{
for (int i=0;i<b;i++)
A[i]=rand();
}
void SelectionSort1(int Z[],int n)
{
int i,j,x;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(Z[i]>Z[j])
{
x=Z[i];
Z[i]=Z[j];
Z[j]=x;
}
}
}
}

2. Selection Sort - vers. 2


// Selection Sort - Vers. 2
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
void Vektori(int A[],int b);
void SelectionSort2(int Z[],int n);
int const maxi=100000;

int main()
{
double Koha;
int B[maxi];
int Z[10]={10000,15000,25000,30000,45000,50000,65000,75000,90000, 100000};
// int Z[10]={100,150,250,300,450,500,650,750,900,1000};
int i,m;
clock_t ta,tb;
srand(time( NULL));
cout << "\nRezultatet pr Selection Sort2:\n\n";
cout << " Nr
Antar
Koha(s)\n";
cout << "---------------------------\n";
for(i=0; i<10;i++)
{
m = Z[i];
Vektori(B, m );
ta = clock();
SelectionSort2(B, m );
tb = clock();
Koha=(tb - ta)/(double)CLOCKS_PER_SEC;
cout << setw(3) << i
<< setw(11) << m
<< setw(12) << fixed << setprecision(4) << Koha
<< endl;
}
cout << "---------------------------\n\n";
cout << "Kujdes, tabela me vlera ndryshon gjat cdo riekzekutimi\n";
cout << "Kur vektori ka pak elemente, kolona e fundit nuk do te thot se sht rrit
se\n";
cout << "Kjo ndrlidhet me ate se sa jan t pasortuar elementet\n\n";
return 0;
}
void Vektori(int A[],int b )
{
for (int i=0;i<b;i++)
A[i]=rand();
}
void SelectionSort2(int Z[],int n)
{
int i,j,k,x;
for (i =0;i < (n-1); i++)
{
k=i;
for (j=i+1;j<n;j++)
{
if (Z[j]<Z[k])
k=j;
x=Z[i];
Z[i]=Z[k];
Z[k]=x;
}
}
}

3. Bubble Sort
// Bubble Sort
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
void Vektori(int A[],int b);
void BubbleSort(int Z[],int n);
int const maxi=100000;
int main()
{
double Koha;
int B[maxi];
int Z[10]={10000,15000,25000,30000,45000,50000,65000,75000,90000, 100000};
// int Z[10]={100,150,250,300,450,500,650,750,900,1000};
int i,m;
clock_t ta,tb;
srand(time( NULL));
cout << "\nRezultatet pr Bubble Sort:\n\n";
cout << " Nr
Antar
Koha(s)\n";
cout << "---------------------------\n";
for(i=0; i<10;i++)
{
m = Z[i];
Vektori(B, m );
ta = clock();
BubbleSort(B, m );
tb = clock();
Koha=(tb - ta)/(double)CLOCKS_PER_SEC;
cout << setw(3) << i
<< setw(11) << m
<< setw(12) << fixed << setprecision(4) << Koha
<< endl;
//
for (j=0;j<m;j++)
//
cout << B[j] << " ";
//
int j;
//
cin >> j;
}
cout << "---------------------------\n\n";
cout << "Kujdes, tabela me vlera ndryshon gjat cdo riekzekutimi\n";
cout << "Kur vektori ka pak elemente, kolona e fundit nuk do te thot se sht rrit
se\n";
cout << "Kjo ndrlidhet me ate se sa jan t pasortuar elementet\n\n";
return 0;
}
void Vektori(int A[],int b )
{
for (int i=0;i<b;i++)
A[i]=rand();
}
void BubbleSort(int Z[],int n)
{
bool fundi=false;
int i,j,x;
for (i =(n - 1);i >=0; i--)
{
if (fundi)
break;
fundi=true;

for (j=1;j<=i;j++)
{
if (Z[j-1]>Z[j])
{
fundi=false;
x=Z[j-1];
Z[j-1]=Z[j];
Z[j]=x;
}
}
}
}

Grafiku tregon se te Bubble Sort kemi t bjm me nj funkcion t tipit n 2 .


4. Insertion Sort - vers. 1
// Insertion Sort1
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
void Vektori(int A[],int b);
void InsertionSort1(int Z[],int n);
int const maxi=100000;
int main()

{
double Koha;
int B[maxi];
// int Z[10]={10000,15000,25000,30000,45000,50000,65000,75000,90000, 100000};
int Z[10]={100,150,250,300,450,500,650,750,900,1000};
int i,m;
clock_t ta,tb;
srand(time( NULL));
cout << "\nRezultatet pr Insertion Sort1:\n\n";
cout << " Nr
Antar
Koha(s)\n";
cout << "---------------------------\n";
for(i=0; i<10;i++)
{
m = Z[i];
Vektori(B, m );
ta = clock();
InsertionSort1(B, m );
tb = clock();
Koha=(tb - ta)/(double)CLOCKS_PER_SEC;
cout << setw(3) << i
<< setw(11) << m
<< setw(12) << fixed << setprecision(4) << Koha
<< endl;
}
cout << "---------------------------\n\n";
cout << "Kujdes, tabela me vlera ndryshon gjat cdo riekzekutimi\n";
cout << "Kur vektori ka pak elemente, kolona e fundit nuk do te thot se sht rrit
se\n";
cout << "Kjo ndrlidhet me ate se sa jan t pasortuar elementet\n\n";
return 0;
}
void Vektori(int A[],int b )
{
for (int i=0;i<b;i++)
A[i]=rand();
}
void InsertionSort1(int Z[],int n)
{
bool fundi=false;
int i,j,x;
for (i =1; i<n; ++i)
{
if (Z[i]<Z[i-1])
{
x=Z[i];
j=i;
do
{
Z[j]=Z[j-1];
j--;
}
while((Z[j-1]>x) && (j>0));
Z[j]=x;
}
}
}

5. Insertion Sort - vers. 2


// Insertion sort2
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
void Vektori(int A[],int b);
void InsertionSort2(int Z[],int n);
int const maxi=100000;
int main()
{
double Koha;
int B[maxi];
int Z[10]={10000,15000,25000,30000,45000,50000,65000,75000,90000, 100000};
// int Z[10]={100,150,250,300,450,500,650,750,900,1000};
int i,m;
clock_t ta,tb;
srand(time( NULL));
cout << "\nKonstanta CLOCKS_PER_SEC: "
<< CLOCKS_PER_SEC << "\n\n";
// Konstante me vlere 1000.
cout << "\nRezultatet pr Insertion Sort2:\n\n";
cout << " Nr
Antar
Koha(s)\n";
cout << "---------------------------\n";
for(i=0; i<10;i++)
{
m = Z[i];
Vektori(B, m );
ta = clock();
InsertionSort2(B, m );
tb = clock();
Koha=(tb - ta)/(double)CLOCKS_PER_SEC;
cout << setw(3) << i
<< setw(11) << m
<< setw(12) << fixed << setprecision(4) << Koha
<< endl;
}
cout << "---------------------------\n\n";
cout << "Kujdes, tabela me vlera ndryshon gjat cdo riekzekutimi\n";
cout << "Kur vektori ka pak elemente, kolona e fundit nuk do te thot se sht rrit
se\n";
cout << "Kjo ndrlidhet me ate se sa jan t pasortuar elementet\n\n";
return 0;
}
void Vektori(int A[],int b )
{
for (int i=0;i<b;i++)
A[i]=rand();
}
void InsertionSort2(int Z[],int n)
{
int i,j,x;
for(i=0;i<n;i++)
{
x=Z[i];
j=i;
while(Z[j-1]>x)

{
Z[j]=Z[j-1];
j=j-1;
if (j<=0)
break;
}
}
}

Pr krahasim t rezultateve, m mir do t ishte q numrat e gjeneruar pr antart e vektorit n t gjitha metodat t merren
t njjt (pa i inicuar me srand(time( NULL))).
------------------------------------------------------------------------------------------------------------------------------------------------------------------

Funkcione pr sortim si templejt


1.
// Selection sort
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
void Vektori(int A[],int b);
template <typename T>
void SelectionSort(T Z[],int n);
int const m=100;
int main()
{
int B[m];
int i;
srand(time( NULL));
Vektori(B, m );
SelectionSort(B, m );
for (i=0;i<m;i++)
cout << setw(6) << B[i] << "
cout << endl;

";

return 0;
}
void Vektori(int A[],int b )
{
for (int i=0;i<b;i++)
A[i]=rand();
}
template <typename T>
void SelectionSort(T Z[],int n)
{
T x;
int i,j;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(Z[i]>Z[j])
{
x=Z[i];
Z[i]=Z[j];
Z[j]=x;
}
}
}

----------------------------------------------------------------------------------------------------------2.
// Insertion sort
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
void Vektori(int A[],int b);
template <typename T>
void InsertionSort(T Z[],int n);
int const m=100;
int main()
{
int B[m];
int i;
srand(time( NULL));
Vektori(B, m );
InsertionSort(B, m );
for (i=0;i<m;i++)
cout << setw(6) << B[i] << "
cout << endl;
return 0;
}

";

void Vektori(int A[],int b )


{
for (int i=0;i<b;i++)
A[i]=rand();
}
template <typename T>
void InsertionSort(T A[],int n)
{
T v;
for (int i=1;i<n;++i)
{
if (A[i]<A[i-1])
{
v=A[i];
int j=i;
do
{

A[j]=A[j-1];
--j;

}
while ((j>0) && (v<A[j-1]));
A[j]=v;
}

-----------------------------------------------------------------------------------------------------------3.
// Bubble sort
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
void Vektori(int A[],int b);
template <typename T>
void BubbleSort(T Z[],int n);
int const m=100;
int main()
{
int B[m];
int i;
srand(time( NULL));
Vektori(B, m );
BubbleSort(B, m );
for (i=0;i<m;i++)
cout << setw(6) << B[i] << "
cout << endl;
return 0;
}

";

void Vektori(int A[],int b )


{
for (int i=0;i<b;i++)
A[i]=rand();
}
template <typename T>
void BubbleSort(T Z[],int n)
{
bool fundi=false;
int i,j,x;
for (i =(n - 1);i >=0; i--)
{
if (fundi)
break;
fundi=true;
for (j=1;j<=i;j++)
{
if (Z[j-1]>Z[j])
{
fundi=false;
x=Z[j-1];
Z[j-1]=Z[j];
Z[j]=x;
}
}
}
}

------------------------------------------------------------------------------------------------------------

You might also like