c program bubble sort

C program to Bubble Sort

c programming code for bubble sort to sort numbers or arrange them in ascending order

Simple Program:

#include <stdio.h>
#include<conio.h>
 
int main()
{
  int array[100], n, c, d, swap;
 
  printf("Enter number of elements\n");
  scanf("%d", &n);
 
  printf("Enter %d numbers\n", n);
 
  for (c = 0; c < n; c++)
    scanf("%d", &array);
 
  for (c = 0 ; c < ( n - 1 ); c++)
  {
    for (d = 0 ; d < n - c - 1; d++)
    {
      if (array[d] > array[d+1]) /* For decreasing order use < */
      {
        swap       = array[d];
        array[d]   = array[d+1];
        array[d+1] = swap;
      }
    }
  }
 
  printf("Sorted list in ascending order:\n");
 
  for ( c = 0 ; c < n ; c++ )
     printf("%d\n", array);
 
  return 0;
}

Output:
Enter number of elements:
5
Enter 5 numbers:
5
7
6
3
2
Sorted list in ascending order:
2
3
5
6
7

This is simple program of bubble sort using C program.

have any doubts please ask your question on our special community of Q&A section for programmers to gain
knowledge and sharing knowledge click here.

READ  c program for call by value

Leave A Comment

Your email address will not be published. Required fields are marked *