C Program to Sort an Array in Ascending Order

An array is a collection of similar data elements stored in a contiguous memory location.

Problem Description

This program will implement a one-dimentional array of some fixed size, filled with some random numbers, then will sort all the filled elements of the array.

Problem Solution

1. Create an array of fixed size (maximum capacity), lets say 10.
2. Take n, a variable which stores the number of elements of the array, less than maximum capacity of array.
3. Iterate via for loop to take array elements as input, and print them.
4. The array elements are in unsorted fashion, to sort them, make a nested loop.
5. In the nested loop, the each element will be compared to all the elements below it.
6. In case the element is greater than the element present below it, then they are interchanged
7. After executing the nested loop, we will obtain an array in ascending order arranged elements.

Program/Source Code

Here is source code of the C program to sort the array in an ascending order. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1.  
  2.     /*
  3.      * C program to accept N numbers and arrange them in an ascending order
  4.      */
  5.  
  6.     #include <stdio.h>
  7.     void main()
  8.     {
  9.  
  10.         int i, j, a, n, number[30];
  11.         printf("Enter the value of N \n");
  12.         scanf("%d", &n);
  13.  
  14.         printf("Enter the numbers \n");
  15.         for (i = 0; i < n; ++i)
  16.             scanf("%d", &number[i]);
  17.  
  18.         for (i = 0; i < n; ++i) 
  19.         {
  20.  
  21.             for (j = i + 1; j < n; ++j)
  22.             {
  23.  
  24.                 if (number[i] > number[j]) 
  25.                 {
  26.  
  27.                     a =  number[i];
  28.                     number[i] = number[j];
  29.                     number[j] = a;
  30.  
  31.                 }
  32.  
  33.             }
  34.  
  35.         }
  36.  
  37.         printf("The numbers arranged in ascending order are given below \n");
  38.         for (i = 0; i < n; ++i)
  39.             printf("%d\n", number[i]);
  40.  
  41.     }
Program Explanation

1. Declare an array of some fixed capacity, lets say 30.
2. From users, take a number N as input, which will indicate the number of elements in the array (N <= maximum capacity)
3. Iterating through for loops (from [0 to N) ), take integers as input from user and print them. These input are the elements of the array.
4. Now, create a nested for loop with i and j as iterators.
5. Start the sorting in ascending order by extracting each element at position i of outer loop.
6. This element is being compared to every element from position i+1 to size-1 (means all elements present below this extracted element)
7. In case any of the extracted element is greater than the element below it, then these two interchange their position, else the loop continues.
8. After this nested loop gets executed, we get all the elements of the array sorted in ascending order.

advertisement
Runtime Test Cases
 
Enter the value of N
6
Enter the numbers
3
78
90
456
780
200
The numbers arranged in ascending order are given below
3
78
90
200
456
780

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

Free 30-Day Python Certification Bootcamp is Live. Join Now!

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.