// Program by Akash Tripathi (@proakash256)
#include <stdio.h>
int main()
{
int n , s , pos;
printf("Enter the number of elements you want to
enter in the array : ");
scanf("%d" , &n);
int arr[n];
printf("\nEnter the elements :\n");
for(int i = 0; i < n; i = i + 1)
scanf("%d" , &arr[i]);
for(int i = 0; i < (n - 1); i = i + 1)
{
for(int j = 0; j < (n - 1 - i); j = j + 1)
{
if(arr[j] > arr[j + 1])
{
arr[j] = arr[j] + arr[j + 1];
arr[j + 1] = arr[j] - arr[j + 1];
arr[j] = arr[j] - arr[j + 1];
}
}
}
printf("\nThe Sorted array in ascending order is :\n");
for(int i = 0; i < n; i = i + 1)
printf("%d\n" , arr[i]);
int start = 0 , last = (n - 1) , middle , found = 0;
printf("\nEnter the element that you want to
search in the array : ");
scanf("%d" , &s);
while(start <= last)
{
middle = (start + last) / 2;
if(arr[middle] == s)
{
pos = middle;
found = 1;
break;
}
else if(s > arr[middle])
start = middle + 1;
else if(s < arr[middle])
last = middle - 1;
}
if(found == 1)
printf("\nThe element %d is at
position %d\n\n" , s , (pos + 1));
else
printf("\nValue Not Found.\n\n");
return 0;
}
Comments
Post a Comment