Yangi bo’limimiz dasturlashda eng muhim mavzulardan bo’lgan saralash mavzusiga bag’ishlanadi. Biror bir ma’lumotni saralash yoki qandaydir qolipga solish juda ham muhim


Download 0.77 Mb.
bet2/2
Sana15.03.2023
Hajmi0.77 Mb.
#1270814
1   2
Bog'liq
MTA Mustaqil ish

Misol:

#include

using namespace std;

int a[100001];

void qsort(int L, int R) {

int m = (L+R) / 2;

int X = a[m];

int i = L;

int j = R;

while (i <= j) {

while (a[i] < X) i++;

while (a[j] > X) j--;

if (i <= j) {

int t = a[i]; a[i] = a[j]; a[j] = t;
i++;

j--;
}


}


if (L < j) qsort(L, j);

if (i < R) qsort(i, R);
}

int main() {


int n;
cin>>n;

for (int i = 0; i < n; i++)
cin>>a[i];
qsort(0, n-1);

for (int i = 0; i < n; i++)
cout<

return 0;
}

Agar bo’luvchi elementni o’rtadan emas, balki [L, R] kesmadan ixtiyoriy tanlab olinsa ishlash vaqti O(nlong(n)).


C++ dasturlash tilida saralash.
C++ da saralash juda oddiy Quick Sort bo’yicha saralaydi:


#include

#include

using namespace std;

int main() {

int n;
cin>>n;

int a[n];

for (int i = 0; i < n; i++)
cin>>a[i];
sort(a, a+n);

for (int i = 0; i < n; i++)
cout<

return 0;
}

Mavzuga doir Testlar

1) To’g’ridan-to’g’ri qo’shish orqali saralash algoritmi qanday bo’ladi:


A)
//
B)

C)

D)TJY

2)Saralashlarning nechi xil turi mavjud ?

A) 1
B) 2 //
C)3
D)4

3) To’g’ridan-to’g’ri qo’shish usuli nima deb ataladi ?

A) by insertion //
B ) by selection
C ) by exchange
D) TJY

4) To’g’ridan-to’g’ri tanlash usuli nima deb ataladi ?



A) by insertion
B ) by selection //
C ) by exchange
D) TJY

5) To’g’ridan-to’g’ri almashtirish usuli nima deb ataladi ?



A) by insertion
B ) by selection
C ) by exchange //
D) TJY


Download 0.77 Mb.

Do'stlaringiz bilan baham:
1   2




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