How to write a C Program to Bubble Sort in C Programming Language ?
Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller elements "bubble" to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort. It can be practical if the input is usually in sort order but may occasionally have some out-of-order elements nearly in position.See More: Bubble sort
Solution For C Program :
//BUBBLE SORT
#include<stdio.h>
main()
{
int a[10],i,n;
clrscr();
printf("enter how many elements do u want to enter: ");
scanf("%d",&n);
printf("enter the array elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
bub_sort(a,n);
getch();
}
bub_sort(int a[],int n)
{
int i,j,temp,limit;
limit=n-1;
for(i=0;i<limit;i++)
{
for(j=0;j<limit-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("final sorted array is: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
}
Learn More :
Sort
- Sort Three Numbers - program reads in three Integers and displays them in ascending order.
- C Program Sort Example
- C Program Shell Sort Using C Programming Language
- C Program Selection Sort Using C Programming Language
- C Program Insertion Sort Using C Programming Language
- Heap Sort Using C Programming Language
- C Program To Sort An Array In Ascending And Descending Order
- C Program To Sort An Array Of Names In Alphabetical And Reverse Order
- C Program Sort Array By Segment
- C Program to sort an array using bubble sort
- C Program to merge and sort two arrays
- Un-sortiertes Array and Sortiertes Array
- matrix sort in C Program
- C program that receives 10 float numbers from the console and sort them in non-ascending order, and prints the result
- C Program to accept 5 names from user & store these names into an array. Sort these array elements in alphabetical order
- C Program to accept n numbers from user,store these numbers into an array & sort the number of an array
- C Program to Implement Quick Sort
- QuickVSInsertion Sorting C Program