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
Do'stlaringiz bilan baham: |