C program to sort array in ascending or descending order

Write a C program to input elements in array and sort array elements in ascending or descending order. How to sort array in ascending order in C programming. Logic to sort array in ascending or descending order.



Input size of array: 10
Input array elements: 20, 2, 10, 6, 52, 31, 0, 45, 79, 40


Array sorted in ascending order: 0, 2, 6, 10, 20, 31, 40, 45, 52, 79

Required knowledge

Basic Input Output, If else, For loop, Nested Loop, Array

Logic to sort array in ascending order

There are numerous logic to sort given set of numbers. Here I am using general algorithm which we apply in real life for simplicity. To sort array we select an element and place it to its correct position by comparing with subsequent elements.

Step by step descriptive logic to sort array in ascending order.

  1. Input size of array and elements in array. Store it in some variable say size and arr.
  2. To select each element from array, run an outer loop from 0 to size - 1. The loop structure must look like for(i=0; i<size; i++).
  3. Run another inner loop from i + 1 to size - 1 to place currently selected element at its correct position. The loop structure should look like for(j = i + 1; j<size; j++).
  4. Inside inner loop to compare currently selected element with subsequent element and swap two array elements if not placed at its correct position.

    Which is if(arr[i] > arr[j]) then swap arr[i] with arr[j].

Program to sort array in ascending order

 * C program to sort elements of array in ascending order

#include <stdio.h>
#define MAX_SIZE 100    // Maximum array size

int main()
    int arr[MAX_SIZE];
    int size;
    int i, j, temp;

    /* Input size of array */
    printf("Enter size of array: ");
    scanf("%d", &size);

    /* Input elements in array */
    printf("Enter elements in array: ");
    for(i=0; i<size; i++)
        scanf("%d", &arr[i]);

    for(i=0; i<size; i++)
         * Place currently selected element array[i]
         * to its correct place.
        for(j=i+1; j<size; j++)
             * Swap if currently selected array element
             * is not at its correct position.
            if(arr[i] > arr[j])
                temp     = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;

    /* Print the sorted array */
    printf("\nElements of array in ascending order: ");
    for(i=0; i<size; i++)
        printf("%d\t", arr[i]);

    return 0;

Once done with this program advance your learning skill by learning this method using pointers.

Learn how to sort arrays using pointers.

Important note: With a small change in the program you can change the logic for descending order. Which means replace condition if(arr[i] > arr[j]) with if(arr[i] < arr[j]) to transform the logic for descending order.


Enter size of array: 10
Enter elements in array: 20 2 10 6 52 31 0 45 79 40

Elements of array in ascending order: 0      2      6      10      20      31      40      45      52      79

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>