0
0
CProgramBeginner · 2 min read

C Program to Sort Array in Ascending Order

You can sort an array in ascending order in C by using nested for loops to compare and swap elements, like this: for (int i = 0; i < n-1; i++) for (int j = i+1; j < n; j++) if (arr[i] > arr[j]) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; }.
📋

Examples

Input5 3 8 1 2
Output1 2 3 5 8
Input10 9 8 7 6
Output6 7 8 9 10
Input1 1 1 1 1
Output1 1 1 1 1
🧠

How to Think About It

To sort an array in ascending order, think of comparing each element with the others and swapping them if they are in the wrong order. Repeat this process until no more swaps are needed, so the smallest numbers move to the front step by step.
📐

Algorithm

1
Get the number of elements and the array input.
2
Start from the first element and compare it with every other element after it.
3
If the current element is greater than the compared element, swap them.
4
Repeat this for all elements until the entire array is sorted in ascending order.
5
Print the sorted array.
💻

Code

c
#include <stdio.h>

int main() {
    int arr[] = {5, 3, 8, 1, 2};
    int n = 5, temp;
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
            if (arr[i] > arr[j]) {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}
Output
1 2 3 5 8
🔍

Dry Run

Let's trace the array {5, 3, 8, 1, 2} through the sorting code.

1

Initial array

arr = {5, 3, 8, 1, 2}

2

Compare arr[0] with others

Compare 5 with 3, swap -> arr = {3, 5, 8, 1, 2} Compare 3 with 8, no swap Compare 3 with 1, swap -> arr = {1, 5, 8, 3, 2} Compare 1 with 2, no swap

3

Compare arr[1] with others

Compare 5 with 8, no swap Compare 5 with 3, swap -> arr = {1, 3, 8, 5, 2} Compare 3 with 2, swap -> arr = {1, 2, 8, 5, 3}

4

Compare arr[2] with others

Compare 8 with 5, swap -> arr = {1, 2, 5, 8, 3} Compare 5 with 3, swap -> arr = {1, 2, 3, 8, 5}

5

Compare arr[3] with arr[4]

Compare 8 with 5, swap -> arr = {1, 2, 3, 5, 8}

6

Sorted array

arr = {1, 2, 3, 5, 8}

IterationArray State
Start5 3 8 1 2
After i=01 2 8 5 3
After i=11 2 3 8 5
After i=21 2 3 5 8
After i=31 2 3 5 8
💡

Why This Works

Step 1: Nested loops for comparison

The outer loop picks each element one by one, and the inner loop compares it with all elements after it using for loops.

Step 2: Swapping elements

If the current element is bigger than the compared element, they are swapped using a temporary variable to place the smaller element first.

Step 3: Resulting sorted array

Repeating this process moves the smallest elements to the front, resulting in the array sorted in ascending order.

🔄

Alternative Approaches

Bubble Sort
c
#include <stdio.h>

int main() {
    int arr[] = {5, 3, 8, 1, 2};
    int n = 5, temp;
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}
Bubble sort repeatedly swaps adjacent elements; it is simple but less efficient for large arrays.
Using qsort from stdlib.h
c
#include <stdio.h>
#include <stdlib.h>

int compare(const void *a, const void *b) {
    return (*(int*)a - *(int*)b);
}

int main() {
    int arr[] = {5, 3, 8, 1, 2};
    int n = 5;
    qsort(arr, n, sizeof(int), compare);
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}
qsort is a built-in C function that sorts efficiently using quicksort; it is faster and recommended for large arrays.

Complexity: O(n^2) time, O(1) space

Time Complexity

The nested loops each run up to n times, so the total comparisons and swaps are proportional to n squared, making it O(n^2).

Space Complexity

Sorting is done in-place by swapping elements, so no extra array is needed, resulting in O(1) space.

Which Approach is Fastest?

Using the built-in qsort function is faster (average O(n log n)) than manual nested loops (O(n^2)) especially for large arrays.

ApproachTimeSpaceBest For
Nested loops swapO(n^2)O(1)Small arrays, learning basics
Bubble sortO(n^2)O(1)Simple implementation, educational
qsort (stdlib)O(n log n)O(log n)Large arrays, production code
💡
Use a temporary variable to swap two elements safely when sorting.
⚠️
Forgetting to swap elements correctly or mixing up the comparison operator causes incorrect sorting.