C Program to sort n numbers in ascending order


February 4, 2022, Learn eTutorial
1324

What is sorting?

Here’s a simple Program to Sort n numbers in ascending order. Sorting is a process of rearranging the numbers in an array or a list in some order, which will be ascending or descending order. Sorting is handy and has a lot of application-level usage in real life. In c programming, there are different kinds of sorting methods used depends on the technique used to sort the elements. Each method uses a different type of technique, and all have various complexities for both the best case and worst case. Some of the sorting ways in c are:

  • Bubble Sort
  • Selection Sort
  • Merge Sort
  • Insertion Sort
  • Quick Sort
  • Heap Sort

How do sort the numbers in ascending order?.

Ascending order means arranging the numbers from smallest to biggest. In this c program, we need to put the elements of an array in ascending order. For example, if we have a set of numbers like '1, 5, 3, 8, 7, 6'. we need to change it to '1, 3, 5, 6, 7, 8' that is sorted in ascending order.

In this c program, first, we accept the count of the numbers the user wants to check. Then we get the numbers from the user using for loop and save it into the array number[]. By using a nested 'for loop', we check each element of the array with the next element; if the number is greater than the following number, then we will swap the numbers using a temporary variable. Finally, after completing iterations of the for loop, display the sorted array using for loop.

ALGORITHM

STEP 1: Include the header files to use the built-in functions in the C program.

STEP 2: Declare the integer variables i, j, a, n, and the Array number[30].

STEP 3: Read the value of N into the variable 'n'.

STEP 4: Read the numbers into the Array number[i] using for loop.

STEP 5: Set i=0.

STEP 6: Check i

STEP 7: set j=i+1.

STEP 8: Check if number[i] > number[j] if true then Swap the numbers.

STEP 9: Increment 'j' by 1 and do step 7.

STEP 10: Increment 'i' by 1 do step 6.

STEP 11: Display the numbers arranged in ascending order as the numbers in the Array number[] using for loop.

C Source Code

                                          #include <stdio.h>

void main()
{

  int i, j, a, n, number[30];
  printf("Enter the value of N\n");
  scanf("%d", & n);
  printf("Enter the numbers \n"); /* accept the elements from the user */
 for (i = 0; i < n; ++i)
  {
       scanf("%d", & number[i]);
  }
  for (i = 0; i < n; ++i)
  {
    for (j = i + 1; j < n; ++j)
    {
      if (number[i] > number[j]) /* swapping the elements in the ascending order */
      {
        a = number[i];
        number[i] = number[j];
        number[j] = a;
      }
    }
  }

  printf("The numbers arranged in ascending order are given below\n"); /* display the output  */

  for (i = 0; i < n; ++i)
    printf("%d\n", number[i]);

} /* End of main() */
                                      

OUTPUT

Enter the value of N
5

Enter the numbers
80
20
67
10
45

The numbers arranged in ascending order are given below
10
20
45
67
80