Sorting Array without bubble sort function

 #include <stdio.h>


int main() {

    int n, i, j;

    printf("Enter the size of the array: ");

    scanf("%d", &n);


    int arr[n];

    printf("Enter the elements of the array: \n");

    for (i = 0; i < n; i++) {

        scanf("%d", &arr[i]);

    }


    // Sorting the array in ascending order

    for (i = 0; i < n-1; i++) {

        for (j = i+1; j < n; j++) {

            if (arr[i] > arr[j]) {

                int temp = arr[i];

                arr[i] = arr[j];

                arr[j] = temp;

            }

        }

    }


    printf("The sorted array is: \n");

    for (i = 0; i < n; i++) {

        printf("%d ", arr[i]);

    }

    printf("\n");


    return 0;

}

Comments