How to sort an array in C language (Sorting Algorithm Explained) - ComputeNepal
Hello readers, in this article we will discuss one of the most important algorithms that everyone should know i.e. sorting algorithm. Sorting is one of the basic and most important algorithms in almost any programming language. Sorting is a basic need in most of the programs that exist. We will discuss the algorithm on the basis of C language but the algorithm is the same in almost all the languages out there.

What is an array?

Simply, the array is the collection of the same type of data that can be stored inside a single variable increasing the productivity and efficiency of programmers. If you want to enter say around 50 data and store it somewhere, the traditional approach would be to declare 50 variables and store data in them but it is not efficient in this case. We can use this technique to store 10, 20, 30, or maybe even 40 but why do this is a better way is already there. Here comes the play of array. We can store multiple numbers of homogenous data in a single variable eliminating the need for multiple variables in the program.
 

What is sorting of an array?

Now we know what is an array, let’s discuss the sorting of the array. Sorting of array basically is nothing but arranging the data inside an array in either ascending or descending order on the basis of requirement. An array can contain data in random order, the process of arranging them in a specific order is called sorting of an array. Sorting seems like nothing but it’s definitely not the case. Sorting involves a small algorithm in it. That leads to the next heading i.e. swapping of elements during sorting.
 

Swapping of Element during Sorting

The concept of swapping is the main game here. Let’s consider we want to arrange an array in ascending order, in order to do that we need to swap different elements of the array. In order to swap the elements, we will need to first compare elements of the array at different indexes. First of all, we would need to compare the first element of the array with other elements. If the first number is not the greatest then we need to swap the first element with the compared element. This will bring the greatest element to the first index of the array. Then we will need to compare the second element with all the other following elements. And swap if the compared element is greater than the second element. This process continues till the last index of the array. When the swapping of elements is completed, then we can see that the array is sorted. The basic algorithm of sorting is just it but we also need to convert the concept to the actual program. That leads us to the next heading.
 

Program to sort an array in ascending order.

    // WAP in C to sort an array in ascending order
    #include <stdio.h>
    int main()
    {
        int a[10], temp_var;
        printf("Enter the elements of the array:n");
        for (int i = 0; i < 10; i++)
        {
            scanf("%d", &a[i]);
        }
        for (int i = 0; i < 10; i++)
        {
            for (int j = i + 1; j < 10; j++)
            {
                if (a[i] > a[j])
                {
                    temp_var = a[i];
                    a[i] = a[j];
                    a[j] = temp_var;
                }
            }
        }
        printf("Array in ascending order is:n");
        for (int i = 0; i < 10; i++)
        {
            printf("%dn", a[i]);
        }
        return 0;
    }
 
 
This is the program in C language to sort an array in ascending order. Here we will need to use different concepts like an array, looping, return statement, etc.
 

Program to sort an array in descending order.

 
    // Wap to enter an array an sort it in descending order
    #include <stdio.h>
    int main()
    {
        int a[10], temp;
        printf("Enter 10 numbersn");
        for (int i = 0; i < 10; i++)
        {
            scanf("%d", &a[i]);
        }
        for (int i = 0; i < 10; i++)
        {
            for (int j = i + 1; j < 10; j++)
            {
                if (a[i] < a[j])
                {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
        printf("Array in descending order  isn");
        for (int i = 0; i < 10; i++)
        {
            printf("%dn", a[i]);
        }
        return 0;
    }
 
Both the program of sorting in ascending and descending order is almost the same but with few changes.
 
The sorting algorithm is important from both an academic point of view and practical implementation. So everyone should know the concept of sorting array.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *