#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_kth_elements(uint8_t *arr, uint8_t n, uint8_t k, uint8_t *smallest, uint8_t *largest) {
// Your logic here
bubble_sort_desc(arr, n);
*smallest = arr[n-k];
*largest = arr[k-1];
}
int main() {
uint8_t n, k;
scanf("%hhu", &n);
uint8_t arr[100];
for (uint8_t i = 0; i < n; i++) {
scanf("%hhu", &arr[i]);
}
scanf("%hhu", &k);
uint8_t smallest, largest;
find_kth_elements(arr, n, k, &smallest, &largest);
printf("%hhu %hhu", smallest, largest);
return 0;
}
use the bubble_sort_desc I wrote in preceding example,
now the array is sorted, pick the kth largest and smallest
Input
5 10 3 5 2 7 2
Expected Output
3 7