#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; }
Friday, 10 November 2017
Bubble sort In c
Similar Posts
C Program to find Prime numbers using sieve Algorithm
C Program of merging two sorted arrays into a third sorted array
C Program of sorting using insertion sort
C Program to insert an item in a sorted array at the proper place by shifting other elements to the right
C Program to insert an item in an array at a specified index by moving other elements to the right
Selection sort In C
About Admin
THE COOL PROGRAMMER
Subscribe to:
Post Comments (Atom)
No comments:
Write comments