filmov
tv
Data Structures using C Part 14 - Binary searching program using c programming language

Показать описание
Searching in Data structure is a process of finding the location where a specified element is available.
There are two types of search techniques used in DS.
1. Linear or sequential search
2. Binary search
Binary searching :
The binary search algorithm can be used with only sorted list of element.
This search process starts comparing the search element with the middle element in the list. If both are matched, then the result is "element found".
Otherwise, we check whether the search element is smaller or larger than the middle element in the list. If the search element is smaller, then we repeat the same process for left sub-list of the middle element.
If the search element is larger, then we repeat the same process for right sub-list of the middle element.
We repeat this process until we find the search element in the list or until we left with a sub-list of only one element.
And if that element also doesn't match with the search element, then the result is "Element not found in the list".
Binary search program :
void main()
{
int number[25], n, data, i, flag = 0, low, high, mid;
clrscr();
printf("\n Enter the number of elements: ");
scanf("%d", &n);
printf("\n Enter the elements in ascending order: ");
for(i = 0; i lessthan n; i++)
scanf("%d", &number[i]);
printf("\n Enter the element to be searched: ");
scanf("%d", &data);
low = 0; high = n-1;
while(low lessthan= high)
{
mid = (low + high)/2;
if(number[mid] == data)
{
flag = 1;
break;
}
else
{
if(data lessthan number[mid])
high = mid - 1;
else
low = mid + 1;
}
}
if(flag == 1)
printf("\n Data found at location: %d", mid + 1);
else
printf("\n Data Not Found ");
getch();
}
ankpro
ankpro training
C#
C sharp
Bangalore
Rajajinagar
Selenium
Coded UI
Mobile automation testing
Mobile testing
JQuery
JavaScript
.Net
C
C++
Components of the .Net framework
Hello World
Literal
Keywords
Variable
Data types
Operators
Branching
Loops
Arrays
Strings
Structures
Enums
Functions
There are two types of search techniques used in DS.
1. Linear or sequential search
2. Binary search
Binary searching :
The binary search algorithm can be used with only sorted list of element.
This search process starts comparing the search element with the middle element in the list. If both are matched, then the result is "element found".
Otherwise, we check whether the search element is smaller or larger than the middle element in the list. If the search element is smaller, then we repeat the same process for left sub-list of the middle element.
If the search element is larger, then we repeat the same process for right sub-list of the middle element.
We repeat this process until we find the search element in the list or until we left with a sub-list of only one element.
And if that element also doesn't match with the search element, then the result is "Element not found in the list".
Binary search program :
void main()
{
int number[25], n, data, i, flag = 0, low, high, mid;
clrscr();
printf("\n Enter the number of elements: ");
scanf("%d", &n);
printf("\n Enter the elements in ascending order: ");
for(i = 0; i lessthan n; i++)
scanf("%d", &number[i]);
printf("\n Enter the element to be searched: ");
scanf("%d", &data);
low = 0; high = n-1;
while(low lessthan= high)
{
mid = (low + high)/2;
if(number[mid] == data)
{
flag = 1;
break;
}
else
{
if(data lessthan number[mid])
high = mid - 1;
else
low = mid + 1;
}
}
if(flag == 1)
printf("\n Data found at location: %d", mid + 1);
else
printf("\n Data Not Found ");
getch();
}
ankpro
ankpro training
C#
C sharp
Bangalore
Rajajinagar
Selenium
Coded UI
Mobile automation testing
Mobile testing
JQuery
JavaScript
.Net
C
C++
Components of the .Net framework
Hello World
Literal
Keywords
Variable
Data types
Operators
Branching
Loops
Arrays
Strings
Structures
Enums
Functions
Комментарии