|
Davlatov Asatilla 6-variant Shaxs (fish, yoshi, jinsi, millati)
|
tarix | 25.12.2023 | ölçüsü | 18,83 Kb. | | #195590 |
|
Davlatov Asatilla
6-variant
1.Shaxs (FISH, yoshi, jinsi, millati)
#include
#include
#include
#include
// Shaxs haqida ma'lumotlarni saqlash uchun klass
class Person {
public:
std::string ism;
std::string familya;
int yosh;
std::string jins;
std::string millat;
// Operator, ism-sharif bo'yicha saralash uchun
bool operator<(const Person& other) const {
return ism < other.ism;
}
};
// Fayldan ma'lumotlarni o'qib oladigan funksiya
std::vector
readDataFromFile(const std::string& filename) {
std::ifstream file(filename);
std::vector
people;
if (file.is_open()) {
std::string line;
while (std::getline(file, line)) {
std::istringstream iss(line);
Person person;
iss >> person.ism >> person.familya >> person.yosh >> person.jins >> person.millat;
people.push_back(person);
}
file.close();
} else {
std::cerr << "Fayl ochilmadi!" << std::endl;
}
return people;
}
// Selection Sort algoritmi
void selectionSortByFullName(std::vector
& people) {
int n = people.size();
for (int i = 0; i < n - 1; ++i) {
int min_idx = i;
for (int j = i + 1; j < n; ++j) {
if (people[j] < people[min_idx]) {
min_idx = j;
}
}
std::swap(people[i], people[min_idx]);
}
}
int main() {
std::string filename = "malumotlar.txt"; // Sizning fayl nomeringiz
// Fayldan ma'lumotlarni o'qib olamiz
std::vector
people = readDataFromFile(filename);
// Selection Sort orqali ism-sharif bo'yicha saralash
selectionSortByFullName(people);
// Saralgan ma'lumotlarni ekranga chiqaramiz
for (const auto& person : people) {
std::cout << "Ism-sharif: " << person.ism << " " << person.familya << std::endl;
std::cout << "Yoshi: " << person.yosh << std::endl;
std::cout << "Jinsi: " << person.jins << std::endl;
std::cout << "Millati: " << person.millat << std::endl << std::endl;
}
return 0;
}
2. 34, 23, 73, 45, 25, 21, 33, 59 ExchangeSort MergeSort
#include
#include
// Merge Sort algoritmi
void merge(std::vector& to'plam, int bosh, int o'rtacha, int oxir) {
int n1 = o'rtacha - bosh + 1;
int n2 = oxir - o'rtacha;
std::vector chap_qism(n1);
std::vector o'ng_qism(n2);
for (int i = 0; i < n1; i++) {
chap_qism[i] = to'plam[bosh + i];
}
for (int j = 0; j < n2; j++) {
o'ng_qism[j] = to'plam[o'rtacha + 1 + j];
}
int i = 0;
int j = 0;
int k = bosh;
int taqqoslashlar = 0; // Taqqoslashlar soni
while (i < n1 && j < n2) {
if (chap_qism[i] <= o'ng_qism[j]) {
to'plam[k] = chap_qism[i];
i++;
} else {
to'plam[k] = o'ng_qism[j];
j++;
}
k++;
taqqoslashlar++; // Har bir taqqoslashda hisoblash
}
while (i < n1) {
to'plam[k] = chap_qism[i];
i++;
k++;
}
while (j < n2) {
to'plam[k] = o'ng_qism[j];
j++;
k++;
}
}
void mergeSort(std::vector& to'plam, int bosh, int oxir) {
if (bosh < oxir) {
int o'rtacha = bosh + (oxir - bosh) / 2;
mergeSort(to'plam, bosh, o'rtacha);
mergeSort(to'plam, o'rtacha + 1, oxir);
merge(to'plam, bosh, o'rtacha, oxir);
}
}
int main() {
std::vector to'plam = {34, 23, 73, 45, 25, 21, 33, 59};
int n = to'plam.size();
std::cout << "Boshlang'ich to'plam: ";
for (int i = 0; i < n; i++) {
std::cout << to'plam[i] << " ";
}
std::cout << std::endl;
mergeSort(to'plam, 0, n - 1);
std::cout << "Saralgan to'plam: ";
for (int i = 0; i < n; i++) {
std::cout << to'plam[i] << " ";
}
std::cout << std::endl;
return 0;
}
Dostları ilə paylaş: |
|
|