0% found this document useful (0 votes)
27 views

Bubble Sort

The document describes the bubble sort algorithm which sorts elements in an array by repeatedly comparing adjacent elements and swapping them if they are in the wrong order. It involves multiple passes through the array, swapping adjacent elements until it is fully sorted.

Uploaded by

userbroadband
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Bubble Sort

The document describes the bubble sort algorithm which sorts elements in an array by repeatedly comparing adjacent elements and swapping them if they are in the wrong order. It involves multiple passes through the array, swapping adjacent elements until it is fully sorted.

Uploaded by

userbroadband
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

Bubble Sort:

If n elements are to be sorted in ascending order, the steps are:


1. First compare the 1st and 2nd element of array
1.1 if 1st > 2nd then interchanges the values of 1st and 2nd
2. Now compare the values of 2nd with the 3rd.
3. Similarly compare until the N-1th element is compared with Nth element.
4. Now the highest value element is reached at the Nth place.
5. Repeat until n-1 elements are compared

#include<stdio.h>
#define MAX 10 /* Number of elements to be sorted */
int main()
{
int a[MAX],i,j,temp;
printf("Enter %d elements\n",MAX);
for(i=0;i<MAX;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<MAX;i++)
{
for(j=0;j<MAX-1;j++)
{
if(a[j]>a[j+1]) /* For descending order, check for less than */
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("The sorted elements are \n");
for(i=0;i<MAX;i++)
printf("%d\n",a[i]);
return 0; /* Exit the program */
}

You might also like