Sort an Array in Ascending Order

Code

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

void bubble_sort(uint8_t *arr, uint8_t n) {
     bool sorted = false;
     while(!sorted){
        sorted = true;
        uint8_t start=0,end=start+1;
        while(start<=n-2 || end<=n-1){
            if(arr[start]>arr[end]){
                uint8_t tmp=arr[start];
                arr[start]=arr[end];
                arr[end]=tmp;
                sorted = false;
            }
            start++,end++;
        }
     }
     
}

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;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

5 10 3 5 2 7

Expected Output

2 3 5 7 10