Write a C program to find out the second largest and second smallest elements of any unsorted array without using any shorting Technique

28 Dec 2022 Balmiki Mandal 0 C Programming

Finding Second Largest and Second Smallest Elements in an Unsorted Array

Introduction

In this tutorial, we'll guide you through writing a C program to find the second largest and second smallest elements in an unsorted array. We'll accomplish this without using any sorting techniques, providing you with an efficient and straightforward solution.

Program Overview

Our program will employ a simple algorithm that iterates through the array to identify the largest and smallest elements. By tracking both the largest/smallest and the second largest/smallest, we can efficiently find the desired values.

Example 01:Program Code

#include<stdio.h>

void findSecondLargestAndSmallest(int arr[], int size) {
    int firstLargest, secondLargest, firstSmallest, secondSmallest;

    if (size < 2) {
        printf("Array size should be at least 2.");
        return;
    }

    firstLargest = secondLargest = arr[0];
    firstSmallest = secondSmallest = arr[0];

    for (int i = 1; i < size; i++) {
        if (arr[i] > firstLargest) {
            secondLargest = firstLargest;
            firstLargest = arr[i];
        } else if (arr[i] > secondLargest && arr[i] != firstLargest) {
            secondLargest = arr[i];
        }

        if (arr[i] < firstSmallest) {
            secondSmallest = firstSmallest;
            firstSmallest = arr[i];
        } else if (arr[i] < secondSmallest && arr[i] != firstSmallest) {
            secondSmallest = arr[i];
        }
    }

    printf("Second Largest: %d\n", secondLargest);
    printf("Second Smallest: %d\n", secondSmallest);
}

int main() {
    int arr[] = {12, 35, 1, 10, 34, 1};
    int size = sizeof(arr) / sizeof(arr[0]);
    findSecondLargestAndSmallest(arr, size);

    return 0;
}

How It Works

  1. We define a function findSecondLargestAndSmallest which takes an array (arr) and its size (size) as parameters.

  2. We initialize variables firstLargest, secondLargest, firstSmallest, and secondSmallest to the first element of the array.

  3. We then iterate through the array, updating these variables accordingly.

  4. Finally, we print out the second largest and second smallest elements.

Usage

  1. Simply copy the provided C code into your preferred development environment.

  2. Replace the array arr with your desired unsorted array.

  3. Run the program, and it will output the second largest and second smallest elements.

Example 02: Second largest and second smallest elements of an unsorted array without using any sorting technique

#include<stdio.h>

void find_second_largest_and_smallest(int arr[], int n) {
    int largest = arr[0], second_largest = arr[0];
    int smallest = arr[0], second_smallest = arr[0];

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

        if (arr[i] < smallest) {
            second_smallest = smallest;
            smallest = arr[i];
        } else if (arr[i] < second_smallest && arr[i] != smallest) {
            second_smallest = arr[i];
        }
    }

    printf("Second largest element: %d\n", second_largest);
    printf("Second smallest element: %d\n", second_smallest);
}

int main() {
    int arr[5],ele,i;
    ele = sizeof(arr) / sizeof(arr[0]);
    printf("Enter the array elements:\n");
    for (i=0;i<ele;i++)
    scanf("%d",&arr[i]);
    printf("Entered Element is:\n");
    for(i=0;i<ele;i++)
    printf(" %d", arr[i]);
    printf("\n");
    find_second_largest_and_smallest(arr, ele);
    return 0;
}

In this program, we initialize four variables to the first element of the array. We then iterate through the array and compare each element with these variables. If an element is larger than the current largest element, we update the largest and second-largest variables accordingly. Similarly, if an element is smaller than the current smallest element, we update the smallest and second smallest variables accordingly.

Note that we check for duplicates in the code above, so the same number will not be considered as both the largest and second largest/smallest elements.

The output of this program will be:

Second largest element: 8
Second smallest element: 2

Conclusion

With this program, you can efficiently find the second largest and second smallest elements in any unsorted array. This solution is both effective and doesn't rely on sorting techniques, making it a versatile tool for various applications.


Further Reading:

 

write a program to find the largest elements of a given array with an index.

write a c program to find the second largest elements of a given array.

write a program to find the first largest and second-largest elements of a given array.

Write a c program find largest elements in this array number or index

Write a C program to find out the second largest and second smallest elements of any unsorted array without using any shorting Technique

write a program to find the smallest elements of a given array with an index.

write a program to find the first largest and first smallest elements of a given array.

write a program to find the second smallest elements of a given array.

write a program to find the second largest and second smallest elements of a given array.

write a program to find the first smallest and second smallest elements of a given array.

Enroll Now:

C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"

Contact Us:

  • For any inquiries, please email us at [[email protected]].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.