O’zbekiston Respublikasi Axborot Texnologiyalari va
Kommunikatsiyalarini Rivojlantirish Vazirligi
Muhammad Al-Xorazmiy nomidagi Toshkent Axborot Texnologiyalari Universiteti
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:
Do'stlaringiz bilan baham: |