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

A C Program

The document describes bubble sort algorithm to sort an array of elements. It includes code to accept array size and values, then perform bubble sort by comparing adjacent elements and swapping them if out of order until fully sorted.

Uploaded by

darkdreamday
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

A C Program

The document describes bubble sort algorithm to sort an array of elements. It includes code to accept array size and values, then perform bubble sort by comparing adjacent elements and swapping them if out of order until fully sorted.

Uploaded by

darkdreamday
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

// This Program Sorts an array of elements using Bubble Sort

#include<stdio.h>
#include<conio.h>
void main()
{
int a[100];
int n;
int i;
int j;
int temp;
printf("Enter the size of the array");
scanf("%d",&n);
printf("Enter the values in the array\n");
for(i=0;i<n-1;i++)
{
scanf("%d",&a[i]);
}
//Now, the sorting starts, with:
//a variable, called as j, which begins at 0, the start of the array
//and compares itself, or the value it contains with the next ie j+1 variable of
the list, as is the main principle of bubble sort
//and, if it is greater, it swaps the postions, with help of a temp variable. it
stores its value in temp and then stores j+1 in j
//and, finally it exchanges the old value of j , now in temp with j+1
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
printf("The sorted array is: \n");
for(i=0;i<=n-1;i++)
{
printf("%d\n",a[i]);
}
getch();
}

You might also like