Bubble Sort Program - C/C++

Bubble sort, sometimes incorrectly referred to as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping 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 gets its name from the way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort. Although the algorithm is simple, most of the other sorting algorithms are more efficient for large lists.



The implementation of Bubble sort is given below. 
Give your opinion  below in comment section.
Subscribe to this blog.



#include<stdio.h>
#include<conio.h>
void main()
{
 int a[10], i, j, temp;
 clrscr();
 printf("Enter Element");
 for (i = 0; i < 10; i++)
 {
  printf("\nEnter [%d] element", i + 1);
  scanf("%d", &a[i]);
 }
 for (i = 0; i < 10; i++)
 {
  for (j = i + 1; j<10; j++)
  {
   if (a[i]>a[j])
   {
    temp = a[i];
    a[i] = a[j];
    a[j] = temp;
   }
   printf("\n");
  }
 }
 printf("After Sorting");
 for (i = 0; i < 10; i++)
 {
  printf("\nEnter [%d] element:- %d", i + 1, a[i]);
 }
 getch();
}

 
 
Check out other methods of sorting in Data Structure :
1. Implementation of Insertion Sort in C
2. Implementation of Selection Sort in C
3. Implementation of Quick Sort in C
4. Implementation of Bubble Sort in C
5. Implementation of Heap sort in C
6. Implementation of Merge Sort in C
7. Implementation of Radix Sort in C

1 comments :

Post a Comment