C program sort array using Bubble Sort technique.

Posted by Mangesh on March 16, 2018

/* Bubble Sort */

Description :

Bubble sort is a comparison based sorting algorithm wherein comparing adjacent elements is a primitive operation. In each pass, it compares the adjacent elements in the array and exchanges those that are not in order. Basically, each pass through the array places the next largest value in its proper place, hence the number of comparisons reduces by one at each pass. In other words, each item “bubbles”up to the location where it is supposed to be in the sorted sequence. This invariant is maintained by the algorithm in each pass and hence, bubble sort correctly outputs the sorted sequence. For an n-element array, the below pseudocode requires n − i comparisons for the i th iteration (Pass).

Program :


void BubbleSort(int arr[],int n)
  int i,j,temp;
     if(arr[j] < arr[j-1])
       temp = arr[j];
       arr[j] = arr[j-1];
       arr[j-1] = temp;
void printarray(int arr[],int n)
int i;
      printf("%d ",arr[i]);

void main()
int arr[10];
int i,n;
    printf("Enter the total number of element: ");
      printf("Enter %d number in array:",i+1);
    printf("\n_________Before Bubble Sort__________\n\n");
    printf("\n\n_________After Bubble Sort__________\n\n");


Output :

C program for bubble sort` tested in Turbo C 3.2

Written with from Mangesh.

Related Post
1 C program sort array using Bubble Sort technique.
2 C program for sorting an array using Insertion Sort technique.
3 C program to sort an array using Selection Sort technique.
Latest Post
1 C program to implement Queue using linked list.
2 C program for binary search tree (BST).
3 C program to search an element in linked list.
4 C program for postorder traversal in binary tree.
5 C program for preorder traversal in binary tree.