Array Operations
Array Operations
Traverse Operation
Example
Following program traverses and prints the elements of an array:
#include <stdio.h>
Int main() {
int LA[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;
printf("The original array elements are :\n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);
}
return 0;
}
Insertion Operation
• Insert operation is to insert one or more data elements into an array. Based on the requirement, a new element
can be added at the beginning, end, or any given index of array.
#include <stdio.h>
#define MAX 5
void main() {
int array[MAX] = {2, 3, 4, 5};
int N = 4; // number of elements in array
int i = 0; // loop variable Insertion at the Beginning of an Array
int value = 1; // new data element to be stored in array
// print to confirm
printf("Printing array after insertion −\n");
#include <stdio.h>
#define MAX 5
void main() {
int array[MAX] = {1, 2, 4, 5};
// print to confirm
printf("Printing array after insertion −\n");
#define MAX 5
void main() {
int array[MAX] = {1, 2, 4, 5};
// print to confirm
printf("Printing array after insertion −\n");
Types
Linear search/sequential search
Binary Search
What is a Linear Search?
• A linear search, also known as a sequential search, is a method of
finding an element within a list. It checks each element of the list
sequentially until a match is found or the whole list has been
searched.
How Linear Search Works? 1.Start from the first element,
compare k with each element x.
key is 1
2.If x == k, return the index
• 1. In Linear search, we search an element or value in a given array by traversing the array from
the starting, till the desired element or value is found.
• 2. The array is searched sequentially and the position is returned if the key element to be
searched is available in the array, otherwise -1 is returned.
• 3. Here in this C Program we have not made any function specifically for linear search, rather we
can look for presence of element in an array in the main function itself.
• 4. We traverse the array from the 0th index in increasing order of index, if we find the element we
break the loop there itself and print the position of the element in the array, but if the element
requested is not there in array, we simply print that “Element is not present in the array”.
• 5. If we’d have created a separate function for linear search and the element could not be found
in the array, we would have returned -1 in that case denoting absence of the element.
Binary Search
• A Binary Search is a sorting algorithm, that is used to search an
element in a sorted array. A binary search technique works only on a
sorted array, so an array must be sorted to apply binary search on the
array. It is a searching technique that is better then the liner search
technique as the number of iterations decreases in the binary search.
• The logic behind the binary search is that there is a key. This key holds
the value to be searched. The highest and the lowest value are added
and divided by 2. Highest and lowest and the first and last element in
the array. The mid value is then compared with the key. If mid is equal
to the key, then we get the output directly. Else if the key is greater
then mid then the mid+1 becomes the lowest value and the process is
repeated on the shortened array. Else if the key value is less then mid,
mid-1 becomes the highest value and the process is repeated on the
shortened array. If it is not found anywhere, an error message is
displayed.
Example
• Selection Sort repeatedly searches for the smallest element from the
unsorted part of the array and places it at the end of sorted part of
the array. Selection sort first finds the smallest element in the
unsorted array and swaps it with the first element. Then it finds the
second smallest element in the unsorted array and swaps it with the
second element, and the algorithm keeps doing this until the entire
array is sorted.
Let's take an array {20, 12 , 23, 55 ,21}
Set the first element of the array as minimum. Place the minimum at the first position( index 0) of the array.
Compare the minimum with the next element, if it is smaller for the next iteration, start sorting from the first unsorted
than minimum assign this element as minimum. Do this till the element i.e. the element next to where the minimum is place
end of the array.
Array = {12, 20 ,23, 55, 21}
Comparing with 12 : 20 > 12 , minimum = 12
Searching starts from 20, next element where minimum is
Comparing with 23 : 12 < 23 , minimum = 12 placed.
Minimum = 23.
Iteration 2 :
Comparing with 55 : 23 < 55 , minimum = 23
Minimum = 20
Comparing with 21 : 23 > 21 , minimum = 21
Comparing with 23 : 20 < 23 , minimum = 20
Minimum is moved to index = 2
Comparing with 55 : 20 < 55 , minimum = 20
Array = {12, 20, 21, 55, 23}
Comparing with 21 : 20 < 21 , minimum = 20
Iteration 4 :
Minimum in place no change,
Minimum = 55
Array = {12, 20 ,23, 55, 21}
Comparing with 23 : 23 < 55 , minimum = 23