Sort numbers using Bubble Sort

Input numbers in an array and sort them using the bubble sort technique.. 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. 

Ascending Order

/*
Bubble Sort Ascending
Shaswat Saxena
10/11/2017
*/
#include <stdio.h>
#include <conio.h>
void main ()
{
  int i,j,size,temp;
  clrscr();
  printf("Enter the total numbers you will enter :\n");
  scanf("%d",&size);
  int num[size];
  for (i=0; i < size; i++) {
    printf("Enter the  number\n");
    scanf("%d",&num[i]);
  }
    //Arrange in Ascending Order
  for (i=0 ;i < size; i++)
    {
      for ( j = 0; j < (size - i - 1); j++) {
        if (num[j]>num[j+1]) {
          temp=num[j];
          num[j]=num[j+1];
          num[j+1]=temp;
        }
      }
    }
  printf("Sorted Numbers in Ascending Order are :\n");
  for ( i = 0; i < size; i++) {
    printf("%d\n",num[i]);
  }
  getch();
}

Output

Descending Order

/*
Bubble Sort Descending
Shaswat Saxena
10/11/2017
*/
#include <stdio.h>
#include <conio.h>
void main ()
{
  int i,j,size,temp;
  clrscr();
  printf("Enter the total numbers you will enter :\n");
  scanf("%d",&size);
  int num[size];
  for (i=0; i < size; i++) {
    printf("Enter the  number\n");
    scanf("%d",&num[i]);
  }
    //Arrange in Descending Order
  for (i=0 ;i < size; i++)
    {
      for ( j = 0; j < (size - i - 1); j++) {
        if (num[j] < num[j+1]) {
          temp=num[j];
          num[j]=num[j+1];
          num[j+1]=temp;
        }
      }
    }
  printf("Sorted Numbers in Descending Order are :\n");
  for ( i = 0; i < size; i++) {
    printf("%d\n",num[i]);
  }
  getch();
}

Output

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.

Recommended for You  Find the smallest of 3 numbers

This Post Has One Comment

  1. Thanks..? ?

Leave a Reply

Close Menu