C program to sort array in ascending or descending order

Write a C program to read elements in an array and sort elements of the array in ascending order. Read unsorted integer values in array and sort them in ascending order. Logic to sort an array in ascending order.

Example

Input

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

Output

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

Required knowledge

Basic C programming, If else, For loop, Array

Read more - Program to swap two numbers

Logic to sort an array in ascending order

There are numerous logic to sort a 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.
Below is the 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 array[].
  2. To select each element from the 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 the currently selected element at its correct position. The loop structure should look like for(j=i+1; j<size; j++).
  4. Inside the inner loop to compare the currently selected element with subsequent elements and perform swapping if needed. Which is if(array[i] > array[j]) then swap array[i] and array[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

int main()
{
    int array[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", &array[i]);
    }

    for(i=0; i<size; i++)
    {
        /* 
         * Place the 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(array[i] > array[j])
            {
                temp     = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }

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

    return 0;
}

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

Output

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

Elements of array in sorted 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>