The document discusses various array operations like searching, sorting, and inserting elements. It describes linear and binary search algorithms, where binary search has fewer comparisons but requires a sorted array. Sorting algorithms like bubble sort are demonstrated to arrange elements in ascending or descending order. Code examples are provided to search an element, sort an array, and insert an element at a given position within an array.
2. Radhesyam.V
Searching
What is Searching?
Searching is a process to find out the existence of an
element in a given array.
What are the main types of Searching?
There are two types of searching.
Linear Search.
Binary Search.
2
3. Radhesyam.V
Input and Output
3
Array and key are the inputs and Yes or No is the output for a
searching algorithm.
Disadvantage: In some situations linear search is a slow process. In an
array of 100 elements if we want to search an element which is located at
98th position we have to make 97 unnecessary comparisons.
Advantage: Searching technique can be performed on elements with not
have an order (Increasing or Decreasing) also.
4. Radhesyam.V
Implementation
Void main()
{
// Variable declaration
int a[100], key, i, n;
// Input Array data
printf("Enter number of elements in array");
scanf("%d", &n);
printf("Enter array elements);
for (i = 1; i < =n; i++)
scanf("%d", &array[i]);
// Which element to search
printf("Enter a number to searchn");
scanf("%d", &key);
4
for (i =1; i < =n; i++)
{
if (a[i] == key)
// If required element is found
{
printf(“ key is at location %d", i);
break;
}
}
if (i == n)
printf(“ key isn't present in the array”);
return 0;
}
5. Radhesyam.V
Binary search
The rule followed by binary search technique is “DIVIDE
AND CONQUER”.
Number of comparisons are less when compared to linear
search
Elements must follow a specific order either increasing or
decreasing
5
6. Radhesyam.V
What is Sorting
Given Array of Elements
The process of arranging in to either ascending or
descending order
6
Example-1
Input: arr[] = {0, 23, 14, 12, 9}
Output: {0, 9, 12, 14, 23}
Example-2
Input: arr[] = {7, 0, 2}
Output: {0, 2, 7}
9. Radhesyam.V
Insert Element at Particular Position
void main()
{
int array[100], position, i, n, value;
printf("Enter number of elements in array");
scanf("%d", &n);
printf("Enter elementsn");
for (i = 0; i < n; i++)
scanf("%d", &array[i]);
printf("Enter the location to insert elementn");
scanf("%d", &position);
9
printf("Enter the value to insertn");
scanf("%d", &value);
for (i = n - 1; i >= position - 1; i--)
array[i+1] = array[i];
array[position-1] = value;
//Output
printf("Resultant array isn");
for (i = 0; i <= n; i++)
printf("%dn", array[c]);
}