
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Minimum Element in an Array Using Linear Search in C
Searching for an algorithm is considered as a step-by-step procedure for specifying a data among a large set of data.
The C programming language provides two types of searching techniques. They are as follows ?
Linear Search
A Linear Search is also known as sequential search, which is a process for finding an item in a list of items. This kind of algorithm checks each element of the list one by one until a match is found in the total list .
- Searching for the key element is performed in a linear manner.
- It is the most simplest searching technique.
- It does not require the list to be sorted.
- Limitation? It consumes more time and reduces the system's efficiency.
Given an unsorted list of elements and a key, the output will indicate "Success" if the key is found within the list. If the key is not found, the output will be "Unsuccessful".
Example 1
Below is a C program to determine the minimum elements in an array using linear search. This program specifies the user to input elements and a key, that checks the array, indicating success or failure.
#include <stdio.h> int main() { int a[50], n, i, key, flag = 0; printf("enter the no: of elements"); scanf("%d", &n); printf("enter the elements:"); for (i = 0; i < n; i++) scanf("%d", &a[i]); printf("enter a key element:"); scanf("%d", &key); for (i = 0; i < n; i++) { if (a[i] == key) { flag = 1; break; } } if (flag == 1) printf("search is successful:"); else printf("search is unsuccessfull:"); return 0; }
When the above program is executed, it produces the following result <
enter the no: of elements 5 enter the elements: 12 34 56 78 89 enter a key element:56 search is successful
Example 2
In the following program to find the minimum elements in an array using linear search. This C program determines the user to input elements, that iterates through the array to identify and return the smallest value.
#include <stdio.h> int min_ele(int numbers[], int n) { int min = numbers[0]; for (int i = 1; i < n; i++) { if (min > numbers[i]) min = numbers[i]; } return min; } int main() { int n; printf("Enter number of elements in the array: "); scanf("%d", &n); int numbers[n]; printf("Enter %d numbers: ", n); for (int i = 0; i < n; i++) { scanf("%d", &numbers[i]); } int min = min_ele(numbers, n); printf("In the array, the minimum number is: %d", min); return 0; }
The result is generated as follows ?
Enter no: of elements in an array: 5 Enter 5 numbers: 23 56 78 9 20 In an array the minimum number is: 9
Binary Search
Binary Search is an algorithm that locates the position of a target value within a sorted array. It repeatedly divides the search interval in half by comparing the target element with the middle value of the search space.
Binary Search Algorithm
A Binary search algorithm is used to find an element in a sorted array. This technique works only on sorted arrays, so the array must be sorted before applying binary search. It reduces the number of iterations needed to find the target element.
Binary Search operates on a logic principle for each search key element. It holds the value of the key to be searched. The highest and lowest values are divided and added by 2. The first and last elements represents the highest and lowest values. The middle value is compared with the key. If the middle value equals the key, the search is successful, and the output is obtained directly.
Example
This C program demonstrates a binary search on a sorted array. It prompts the user to input elements and a key, then checks if the key exists in the array, indicating success or failure.
#include <stdio.h> int main() { int i, low, high, mid, n, key, array[100]; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter %d integers: ", n); for (i = 0; i < n; i++) { scanf("%d", &array[i]); } printf("Enter value to find: "); scanf("%d", &key); low = 0; high = n - 1; mid = (low + high) / 2; while (low <= high) { if (array[mid] < key) { low = mid + 1; } else if (array[mid] == key) { printf("%d found at location %d.", key, mid + 1); break; } else { high = mid - 1; } mid = (low + high) / 2; } if (low > high) { printf("Not found! %d isn't present in the list.", key); } return 0; }
We will get the obtained result as follows ?
Enter number of elements 5 Enter 5 integers Enter value to find 1 2 3 4 5