If you need to create an array by taking user's input and then sort the elements of an array in ascending order in C, then you have landed on the right place, I am going to provide you code with an explanation for sorting an array in ascending order in C

Let's understand the Algorithm to do this in C language:

  • Create a fixed size array, let's say 50, then ask the user the number of elements user want to sort & enter it into an array, let's say the size of an element is N
  • Get all the number from the user until we reach N(means need to loop here for N number for times using scanf).
  • At present, the array elements are in unsorted fashion, to sort them, make a nested loop.
  • In the nested loop, each element will be compared to all the elements below it.
  • In case the element is greater than the element present below it, then they are interchanged using a temporary variable
  • After executing the nested loop, we will obtain an array in ascending order arranged elements.

Now, we have the algorithm, let's create the program for sorting array elements in ascending order

#include <stdio.h>
#define MAX_SIZE 50 
void main()
{
   int i, count, arr[MAX_SIZE];
 
   printf("Enter the number of elements you want to sort:");
   scanf("%d", &count);
   printf("\nEnter the numbers:");
   
   for (i = 0; i < count; ++i)
   {
      scanf("%d", &arr[i]);
   }
 
   array_sort_ascending_order(arr, count);
}
void array_sort_ascending_order(int arr[], int count)
{
   int temp, i, j, k;
   for (j = 0; j < count; ++j)
   {
      for (k = j + 1; k < count; ++k)
      {
         if (arr[j] > arr[k])
         {
            temp = arr[j];
            arr[j] = arr[k];
            arr[k] = temp;
         }
      }
   }
   printf("Array in ascending order now:\n");
   for (i = 0; i < count; ++i)
   {
      printf("%d\n", arr[i]);
   }
}

In the above program, we have separated the main program logic and created a separate method array_sort_ascending_order, which takes array and number of elements in the array.

This method loop's each element and checks all another element of an array, if the adjacent element is less than the current element, interchange the position, that's it, we get the sorted array and print it.

Output:

Enter the number of elements you want to sort:5                                                                                                            
                                                                                                                                                           
Enter the numbers:11                                                                                                                                       
5                                                                                                                                                          
88                                                                                                                                                         
6                                                                                                                                                          
22                                                                                                                                                         
Array in ascending order now:                                                                                                                              
5                                                                                                                                                          
6                                                                                                                                                          
11                                                                                                                                                         
22                                                                                                                                                         
88 

ascending-order-program-in-c-min.png

The above method, Is also know as Bubble sort.

Bubble sort algorithm for sorting array is an easy method. However, this becomes slow when there are large data items to be sorted.

Sorting array in ascending order in C using Selection sort

#include<stdio.h>
#include<conio.h>
#define MAX_SIZE 50  
void main()
{
    int a[MAX_SIZE],n,i,j,min,temp;
    clrscr();
     
    printf("\n Enter the Number of Elements: ");
    scanf("%d",&n);
     
    printf("\n Enter %d Elements: ",n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
     
    for(i=0;i<n-1;i++)
    {
        min=i;
        for(j=i+1;j<n;j++)
        {
            if(a[min]>a[j])
            min=j;
        }
        if(min!=i)
        {
            temp=a[i];
            a[i]=a[min];
            a[min]=temp;
        }
    }
     
    printf("\n The Sorted array in ascending order: ");
    for(i=0;i<n;i++)
    {
        printf("%d ",a[i]);
    }
    getch();
}

Explanation:

We are using similar method, to get array elements from the using the below line of code

  printf("\n Enter the Number of Elements: ");
    scanf("%d",&n);
     
    printf("\n Enter %d Elements: ",n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }

Main code of this selection sort is explained here, using the nested for loops it finds finds the smallest element in the array and swaps with the first element then with the second element and continues until the entire array is sorted using the following lines:

 for(i=0;i<n-1;i++)
    {
        min=i;
        for(j=i+1;j<n;j++)
        {
            if(a[min]>a[j])
            min=j;
        }
        if(min!=i)
        {
            temp=a[i];
            a[i]=a[min];
            a[min]=temp;
        }
    }

Executing the above program on online compiler gives me result

Enter the Number of Elements: 5                                                                                                       
                                                                                                                                       
Enter 5 Elements: 11                                                                                                                  
23                                                                                                                                     
15                                                                                                                                     
10                                                                                                                                     
9                                                                                                                                      
                                                                                                                                       
 The Sorted array in ascending order: 9 10 11 15 23    

selection-sort-in-c-min.png


There are several other methods of sorting in C like Merge sort, heap sort, Quick Sort etc, we will be discussing this in another article, feel free to ask questions related to this article, in comment's section or in questions section.