alt

Sort an array in C | Ascending and Descending

Hi Guys. Welcome you all to the Digital Chalkie. Today I came with the new article for  “C Programming – How to do it” series. Today we are going to write a program to sort array elements in C programming. Before the start our tutorial I have to do a little request. If you haven’t liked our Facebook Page yet, please like it to support us. (FB Page: BMaxx Edu).

Method

In this event, we simply compare one number with other numbers. You can understand the method by reading the program code. The important thing is we use the ‘swap variable’ method in this event.  So we have to recall it at the beging.

int x=2,y=3;
int k; // interchange suppoter variable
x=k;     //k=2
x=y;    //x=3
y=k;    //y=2
Now the value of x=3 and y=2

Sort an array in Ascending Order

At first we are going to sort an array in c programming in ascending order. Here are the steps to do.

1. Create an array of fixed size or user-defined size
2. Iterate via for loop to take array elements as input, and print them.
3. Make a nested loop to sort the elements.
4. In the nested loop, each element will be compared to all the elements below it.
5. In case the element is greater than the element present below it, then they are interchanged. (using swap variable method)
6. After executing the nested loop, we can print ascending order arranged elements.

int size, i, j, temp;
    printf("Ascending and Descending\n");
    printf("Enter size of array: ");
    scanf("%d", &size);
    int num[size];
    printf("\n");

    for(i=0;inum[j])
            {
                temp=num[i];
                num[i]=num[j];
                num[j]=temp;
            }
        }
    }
    printf("Ascending Order of numbers : ");
    for(i=0;i

Sort in descending order

To sort in descending order, we have to do only a little change. In the above scenario, we used "greater than" logical operation. In this case we use less than operation for the same code.

int size, i, j, temp;
    int num[size];

    printf("Ascending and Descending\n");
    printf("Enter size of array: ");
    scanf("%d", &size);
    printf("\n");

    for(i=0;i<size;i++)
    {
        printf("Enter numbers to array  : ");
        scanf("%d",&num[i]);
    }
  
    for (i=0;i<size;i++)
    {
        for (j=i+1;j<size;j++)
        {
            if(num[i]<num[j])
            {
                temp=num[i];
                num[i]=num[j];
                num[j]=temp;
            }
        }
    }
    printf("Descending Order of numbers : ");
    for(i=0;i<size;i++)
    {
        printf("%d  ",num[i]);
    }

    return 0;
}
Bonomi Tharinda
Bonomi Tharinda is the Developer of the BMaxx Blog. He is a skilled web developer and graphic designer. In addition, he is an undergraduate student of a representative University in Sri Lanka. He is working as a tutor of an institute and as a part-time blogger, he will help you all to make your problems solved.Here are his social accounts and you can follow him through them.