Tag Archives: bubblesort c code

BubbleSort

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 wrong order. We make several passes over the list till no swaps are needed, which indicates that the list is sorted.For more information on bubble sort visit http://en.wikipedia.org/wiki/Bubble_sort.

Below is the C++ code for bubble sort algorithm.
CODE:

/*
 http://randomtechbits.in/
*Program to sort numbers using BUBBLESORT
*inputs: N- number of integers to sort ,A-array of integers
*output: sorted array of integers
*code tested on devc++ IDE
*/
#include<iostream>
#include<conio.h>
using namespace std;

main()
{
int N,temp;
int *A;
int i,j;
cout<<"Enter the number of elements in array:\n";
cin>>N;
A=new int[N];
cout<<"Enter the array\n";
for(i=0;i<N;i++)
cin>>A[i];
for(i=0;i<N-1;i++)
{
for(j=0;j<N-1-i;j++)
 {
 if(A[j]>A[j+1])
  {int t;
  t=A[j];
  A[j]=A[j+1];
  A[j+1]=t;
  }
 }
}
cout<<"\nThe Sorted array after BUBBLE SORT:"<<"\n";
for(i=0;i<N;i++)
 cout<<"\t"<<A[i];
//for more codes visit Code Library
getch();
}