C Program to Sort N Numbers in Ascending Order using Bubble Sort

This C Program sorts the numbers in ascending order using bubble sort. Bubble sort is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. Here we need to sort a number in ascending order.

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

  1. /*
  2.  * C program to sort N numbers in ascending order using Bubble sort
  3.  * and print both the given and the sorted array
  4.  */
  5. #include <stdio.h>
  6. #define MAXSIZE 10
  7.  
  8. void main()
  9. {
  10.     int array[MAXSIZE];
  11.     int i, j, num, temp;
  12.  
  13.     printf("Enter the value of num \n");
  14.     scanf("%d", &num);
  15.     printf("Enter the elements one by one \n");
  16.     for (i = 0; i < num; i++)
  17.     {
  18.         scanf("%d", &array[i]);
  19.     }
  20.     printf("Input array is \n");
  21.     for (i = 0; i < num; i++)
  22.     {
  23.         printf("%d\n", array[i]);
  24.     }
  25.     /*   Bubble sorting begins */
  26.     for (i = 0; i < num; i++)
  27.     {
  28.         for (j = 0; j < (num - i - 1); j++)
  29.         {
  30.             if (array[j] > array[j + 1])
  31.             {
  32.                 temp = array[j];
  33.                 array[j] = array[j + 1];
  34.                 array[j + 1] = temp;
  35.             }
  36.         }
  37.     }
  38.     printf("Sorted array is...\n");
  39.     for (i = 0; i < num; i++)
  40.     {
  41.         printf("%d\n", array[i]);
  42.     }
  43. }

 
$ cc pgm21.c
$ a.out
Enter the value of num
6
Enter the elements one by one
23
45
67
89
12
34
Input array is
23
45
67
89
12
34
Sorted array is...
12
23
34
45
67
89

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best 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.

If you find any mistake above, kindly email to [email protected]

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
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.