Write a function that performs a binary search for the location of a new element in an ordered array and returns a pointer to the location of the inclusion of a new element. Using this function to implement sorting inserts. I wrote the code, but I don’t know how to consider if my element is more than the last element in the array or less. And in general, I'm not sure that the code works correctly, because I work with pointers for the first time. Help to understand, please.
#include<iostream> using namespace std; int search(int* array, int len, int n){ ////СОРТИРОВКА ВСТАВКАМИ for (int i = 0; i < len; i++){ int temp = array[0]; for (int j = i + 1; j < len; j++){ if (array[i] > array[j]){ temp = array[i]; array[i] = array[j]; array[j] = temp; } } } ////БИНАРНЫЙ ПОИСК int *ptr = 0; int l = 0, r = len - 1 ; while(r>l){ int mid = (l + r)/2; if(n<=array[mid]) r = mid; else l = mid +1; }if(array[l] == n) ptr = &l; if(array[l] < n){ int a = l-1; ptr = &a; }if(array[l] > n) ptr = &l ; return *ptr; } int main(){ setlocale(LC_ALL, "rus"); int size,n; cout << "Введите размер массива: "; cin >> size; cout << "Введите цифру: "; cin >> n; int* mas = new int[size]; for(int i = 0; i<size; i++) cin >> mas[i]; search(mas, size, n); int res = search(mas, size, n); for(int i = 0;i<size; i++) cout<<mas[i]<<" "; cout<<"Элемент должен стоять на позиции между "<< res << " и " << res + 1<<" элементом "<< endl; delete []mas; return 0; } 