Binar qidiruv usuli yordamida massivga yangi elementni kiriting


Download 115.27 Kb.
Sana07.01.2023
Hajmi115.27 Kb.
#1081251
Bog'liq
2-mustaqil ish




O’zbekiston Respublikasi Axborot Texnologiyalari va
Kommunikatsiyalarini Rivojlantirish Vazirligi
Muhammad Al-Xorazmiy nomidagi Toshkent Axborot Texnologiyalari Universiteti


*
.2022





072-20 guruh talabasi Abdurahmonov Shuhratning
Malumotlar tuzilmasi va algoritmlar fanidan 2-laboratoriya ishi





23-variant
1-misol: Binar qidiruv usuli yordamida massivga yangi elementni kiriting.
Dastur kodi:
#include
using namespace std;

int insertSorted(int arr[], int n, int key, int capacity)
{

if (n >= capacity)
return n;

int i;
for (i = n - 1; (i >= 0 && arr[i] > key); i--)
arr[i + 1] = arr[i];

arr[i + 1] = key;

return (n + 1);
}

int main()
{
int arr[20] = { 12, 16, 20, 40, 50, 70 };
int capacity = sizeof(arr) / sizeof(arr[0]);
int n = 6;
int i, key = 26;

cout << "\nKiritishdan oldin : ";
for (i = 0; i < n; i++)
cout << arr[i] << " ";

n = insertSorted(arr, n, key, capacity);

cout << "\nBinar usulida kiritishdan keyin : ";
for (i = 0; i < n; i++)
cout << arr[i] << " ";

return 0;
}
Dastur Natijasi:

Download 115.27 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling