Get Update on recent Technology & Programming

Friday, 10 November 2017

Bubble sort In c

Posted by   on Pinterest





#include<stdio.h>

#define MAX 100

int main(void)

{

	int arr[MAX],i,j,temp,n,xchanges;

	printf("Enter the number of elements : ");

	scanf("%d",&n);

	for(i=0; i<n; i++)

	{

		printf("Enter element %d : ",i+1);

		scanf("%d",&arr[i]);

	}

	/*Bubble sort*/

	for(i=0; i<n-1 ;i++)

	{

		xchanges = 0;

		for(j=0; j<n-1-i; j++)

		{

			if(arr[j] > arr[j+1])

			{

				temp = arr[j];

				arr[j] = arr[j+1];

				arr[j+1] = temp;

				xchanges++;

			}

		}

		if(xchanges==0) /*If list is sorted*/

			break;

	}

	printf("Sorted list is :\n");

	for(i=0; i<n; i++)

		printf("%d ",arr[i]);

	printf("\n");

	return 0;

}







No comments:
Write comments

Hey, we've just launched a new custom color Blogger template. You'll like it -
Join Our Newsletter