

![]()
Let A be the vector that needs sorting and n its length. The bubble sort has n-1 steps. The first step is comparing A[1] with A[2], A[2] with A[3].... A[n] with A[n-1]. After this step the biggest element is places on the last position. The k-th step will be comparing A[1] with A[2], A[2] with A[3].... A[n-k] with A[n-k+1].
Pascal program:
var a:array[1..100] of integer;
i,j,k,n:integer;
begin
readln(n);
for i:=1 to n do
readln(a[i]);
for i:=1 to n-1 do
for j:=1 to n-i do
if a[j]>a[j+1] then begin
k:=a[j];
a[j]:=a[j+1];
a[j+1]:=k;
end;
for i:=1 to n do
write(a[i],' ');
end.
C++ program:
#include<iostream.h>
void main()
{
int a[100],i,j,k,n;
cin>>n;
for (i=1;i<=n;i++)
cin>>a[i];
for (i=1;i<=n-1; i++)
{
for (j=1;j<=n-i;j++)
if (a[j]>a[j+1])
{
k=a[j];
a[j]=a[j+1];
a[j+1]=k;
}
}
for (i=1;i<=n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
Time: O(n^2)