#include <stdio.h>
#include <stdint.h>
void bubble_sort_desc(uint8_t *arr, uint8_t n) {
// Your logic here
uint8_t i, j, localmax, hold;
for (i=0; i < n-1; i++) {
localmax= i; // top position
for (j=i; j < n; j++) {
if (arr[j] > arr[localmax]) {
localmax = j;
}
}
hold = arr[i];
arr[i] = arr[localmax];
arr[localmax] = hold;
}
}
void find_top_3(uint8_t *arr, uint8_t n) {
// Your logic here
bubble_sort_desc(arr, n);
uint8_t i;
for (i=0; ((i < 3) && (i < n)); i++)
printf("%hu ", arr[i]);
}
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;
}
Use the bubble_sort_desc I wrote in preceding example and sort the array.
then print the top elements (up to three).
Input
6 10 90 20 80 70 30
Expected Output
90 80 70