Digital Learning

Thursday, January 2, 2020

One-dimensional array-Traversal,Searching:

There are primarily six basic operations that can be performed on an array.
1. Traversal: Accessing each and every element of an array.
2. Search: Finding the subscript of a given value in the array, if it exists.
3. Insertion: Adding a new element into the array.
4. Deletion: Removing an existing element from the array.
5. Merging: Combining two arrays into a single array.
6. Sorting Arrays: Sorting is used to arrange element in increasing and decreasing order.

1. Array Traversal:
It can be done both sequential and random manner. This is because of the linear storage of arrays in memory. The only constraint here is that the subscript of the array should be within bounds of the array size.

For example: a[10] when the size of the array is 5, can lead to undefined behavior of the program. The array subscripts should be between 0 and the size of array -1.

2. Searching in an Array: 
The process of finding out the presence of a given key in an array is called searching. The search is successful if the key is found and the location of the key is returned back.

Searching methods are designed to optimize the search for a particular record or to establish its absence. The searching method chosen can make a substantial difference to an application's performance.

There are lots of searching techniques available and some frequently-used searches in algorithm are:
1. Linear Search/Sequential Search: In linear search, we access each element of an array one by one sequentially and see whether it is desired element or not. A search will be unsuccessful if all the elements are accessed and desired element is not found and successful if desired element is found.

Program: Program to search an element using sequential search.
#include<stdio.h>
#include<conio.h>
void main()
{
int array[10], n, i, item, flag=-1;
clrscr();
printf("Enter the arrray element=");
for(i=0;i<10;i++)
{
scanf("%d", &array[i]);
}
printf("\n Enter the searching element=");
scanf("%d", &item);
for(i=0;i<10;i++)
{
if(item==array[i])
{
flag=i;
break;
}
}
if(flag>=0)
printf("\n %d is found at the position %d", item, flag+1);
else
printf("\n item does not found");
getch();
}
Output:
Enter the array element =10
12
11
16
14
15
17
18
13
20
Enter the searching element =20
20 is found at the position 10

2. Binary Search: Another relatively simple method of accessing an array is the binary search method. The entries in the array are stored in alphabetically or numerically increasing order.
Binary search requires sorted data to operate on. In this method, search begins by examining the records in the middle of file rather than the one of the ends as in sequential search.

Program: To search an element using binary search.
#include<stdio.h>
#define TRUE 0
#define FALSE 1
int main(void)
{
int array[10]={1,2,3,4,5,6,7,8,9,10};
int left=0;
int right=10;
int middle=0;
int number=0;
int bsearch=FALSE;
int i=0;
clrscr();
printf(" ARRAY:");
for(i=1;i<=10;i++)
printf("[%d]", i);
printf("\n Search for number:");
scanf("%d",&number);
while(bsearch==FALSE && left<=right)
{
middle=(left+right)/2;
if(number==array[middle])
{
bsearch=TRUE;
printf("** Number Found**\n");
}
else
{
if(number<array[middle]) right=middle-1;
if(number>array[middle]) left=middle+1;
}
}
if(bsearch==FALSE)
printf("--Number not found---\n);
getch();
}
ARRAY: [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
Search for number: 5
** Number Found**

No comments:

Post a Comment