Find the maximum and minimum elements in an array using C Programming Language

Find the maximum and minimum elements in an array using C Programming Language
Posted on 27-08-2023

Find the maximum and minimum elements in an array using C

Code:

#include <stdio.h>

int main() {
int arr[] = {5, 2, 9, 1, 5, 6};
int size = sizeof(arr) / sizeof(arr[0]); // Calculate the size of the array

int max = arr[0]; // Assume the first element as maximum
int min = arr[0]; // Assume the first element as minimum

for (int i = 1; i < size; i++) {
if (arr[i] > max) {
max = arr[i]; // Update maximum if current element is greater
}
if (arr[i] < min) {
min = arr[i]; // Update minimum if current element is smaller
}
}

printf("Maximum element: %d\n", max);
printf("Minimum element: %d\n", min);

return 0;
}

Explanation:

  1. #include <stdio.h>: This line tells the compiler to include the standard input and output library, which provides functions for input and output operations.

  2. int main() {: This is the beginning of the main function. All C programs start executing from here.

  3. int arr[] = {5, 2, 9, 1, 5, 6};: Here, we define an array named arr containing a list of numbers.

  4. int size = sizeof(arr) / sizeof(arr[0]);: We calculate the size of the array by dividing the total size of the array by the size of a single element. This gives us the number of elements in the array.

  5. int max = arr[0];: We initialize a variable max with the value of the first element in the array. This variable will keep track of the maximum value.

  6. int min = arr[0];: Similarly, we initialize a variable min with the value of the first element in the array. This variable will keep track of the minimum value.

  7. for (int i = 1; i < size; i++) {: We start a loop that goes through each element in the array, starting from the second element (index 1) since we already initialized max and min with the first element.

  8. if (arr[i] > max) { max = arr[i]; }: Inside the loop, we compare the current element (arr[i]) with the current maximum value (max). If the current element is greater than the current maximum, we update max with the value of the current element.

  9. if (arr[i] < min) { min = arr[i]; }: Similarly, we compare the current element with the current minimum value (min). If the current element is smaller than the current minimum, we update min with the value of the current element.

  10. }: The closing brace ends the loop.

  11. printf("Maximum element: %d\n", max);: After the loop, we use printf to print out the maximum value that we found.

  12. printf("Minimum element: %d\n", min);: We also print out the minimum value that we found.

  13. return 0;: This line indicates that the program has finished running and is returning a status of 0, which typically indicates successful execution.

  14. }: The closing brace ends the main function.

Overall, this code reads through an array of numbers, compares each number to find the maximum and minimum values, and then prints out these maximum and minimum values.

Algorithm:

  1. Start

  2. Declare an integer array arr with some values.

  3. Calculate the size of the array by dividing the total size of arr by the size of its first element. Store the result in the variable size.

  4. Initialize variables max and min with the value of the first element of arr.

  5. Loop through each element of the array from index 1 to size - 1: a. Check if the current element arr[i] is greater than the current maximum value max.

    • If true, update max with the value of arr[i]. b. Check if the current element arr[i] is smaller than the current minimum value min.

    • If true, update min with the value of arr[i].

  6. After the loop finishes, print the value of max as the maximum element found in the array.

  7. Print the value of min as the minimum element found in the array.

  8. End

This algorithm describes the step-by-step process of how the code works to find the maximum and minimum elements in the given integer array.

Time Complexity:

The time complexity of the above code is O(n), where n is the number of elements in the array.

Here's why:

  1. The loop iterates through each element of the array exactly once. Since there are n elements in the array, the loop runs n times.

  2. Inside the loop, there are only simple constant-time operations: comparisons and assignments. These operations take roughly the same amount of time regardless of the value of n.

As a result, the dominant factor in determining the time complexity is the number of iterations through the loop, which is directly proportional to the number of elements in the array (n). Therefore, the time complexity of this code is O(n), also known as linear time complexity.

Space Complexity:

The space complexity of the above code is O(1), which is constant space complexity.

Here's why:

  1. The memory usage of the program doesn't grow with the size of the input array. The memory usage remains constant regardless of the number of elements in the array.

  2. The program uses a fixed number of variables (arr, size, max, min, i) that don't depend on the input size. These variables occupy a constant amount of memory throughout the execution of the program.

  3. The program doesn't use any data structures like arrays or lists that would consume additional memory proportional to the input size.

Because the memory usage of the program remains constant regardless of the input size, the space complexity is O(1), indicating constant space usage.

Thank You