Tweet
#include<iostream>
#include<stdio.h>
void swap(int *a,int x,int y)
{
int temp=*(a+x);
*(a+x)=*(a+y);
*(a+y)=temp;
}
void bubble_sort(int a[],int n)
{
int i,j;
for(i=0;i<n-1;i++)
for(j=0;j<n-1-i;j++)
if(a[j]>a[j+1])
swap(a,j,j+1);
}
main()
{ int i,n;
std::cout<<"Program to sort using bubblesort :"<<"\n";
std::cout<<"Enter array capacity :" ;
std::cin>>n;
int a[n];
std::cout<<"Enter the numbers : "<<"\n";
for(i=0;i<n;i++)
std::cin>>a[i];
printf("\nGIVEN ARRAY : ");
for(i=0;i<n;i++)
std::cout<<a[i]<<" ";
printf("\n\nSORTED ARRAY : ");
bubble_sort(a,n);
for(i=0;i<n;i++)
std::cout<<a[i]<<" ";
std::cout<<"\n\n";
}
Bubble Sort in C++
Posted by
LAHAUL SETH
~
Bubble Sort in C++
2011-11-29T11:11:00+05:30
LAHAUL SETH
C++
|
cplusplus
|
Sorting
|
Comments
Bubble Sort in C++
2011-11-29T11:11:00+05:30
LAHAUL SETH
C++
|
cplusplus
|
Sorting
|