#include <stdio.h>
#include <stdint.h>
void bubble_sort(uint8_t *arr, uint8_t n) {
for (int i = 0; i < n-1; i++) {
// Flag to detect if a swap occurred
int swapped = 0;
for (int j = 0; j < n-i-1; j++) {
// Compare adjacent elements
if (arr[j] < arr[j+1]) {
// Swap if in wrong order
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
swapped = 1; // Mark swap happened
}
}
// If no swaps in inner loop, array is sorted
if (swapped == 0)
break;
}
}
int main() {
uint8_t n;
scanf("%hhu", &n);
uint8_t arr[100];
for (uint8_t i = 0; i < n; i++) {
scanf("%hhu", &arr[i]);
}
bubble_sort(arr, n);
for (uint8_t i = 0; i < n; i++) {
printf("%hhu", arr[i]);
if(i < n-1){
printf(" ");
}
}
return 0;
}
Input
5 10 3 5 2 7
Expected Output
10 7 5 3 2