- How to find the minimum element in an array using a function
- How to traverse an array using a pointer instead of an index
- How to compare array elements using
*ptr - Why the first element is initially assumed to be the minimum
- How pointer arithmetic moves from one array element to the next
- Why the time complexity of this method is O(n)
How Pointer-Based Minimum Finding Works
Finding the minimum element means identifying the smallest value stored in an array. The basic idea is to assume that the first element is the minimum, then compare every remaining element with it. Whenever a smaller value is found, update the minimum.
In the pointer version, instead of using an index such as arr[i], the program uses a pointer ptr to visit each element. The expression *ptr gives the value stored at the current pointer position. After each comparison, ptr++ moves the pointer to the next element.
The function starts with min = *arr, which stores the first element as the initial minimum. Then it begins traversal from arr + 1, because the first element has already been considered. The loop continues while ptr < arr + n, where arr + n is one position past the last element.
💡 In one line: Assume the first element is minimum, traverse the remaining elements with a pointer, and update the minimum whenever *ptr is smaller.
The Three Steps of Finding the Minimum
| Step | Pointer operation | Effect |
|---|---|---|
| 1. Set the initial minimum | int min = *arr; | The first element is assumed to be the smallest |
| 2. Traverse and compare | for (ptr = arr + 1; ptr < arr + n; ptr++) | Every remaining element is compared with the current minimum |
| 3. Update the minimum | if (*ptr < min) min = *ptr; | The minimum value is replaced when a smaller element is found |
This method works for positive numbers, negative numbers, and unsorted arrays. The array does not need to be arranged in any particular order because every element is checked one by one.
The pointer version is functionally equivalent to the index version. The main difference is that *ptr replaces arr[i], and ptr++ replaces i++. This helps you practice pointer dereferencing and pointer arithmetic.
Complete C Program
Program: find the minimum element in an array using pointers
#include <stdio.h>
#define MAX 100
/* Find the minimum element using pointers.
Returns the smallest value in the array. */
int find_minimum(const int *arr, int n) {
const int *ptr;
int min;
if (n <= 0) {
printf("Array is empty.\n");
return 0;
}
min = *arr; /* assume first element is minimum */
for (ptr = arr + 1; ptr < arr + n; ptr++) {
if (*ptr < min) {
min = *ptr; /* update minimum */
}
}
return min;
}
int main(void) {
int arr[MAX];
int n, i, minimum;
printf("How many elements? ");
if (scanf("%d", &n) != 1 || n <= 0 || n > MAX) {
printf("Invalid size.\n");
return 1;
}
printf("Enter %d integers:\n", n);
for (i = 0; i < n; i++) {
if (scanf("%d", &arr[i]) != 1) {
printf("Invalid input.\n");
return 1;
}
}
minimum = find_minimum(arr, n);
printf("\nThe minimum element is: %d\n", minimum);
return 0;
}
Program Output
How many elements? 6
Enter 6 integers:
25 89 14 67 93 41
The minimum element is: 14
A second run, with negative numbers:
How many elements? 5
Enter 5 integers:
-10 -45 -3 -78 -20
The minimum element is: -78
In the first run, the pointer compares every element with the current minimum and finally finds 14. In the second run, all values are negative, but the program correctly identifies -78 as the minimum because it starts with the first array element.
Step-by-Step Explanation
Let us understand how the pointer visits each element and finds the smallest value.
1. The function signature
find_minimum(const int *arr, int n) receives a pointer to the first array element and the number of elements. The const keyword tells the compiler that the function will only read the array and will not modify it.
2. Setting the initial minimum
min = *arr; stores the first array element in min. This is important because the minimum cannot safely be initialized to zero when the array may contain only positive numbers.
3. Starting from the second element
The pointer starts at arr + 1, which is the second element. The first element is already stored in min, so there is no need to compare it with itself.
4. Comparing the current element
The condition *ptr < min compares the value at the current pointer position with the current minimum. If the current element is smaller, min is updated to *ptr.
5. Moving the pointer
After each comparison, ptr++ moves the pointer to the next array element. Because ptr is an int pointer, the increment moves forward by sizeof(int) bytes, not by one byte.
6. Loop termination
The loop runs while ptr < arr + n. When ptr reaches arr + n, it has moved past the last valid element, so the traversal stops.
7. Example trace
For the array [25, 89, 14, 67, 93, 41]:
- Initially,
min = 25. - The pointer reaches
89. Since89 > 25,minremains 25. - The pointer reaches
14. Since14 < 25,minbecomes 14. - The pointer reaches
67. Since67 > 14,minremains 14. - The pointer reaches
93. Since93 > 14,minremains 14. - The pointer reaches
41. Since41 > 14,minremains 14. - The function returns 14.
8. Pointer version vs index version
The same logic can be written using an index:
int find_minimum_index(const int arr[], int n) {
int min = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}
Both versions produce the same result. The pointer version uses *ptr instead of arr[i] and ptr++ instead of i++. It is useful for learning how arrays and pointers are related in C.
9. Handling positive and negative numbers
Initializing min with the first array element makes the program correct for both positive and negative values. If min were initialized to zero, an array such as [10, 45, 3] would incorrectly return zero as the minimum.
10. Complexity summary
Time complexity is O(n), because every element must be examined once. Best, average, and worst cases are all O(n), since even the smallest element may be at the end of the array. Space complexity is O(1), because the function uses only a pointer and one minimum variable.
🎯 Remember this: Start with min = *arr, traverse from arr + 1 to arr + n, compare each value using *ptr < min, and update min whenever a smaller element is found.
Frequently Asked Questions
1. Why do we initialize min with the first element?
Initializing min with the first element makes the program correct for all integers, including positive and negative numbers. If min were initialized to zero, an array containing only positive numbers would give an incorrect result.
2. Why does the pointer start at arr + 1?
The first element is already stored in min, so it does not need to be compared again. Starting from arr + 1 avoids one unnecessary comparison.
3. What does *ptr mean?
*ptr dereferences the pointer, meaning it accesses the value stored at the memory location pointed to by ptr. For example, if ptr points to the third array element, *ptr gives the value of that element.
4. Does the array need to be sorted?
No. This method works on both sorted and unsorted arrays because it examines every element individually. Sorting is required for binary search, but not for finding the minimum element.
5. What happens if the array has duplicate minimum values?
The function still returns the same minimum value. For example, in the array [10, 5, 20, 5], it returns 5. If you also need the position of the minimum, you must store the pointer or index whenever a new minimum is found.
6. What is the time complexity of finding the minimum element?
The time complexity is O(n), because the function must check every element at least once. The space complexity is O(1), since only one pointer and one minimum variable are used.
💡 Tip: After implementing this program, modify it to return both the minimum value and its index. You can use pointer subtraction, ptr - arr, to calculate the index of the minimum element.