using namespace std;
void merge_sort(int [],int ,int );
void merge(int [],int,int ,int );
int main()
{
int n;
cout<<"Massiv o'lchamini kiriting: ";
cin>>n;
int a[n];
cout<<"Massiv elementlarini kiriting: ";
for(int i=1;i<=n;i++){
cin>>a[i];
}
cout<<"Birlashtirib foydalanib saralash";
int p=1,r=n;
merge_sort(a,p,r);
cout<<"Saralangan forma"<for(int i=1;i<=n;i++){
cout<<"a["<}
}
void merge_sort(int a[],int p,int r){
int q;
if(pq=(p+r)/2;
merge_sort(a,p,q);
merge_sort(a,q+1,r);
merge(a,p,q,r);
}
}
void merge(int a[],int p,int q,int r){
cout<<"birlashtirish kiritildi"<
int n1=q-p+1;
int n2=r-q;
int L[n1+1];
int R[n2+1];
for(int i=1;i<=n1;i++){
L[i]=a[p+i-1];
}
for(int j=1;j<=n2;j++){
R[j]=a[q+j];
}
L[n1+1]=999; R[n2+1]=999;
int i=1, j=1;
for(int k=p;k<=r;k++){
if(L[i]<=R[j]){
a[k]=L[i];
i=i+1;
}
else{
a[k]=R[j]; j=j+1;
}
}
}
Quick sort (tez saralash)
#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<}
Selection sort (tanlash orqali saralash)
#include
#include
using namespace std;
template
void s_sort(T a[],int n){
int i,j,t;
for(i=0;ifor(j=i+1;jif(a[j]t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
}
int main()
{
int a[100],i,n;
cout<<"Massiv o'lchamini kiriting:\n"; cin>>n;
cout<<"\nMassiv elementlarini kiriting:\n";
for(i=0;icout<<"\nKiriting:"; cin>>a[i]; }
s_sort(a,n);
cout<<"\nSaralashdan keyingi massiv\n";
for(i=0;icout<getch();
return 0;
}
N ta talabadan iborat guruh tuzilsin. Quyidagi ma’lumotlar berilgan: familiya, ism, tug‘ilgan yili, fanlar bo‘yicha bahosi: MTvaA, oliy matematika, fizika, dasturlash, topshirgan sessiya umumiy bali.
24-variant.
Talabalarni uchinchi imtihoni natijasi bo‘yicha o‘sish tartibida.
Dastur kodi:
#include
using namespace std;
int main() {
int a[100];
string b[100];
int n, temp;
cout << " Nechta talaba kiritmoqchisiz N=";
cin>>n;
for (int i = 0; i < n; i++) {
cout<
cin>>b[i];
cout << "Balini kiriting : ";
cin >> a[i];
}
for (int i = 0; i < n; i++) {
for (int j = 1 + i; j < n; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
b[i]=b[j];
} } }
cout << " Natija:"<
for (int i = 0; i < n; i++) {
cout<
}
return 0; }
Natija:
Blok sxemasi:
For(j=0;j
For(i=0;i
Boshlash
int a[100]; string b[100]; int n, temp;
Yo’q ha
Cin>>a[i]>>b[i];
ha yo’q
Yo’q
a[j]>a[j+1]
a[j]>a
For(i=0;i
Cin>>a>>b;
Temp=a[j];
a[j]=a[j+1];
a[j]=temp;
ha ha
ha
Tamom
ha
temp = a[i];
a[i] = a[j];
a[j] = temp;
b[i]=b[j];
cout<
Xulosa
Saralash algoritmlari massivdagi elemantlarni o’sish yoki kamayish tartibida saralaydi va bularga bubble sort, insertion sort, merge sort, quick sort, selection sort kabi turlari mavjud. Bulardan bubble sort ketma-ket 2 ta kelgan har bir elementni solishtirib saralaydigan saralashning turi hisoblanadi.