FaltuTech.Club : Fane of Advanced Logical Thriving Utopian Technical Club

Bubble Sorting In C (C-Code)

Note: Should not be used to for sorting large data.

#include < stdio.h >

#include < conio.h >

#define size 20 // size of array

void main() {
  int i, j, temp, elements[size], n;
  clrscr();
  printf("Enter the number of elements\t"); // Enter the number of total elements to be sorted
  scanf("%d", & n);
  for (i = 0; i < n; i++) {                                     // loop to get the numbers from user
    printf("Enter the element %d\t", i + 1);
    scanf("%d", & elements[i]);
  }
  for (i = 0; i < n; i++) {                                     // loop to sort
    for (j = 0; j < n; j++) {
      if (elements[i] > elements[j]) {                   // if condition is true than swap the numbers
        temp = elements[i];
        elements[i] = elements[j];
        elements[j] = temp;

      }
    }
  }

  for (i = 0; i < n; i++) {                                     // loop to show sorted numbers
    printf("%d", elements[i]);
  }
  getch();
}