Displaying Top 5 Numbers in an Array in C: A Comprehensive Guide

Displaying Top 5 Numbers in an Array in C: A Comprehensive Guide

In C programming, displaying the top 5 numbers in an array is a common task. This task involves sorting the array and then selecting the first five elements. Here, we will explore different methods to achieve this and provide a detailed explanation of each approach.

Standard Approach: Sorting and Displaying the Top 5

To display the top 5 numbers in an array in C, follow these steps:

Sort the array in descending order.

Display the first 5 elements of the sorted array.

Below is a simple example demonstrating this:

include iostreaminclude algorithm // for std::sortint main() {    const int size  10; // Example size    int arr[size]  {12, 5, 8, 20, 15, 2, 30, 25, 11, 9};    // Sort the array in descending order    std::sort(arr, arr   size, std::greaterint());    // Display the top 5 numbers    std::cout  "Top 5 numbers are: "  std::endl;    for (int i  0; i  5  i  size; i  ) {        std::cout  arr[i]  " ";    }    std::cout  std::endl;    return 0;}

Explanation:

std::sort(arr, arr size, std::greaterint()); - This function sorts the array in descending order. The std::greaterint() is a comparator which sorts elements in descending order.

for (int i 0; i 5 i size; i ): - The loop iterates up to 5 times or the size of the array (whichever is smaller) to print the top 5 numbers.

Alternative Approach: Using Vector and Sorting Methods

An alternative approach involves using a std::vector to encapsulate the array and then sorting the vector. This method is particularly useful if you want to work with a dynamic array or need additional functionality.

include iostreaminclude algorithminclude vectorusing namespace std;int main() {    int list[]  {90, 44, 55, 189, 21, 89, 212, 16, 88, 48};    vector myvector  vector(list, list   10);    sort((), myvector.end());    (); // Reverse to get top 5    for (int i  0; i  5; i  ) {        cout  "number: "  myvector[i]  endl;    }}

Explanation:

vector myvector vector(list, list 10); - This line initializes a vector with the elements of the array.

sort((), myvector.end()); - Sorts the vector in ascending order.

(); - Reverses the vector to get the top 5 numbers in descending order.

for (int i 0; i 5; i ): - The loop prints the top 5 numbers.

Understanding "Top 5 Numbers"

When discussing "top 5 numbers," there are usually two interpretations:

Top 5 - The five greatest numbers: These are the five highest values in the array, regardless of order.

Top 5 - The first five numbers: These are the first five elements in the array, which might or might not be the five highest values depending on the order of the initial array.

If you need to find the top 5 greatest numbers, a more efficient method is to use Quickselect, an algorithm that can find the k-th smallest (or largest) element in an unordered list.

Quickselect Algorithm: Finding the Top 5 Greatest Numbers

Here is a simple implementation of the Quickselect algorithm for finding the top 5 greatest numbers:

include vectorinclude algorithminclude iostreamint partition(std::vector arr, int low, int high) {    int pivot  arr[high];    int i  low - 1;    for (int j  low; j  high; j  ) {        if (arr[j]  pivot) {            i  ;            std::swap(arr[i], arr[j]);        }    }    std::swap(arr[i   1], arr[high]);    return i   1;}void quickselect(std::vector arr, int left, int right, int k) {    if (left  right) return;    int pivotIndex  partition(arr, left, right);    if (k  pivotIndex) {        std::sort((), ()   k   1, std::greaterint());    } else if (k  pivotIndex) {        quickselect(arr, left, pivotIndex - 1, k);    } else {        quickselect(arr, pivotIndex   1, right, k);    }}int main() {    std::vector arr  {90, 44, 55, 189, 21, 89, 212, 16, 88, 48};    int n  ();    std::sort((), arr.end());    quickselect(arr, 0, n - 1, n - 5);    for (int i  0; i  5; i  ) {        std::cout  "number: "  arr[i]  std::endl;    }    return 0;}

Explanation:

partition(arr, low, high): - Partitions the array around a pivot.

quickselect(arr, left, right, k): - Recursively selects the k-th largest element.

std::sort((), () k 1, std::greaterint()); - Sorts the first k elements in descending order.

for (int i 0; i 5; i ): - The loop prints the top 5 numbers.

Conclusion

There are multiple ways to display the top 5 numbers in an array in C. The standard approach involves direct sorting, while using a vector and quickselect offers more flexibility and efficiency. Choosing the right method depends on your specific requirements and constraints.