# Guruh talabasi Abdullayev Sayidamirxon

Yüklə 310,93 Kb.
 tarix 05.12.2023 ölçüsü 310,93 Kb. #138360
MTA

 Muhammad al-Xorazmiy nomidagi Toshkent axborot texnologiyalari universiteti Nurafshon filiali­­­­­ Kompyuter injiniringi fakulteti 730-22 guruh talabasi Abdullayev Sayidamirxon Ma’lumotlar tuzilmasi va algoritmi fanidan TOPSHIRIQ #include #include #include // transform funksiyasi uchun using namespace std; // Ma'lumotlar uchun sinf tuzish class Item { public: string name; int value; // Constructor Item(string n, int v) : name(n), value(v) {} }; // Ma'lumotlar to'plami vector items; // Binar qidirish funksiyasi int binarySearch(const vector& data, const string& key) { int low = 0; int high = data.size() - 1; while (low <= high) { int mid = low + (high - low) / 2; if (data[mid].name == key) { return mid; // Indeks topilgan } else if (data[mid].name < key) { low = mid + 1; } else { high = mid - 1; } } return -1; // Topilmadi } int main() { // Ma'lumotlarni to'ldirish items.push_back(Item("APPLE", 10)); items.push_back(Item("BANAN", 5)); items.push_back(Item("ORANGE", 8)); // Qidirish uchun kalit so'z string key; cout << "Narxini bilmoqchi bo'lgan mevani kiriting: "; cin >> key; // Katta harfga o'tkazish transform(key.begin(), key.end(), key.begin(), ::toupper); // Binar qidirishni bajarish int result = binarySearch(items, key); // Natija if (result != -1) { cout << "Topildi! " << key << " narxi: " << items[result].value << " ming so'm.\n"; } else { cout << "Topilmadi!\n"; } return 0; } TOPSHIRIQ TOPSHIRIQ #include using namespace std; // Selection Sort void selectionSort(int arr[], int n) { for (int i = 0; i < n - 1; i++) { int minIndex = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } swap(arr[i], arr[minIndex]); } } // Bubble Sort void bubbleSort(int arr[], int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { swap(arr[j], arr[j + 1]); } } } } // Utility function to print an array void printArray(int arr[], int size) { for (int i = 0; i < size; i++) cout << arr[i] << " "; cout << endl; } int main() { int n, choice; cout << "Sortlash usulini tanlang:\n"; cout << "1 - Selection Sort\n"; cout << "2 - Bubble Sort\n"; cout << "Tanlovingizni kiriting: "; cin >> choice; cout << "Massiv uzunligini kiriting: "; cin >> n; int arr[n]; cout << "Massivni kiriting:\n"; for (int i = 0; i < n; i++) { cin >> arr[i]; } switch (choice) { case 1: // Selection Sort selectionSort(arr, n); cout << "Selection Sort natijasi: "; printArray(arr, n); break; case 2: // Bubble Sort bubbleSort(arr, n); cout << "Bubble Sort natijasi: "; printArray(arr, n); break; default: cout << "Noto'g'ri tanlov!\n"; } return 0; } Yüklə 310,93 Kb.Dostları ilə paylaş:

Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©muhaz.org 2024
rəhbərliyinə müraciət