Quantcast
Channel: C Programming Archives - QnA Plus
Viewing all articles
Browse latest Browse all 93

C Program to Find Second Largest Element in an Array

$
0
0

Here we’ll see the possible solutions of finding the second largest element in an array.

By Sorting the Array:

Easy solution that I can think of to find the second largest element in an array is to first sort the array and then pick the second or second last element of the array depending on whether the array is sorted in descending or ascending order.
In the following program we’ll first sort the array in ascending order using bubble sort. Once the array is sorted, then the second last element will be the second largest element.

#include <stdio.h>
#include <stdlib.h>

void bubble_sort(int *arr, int n)
{
  int i = 0, j = 0;
  int tmp = 0;

  for(i = 0; i < n-1; i++)
  {
    for (j = 0; j < n-i-1; j++)
    {
      if(arr[j+1] < arr[j])
      {
         tmp = arr[j];
         arr[j] = arr[j+1];
         arr[j+1] = tmp;
      }
    }
  }
}

int main()
{
  int n, i;
  int *arr = NULL;
  printf("Enter number of elements: ");
  scanf("%d", &n);

  arr = (int *)malloc(n*sizeof(int));
  if (arr == NULL)
  {
    printf("Memory allocation failed.\n");
    return 0;
  }

  for (i = 0; i < n; i++)
  {
    printf("Enter %d-th element: ", i+1);
    scanf("%d", arr+i);
  }
  printf("Input array: ");
  for (i = 0; i < n; i++)
  {
    printf(" %d", arr[i]);
  }
  printf("\n");

  /*Doing bubble sort*/
  bubble_sort(arr, n);

  printf("Second largest element of the array: %d\n", arr[n-2]); /*(n-1)-th element is the last one, (n-2)-th element is the second last.*/
  return 1;
}

Output of the Program:

Enter number of elements: 6
Enter 1-th element: 32
Enter 2-th element: 1
Enter 3-th element: 34
Enter 4-th element: 24
Enter 5-th element: 5
Enter 6-th element: 76
Input array:  32 1 34 24 5 76
Second largest element of the array: 34

Though this is very simple solution, it has some drawback. It time complexity is O(n2) whereas O(n) solution is possible. O(n2) is very inefficient compared to O(n).

Without Sorting

The following solution is by traversing the array once.

#include <stdio.h>
#include <stdlib.h>


int main()
{
  int n, i;
  int largest, second_largest;
  int *arr = NULL;
  printf("Enter number of elements: ");
  scanf("%d", &n);

  arr = (int *)malloc(n*sizeof(int));
  if (arr == NULL)
  {
    printf("Memory allocation failed.\n");
    return 0;
  }

  for (i = 0; i < n; i++)
  {
    printf("Enter %d-th element: ", i+1);
    scanf("%d", arr+i);
  }
  printf("Input array: ");
  for (i = 0; i < n; i++)
  {
    printf(" %d", arr[i]);
  }
  printf("\n");

  largest = arr[0];
  second_largest = largest;

  for (i = 1; i < n; i++) { if(arr[i] > largest)
    {
      second_largest = largest;
      largest = arr[i];
    }
    else if (arr[i] > second_largest)
    {
      second_largest = arr[i];
    }
  }

  printf("Second largest element of the array: %d\n", second_largest);
  return 1;
}

In every step of the two largest elements are kept. The variables are initialized with the first element of the array assuming that it will be the desired result. From next iteration, the current element of the array is compared with the highest element. If the current element is highest element, the previous highest element will replace the second highest element and will be replaced by the current element. If the current element of the array is not greater than the highest element but greater than the second highest element, then it will replace the second highest element. At the end of the iteration, the variable second_largest will be the second highest element.

The post C Program to Find Second Largest Element in an Array appeared first on QnA Plus.


Viewing all articles
Browse latest Browse all 93

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>