Dastur kodi
#include
#include
using namespace std;
struct table{
int t;
string FIO;};
int q=0;
void qs(table *a,int first,int last){
int i = first, j = last;table x =a[(first + last) / 2];
do {
while (a[i].FIO < x.FIO) i++;
while (a[j].FIO > x.FIO) j--;
if(i <= j) {
if (i < j){ swap(a[i], a[j]);q++;}
i++;
j--;
}
} while (i <= j);
if (i < last)
qs(a,i,last);
if (first < j)
qs(a,first,j);
}
int main(int args, char *argv[])
{ int n;cout<<"n=";cin>>n;
table talaba[n];
for(int i=0;i
talaba[i].t=i+1;
cin>>talaba[i].FIO;
}
qs(talaba,0,n-1);
for(int i=0;i
cout<
cout<<"quicksort algoritmi "<
}
Dastur natijasi:
talabalar sonini kiriting=5
5 ta talabalar FIO sini kiriting
Farhod
Asror
Sobir
Bobur
Vali
| 2 | Asror |
| 4 | Bobur |
| 1 | Farhod |
| 3 | Sobir |
| 5 | Vali |
Variant 33.
Masalasharti: Mashina egalarining ismlari bo‘yicha va mos ravishda ularningmashinalari haqidagi ma’lumotlar chiqarilsin.
#include
#include
using namespace std;
struct table{
int t;
string Ism;
string avtomobil; };
int q=0;
void saralash(table *a,int first,int last){
int i = first, j = last;table x =a[(first + last) / 2];
do {
while (a[i].Ism < x.Ism) i++;
while (a[i].avtomobil < x.avtomobil) i++;
while (a[j].Ism > x.Ism) j--;
while (a[j].avtomobil > x.avtomobil) j--;
if(i <= j) {
if (i < j){ swap(a[i], a[j]);q++;}
i++;
j--;
}
if(i <= j) {
if (i < j){ swap(a[i], a[j]);q++;}
i++;
j--;
}
} while (i <= j);
if (i < last)
saralash(a,i,last);
if (first < j)
saralash(a,first,j);
}
int main()
{
int n;
cout<<"n="; cin>>n;
table talaba[n];
for(int i=0;italaba[i].t=i+1;
cout << "Ism kiriting: ";
cin>>talaba[i].Ism;
cout << "Mashina nomini kiriting: ";
cin >> talaba[i].avtomobil;
}
saralash(talaba,0,n-1);
for(int i=0;icout< ";
cout<cout<<"quicksort algoritmi "<< q <<" ta o'rinlashtirishda saraladi\n";
system("PAUSE");
main(); }
Masala 2: Python da familiyalarni konsoldan kiritib alifbo tartibida chiqarish.
a = []
a1 = int(input('nechta familya kiritasiz: '))
while a1:
a3=input('familya: ')
a.append(a3)
a1=a1-1
a.sort()
print(a)
Masala 3:
1. Ro'yxat olib istalgan qiymatlarni kiritamiz.
Shu kiritilgan qiymatlarni quicksortda saralab bersin.
2. Random orqali sonlarni oraliqni beramiz.
Kompyuter esa bu sonlarni quicksortda saralab berishi kerak.
from random import randrange
def qsort(array):
if len(array) < 2:
return array
else:
pivot = array.pop(randrange(len(array)))
kichik = [i for i in array if i <= pivot]
katta = [i for i in array if i > pivot]
print(f"{kichik} + [{pivot}] + {katta}")
return qsort(kichik) + [pivot] + qsort(katta)
if __name__ == '__main__':
array1 = [1, 5, 12, 0, -5, 66]
print(array1)
print(qsort(array1))
array2 = list(range(20))
print(array2)
print(qsort(array2))
Natija:
Xulosa:
Men bu vazifani bajarish jarayonida c++ va python dasturlash tillaridagi massiv va ro’yxatlardan olgan bilim ko’nikmalarimni oshirib oldim.
Har doim masalalarda massiv va ro’yxat metodlaridan foydalnish dastur codini qisqartirish va natijani mukammalroq chiqishiga yordam beradi.
Do'stlaringiz bilan baham: |