Algoritmlarni loyihalash fani bo`yicha Labaratoriya ishi-3 Bajardi


Download 120.64 Kb.
bet1/2
Sana14.06.2020
Hajmi120.64 Kb.
#118715
  1   2
Bog'liq
CAL lab 3


O`ZBEKISTON RESPUBLIKASI AXBOROT TEXNOLOGIYALARI VA KOMMUNIKATSIYALARINI RIVOJLANTIRISH VAZIRLIGI MUHAMMAD AL-XORAZMIY NOMIDAGI TOSHKENT AXBOROT TEXNOLOGIYALARI UNIVERSITETI

kafedrasi


Algoritmlarni loyihalash fani bo`yicha

Labaratoriya ishi-3

Bajardi: CAL018-L2- guruh talabasi

Zokirov Farrux



Tekshirdi: Murayeva Hodisaxon

Laboratoriya ishi №3. Murakkab malumotlar tuzilmalari: Ustuvor navbatlar.

10,11,12

Tasodifiy elementlar bilan to’ldirilgan massiv

Surish

Sheyker

Bubble sort

500

1600

5000

#include

using namespace std;

//random number

void print( int x[], int n){

srand(time(0));

for (int i=0; i

x[i]=(rand()%200);

}

}



void swap( int *xp, int *yp)

{


int temp = *xp;

*xp = *yp;

*yp = temp;

}


// Bubble sort

void bubbleSort( int arr[], int m)

{

int c,d,swap;



time_t start,end;

double tc;

for(c=m;c>0;c--)

{

arr[c]=c+1;



}

start=clock();

for(c=0;c

{

for(d=0;d

{

if(arr[d]>arr[d+1])

{

swap=arr[d];



arr[d]=arr[d+1];

arr[d+1]=swap;

}

}

}



end=clock();

tc=(difftime(end,start)/CLOCKS_PER_SEC);

printf("%lf | ",tc);

}

void Swap(int *Mas, int i)



{

int temp;

temp=Mas[i];

Mas[i]=Mas[i-1];

Mas[i-1]=temp;

}

void ShakerSort(int Mas[], int Start, int N)



{

int Left, Right, i;

clock_t st, end;

st = clock();

Left=Start;

Right=N-1;

while (Left<=Right)

{

for (i=Right; i>=Left; i--)



if (Mas[i-1]>Mas[i]) Swap(Mas, i);

Left++;


for (i=Left; i<=Right; i++)

if (Mas[i-1]>Mas[i]) Swap(Mas, i);

Right--;

}

end = clock();



printf("%f | ", (double)(end - st) / CLOCKS_PER_SEC);

}

void quicksort(int l,int p,int *tab)



{

int i=l,j=p,x=tab[(l+p)/2],w;

do

{

while (tab[i]

{

i++;


}

while (x

{

j--;


}

if (i<=j)

{

w=tab[i];



tab[i]=tab[j];

tab[j]=w;

i++;

j--;


}

}

while (i<=j);



if (l

{

quicksort(l,j,tab);



}

if (i



{

quicksort(i,p,tab);

}

}

double seconds(size_t n) {



int *tab = (int *)malloc(sizeof(int) * (2*n - 1));

size_t i;

for (i = 0; i < n-1; i++) {

tab[i] = tab[2*n-i-2] = i+1;

}

tab[n-1] = n;



clock_t start = clock();

quicksort(0, 2*n-2, tab);

clock_t finish = clock();

free(tab);

return (double) (finish - start) / CLOCKS_PER_SEC;

}

int main()



{ int n=500, m=1600, p=5000;

int a[n],b[m], c[p];

cout<<"\n________________________________________________________________________________________\n";

cout<<"\nSaralash nomi: || n="<cout<<"Quick Sort || "<cout<<"Shaker Sort || "; ShakerSort(a,1,n); ShakerSort(b,1,m); ShakerSort(c,1,p); cout<

cout<<"Bubble Sort || "; bubbleSort(a,n); bubbleSort(b,m); bubbleSort(c,p);

return 0;



}



2 – topshiriq

C++ (Python, Java) tilida quyida keltirilgan amallarni bajargan holda ustuvor navbat tashkil qiling:



  • Berilgan N ta elementdan iborat ustuvor navbat hosil qiling;

  • Yangi element qo’shing;

  • Eng katta elementni yechib oling;

  • Berilgan qandaydir elementni yechib oling;

  • Ikkita ustuvor navbatni birlashtiring.

#include

#include

using namespace std;

void showpq(priority_queue gq)

{

priority_queue g = gq;



while (!g.empty())

{


cout<< g.top()<<' ';

g.pop();

}

cout << '\n';



}

int main()

{

priority_queue pqueue;



priority_queue pqueue2;

pqueue2.push(3);

pqueue2.push(5);

pqueue2.push(1);

pqueue2.push(7);

pqueue2.push(9);

pqueue2.push(3);

pqueue2.push(2);

int n;

cout<<"navbat elementlari soni kiriting: ";



cin>>n;

int a[n];

for (int i=0; i

cin>>a[i];

pqueue.push(a[i]);

}

cout<<"\nNavbatning berilishi: \n";



showpq(pqueue);

mark:


cout<<"\n\n____________MENYU____________ \n";

cout<<"1. Yangi element qo'shish \n";

cout<<"2. Eng katta elementni yechib olish \n";

cout<<"3. Berilgan qandaydir elementni yechib olish \n";

cout<<"4. Ikkita ustuvor navbatni birlashtiring \n";

int x;


cin>>x;

cout<

switch(x){

case 1:{

int b;

cout<<"biror bir son kiriting: ";



cin>>b;

pqueue.push(b);

showpq(pqueue);

break;


}

case 2:{

cout<<"Navbatdagi eng katta element: "<


pqueue.pop();

cout<<"Eng katta element yechib olingandan keyin: \n";

showpq(pqueue);

break;


}

case 3:{

pqueue.pop();

showpq(pqueue);

break;

}

case 4:{



cout<<"berilgan ikkinchi priority queue elementlari: \n";

showpq(pqueue2);

while (!pqueue2.empty())

{

pqueue.push(pqueue2.top());



pqueue2.pop();

}

cout<<"\nIkkita navbatning qo'shilgani: \n";



showpq(pqueue);

break;


}

default: cout<<"Mavjud bo'lmagan buyruqni tanladingiz";

}

cout<<"\nBosh menyuga qaytasizmi? (ha/yo'q)\n";



string s;

cin>>s;


if(s=="ha"){

goto mark;



}else{

exit;


}

}


Download 120.64 Kb.

Do'stlaringiz bilan baham:
  1   2




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling