C Program to Sort the Array in an Ascending Order

This C Program sorts array in an ascending order.

Here is source code of the C program to sort the array in an ascending order. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

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


$ cc pgm66.c
$ a.out
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 Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Arrays, go to C Programming Examples on Arrays. If you wish to look at programming examples on all topics, go to C Programming Examples.
Subscribe Newsletter & Posts
About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.

Subscribe Newsletter & Posts
Follow Manish & Sanfoundry