110. Sort an Array in Descending Order

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>
#include <stdint.h>
void swap(uint8_t*a,uint8_t*b)
{
    *a=*a^*b;
    *b=*a^*b;
    *a=*a^*b;
}
void bubble_sort_desc(uint8_t *arr, uint8_t n) {
    // Your logic here
    for(int j=0;j<n;j++)
        for(int i=0;i<n;i++)
        {
            if(arr[i]<arr[i+1]) swap(&arr[i],&arr[i+1]);
        }
}

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_desc(arr, n);

    for (uint8_t i = 0; i < n; i++) {
        printf("%hhu", arr[i]);
        if(i < n-1){
            printf(" ");
        }
    }

    return 0;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote