All submissions

Find Top 3 Largest Values in an Array

Code

#include <stdio.h>
#include <stdint.h>

void bubble_sort_asc(uint8_t *arr, uint8_t n) {
    for (int i = 0; i < n; i++)
    {
        for (int j = i; j < n; j++)
        {
            if (arr[i] < arr[j])
            {
                uint8_t temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
}

void find_top_3(uint8_t *arr, uint8_t n) {
    bubble_sort_asc(arr, n);
    if (n >= 1)
        printf("%u", arr[0]);
    if (n >= 2)
        printf(" %u", arr[1]);
    if (n >= 3)
        printf(" %u", arr[2]);
}

int main() {
    uint8_t n;
    scanf("%hhu", &n);
    uint8_t arr[100];

    for (uint8_t i = 0; i < n; i++) {
        scanf("%hhu", &arr[i]);
    }

    find_top_3(arr, n);
    return 0;
}

Solving Approach

 

 

 

Loading...

Input

6 10 90 20 80 70 30

Expected Output

90 80 70