#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void printArray(int *A, int n)
{
for (int i = 0; i < n; i++)
{
printf("%d ", A[i]);
}
printf("\n");
}
int binarySearch(int arr[], int size, int element)
{
int low, mid, high;
low = 0;
high = size - 1;
while (low <= high)
{
mid = (low + high) / 2;
if (arr[mid] == element)
{
return mid;
}
if (arr[mid] < element)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return -1;
}
void bubbleSort(int *A, int n)
{
int temp;
int isSorted = 0;
for (int i = 0; i < n - 1; i++)
{
isSorted = 1;
for (int j = 0; j < n - 1 - i; j++)
{
if (A[j] > A[j + 1])
{
temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
isSorted = 0;
}
}
if (isSorted)
{
return;
}
}
}
int main()
{
int n;
printf("enter the number of elemnts : ");
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printArray(arr, n);
bubbleSort(arr, n);
printArray(arr, n);
int element;
printf("enter the elemnts to search: ");
scanf("%d", &element);
printf("The element %d was found at index %d \n", element, binarySearch(arr, n, element));
return 0;
}