C Program to Sort Array in Ascending Order
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
How to Think About It
Algorithm
Code
#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; }
Dry Run
Let's trace the array {5, 3, 8, 1, 2} through the sorting code.
Initial array
arr = {5, 3, 8, 1, 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
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}
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}
Compare arr[3] with arr[4]
Compare 8 with 5, swap -> arr = {1, 2, 3, 5, 8}
Sorted array
arr = {1, 2, 3, 5, 8}
| Iteration | Array State |
|---|---|
| Start | 5 3 8 1 2 |
| After i=0 | 1 2 8 5 3 |
| After i=1 | 1 2 3 8 5 |
| After i=2 | 1 2 3 5 8 |
| After i=3 | 1 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
#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; }
#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; }
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Nested loops swap | O(n^2) | O(1) | Small arrays, learning basics |
| Bubble sort | O(n^2) | O(1) | Simple implementation, educational |
| qsort (stdlib) | O(n log n) | O(log n) | Large arrays, production code |