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 shubhanshu
2 test
3 C program for Breadth First Search.
4 C program for Depth First Search(DFS).
5 C for circular queue using linked list.