Sort numbers using bubble sort - C Program

C program to sort 'n' numbers in ascending order using bubble sort.

Solution:

  • Bubble sort is used to sort 'n' numbers in ascending order.
  • To sort these 'n' numbers start with 0th position element in array and compare it with a 1st position element. If 0th position element is found greater than 1stposition element, swap the elements. Similarly, compare all the elements in array.

#include<stdio.h>
#define maxsize 20
int main()
{
      int array[maxsize];
      int i, j, num, t;
      printf("How many numbers you want to enter : ");
      scanf("%d", &num);
      printf("\nEnter %d numbers :\n",num);
      for (i = 0; i < num; i++)
      {
            scanf("%d", &array[i]);
      }
      printf("\nInput array is : \n\n");
      for (i = 0; i < num; i++)
      {
            printf("%d ", array[i]);
      }
      /* Bubble sorting begins */
      for (i = 0; i < num; i++)
      {
            for (j = 0; j < (num - i - 1); j++)
            {
                  if (array[j] > array[j + 1])
                  {
                        t = array[j];
                        array[j] = array[j + 1];
                        array[j + 1] = t;
                  }
            }
      } // end of sorting
      printf("\n\nSorted array is : \n\n");
      for (i = 0; i < num; i++)
      {
            printf("%d ", array[i]);
      }
      return 0;
}


Output:

bubble sort