Bubble Sort Algorithm in Data Structure using C Language | BCA SEM 2 BKNMU Junagadh

 



Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted. The algorithm gets its name because smaller elements "bubble" to the top of the list with each iteration.

Here's a basic outline of how the algorithm works:



  1. Start from the beginning of the list.
  2. Compare the first two elements. If the first element is greater than the second element, swap them.
  3. Move to the next pair of elements, comparing and swapping if necessary.
  4. Continue this process until the end of the list is reached.
  5. Repeat steps 1-4 until no more swaps are needed, indicating that the list is sorted.
Here's a simple example of Bubble Sort in action (using integers):

PPT :- Download


Initial list: 57, 32, 74, 2, 22

1st pass:

  • Compare 57 and 32, swap since 57 > 32: 32, 57, 74, 2, 22
  • Compare 57 and 74, no swap needed: 32, 57, 74, 2, 22
  • Compare 74 and 2, swap since 74 > 2: 32, 57, 2, 74, 22
  • Compare 74 and 22, swap since 74 > 22: 32, 57, 2, 22, 74

2nd pass:

  • Compare 32 and 57, no swap needed: 32, 57, 2, 22, 74
  • Compare 57 and 2, swap since 57 > 2: 32, 2, 57, 22, 74
  • Compare 57 and 22, swap since 57 > 22: 32, 2, 22, 57, 74
  • Compare 57 and 74, no swap needed: 32, 2, 22, 57, 74

3rd pass:

  • Compare 32 and 2, swap since 32 > 2: 2, 32, 22, 57, 74
  • Compare 32 and 22, no swap needed: 2, 22, 32, 57, 74
  • Compare 32 and 57, no swap needed: 2, 22, 32, 57, 74
  • Compare 57 and 74, no swap needed: 2, 22, 32, 57, 74

4th pass:

  • Compare 2 and 22, no swap needed: 2, 22, 32, 57, 74
  • Compare 22 and 32, no swap needed: 2, 22, 32, 57, 74
  • Compare 32 and 57, no swap needed: 2, 22, 32, 57, 74
  • Compare 57 and 74, no swap needed: 2, 22, 32, 57, 74

No more passes needed, the list is sorted.

So, the sorted list using Bubble Sort is: 2, 22, 32, 57, 74.


BubbleSort, SortingAlgorithm, Algorithm, Sorting, BubbleSortExample, SortedList, BubbleSortInAction, SortingMethod, BubbleSortExplanation, BubbleSortSteps


1. #BubbleSort 2. #SortingAlgorithm 3. #Algorithm 4. #Sorting 5. #BubbleSortExample 6. #SortedList 7. #BubbleSortInAction 8. #SortingMethod 9. #BubbleSortExplanation 10. #BubbleSortSteps



Example :

/*
C program to implement bubble sort
Sumit Sir, Brahmanand college chaparda Junagadh
*/
#include
int main()
{
int a[]={32,57,2,22,74};
int n=5,i,j,temp;
printf("Before the bubble sort\n");
for(i = 0; i < n; i++)
{
printf("%d ",a[i]);
}

for(i=0;ia[j+1]) { temp = a[j]; a[j]= a[j+1]; a[j+1] = temp; } } } printf("\nAfter the bubble sort\n"); for(i = 0; i < n; i++) { printf("%d ",a[i]); } return 0; } {
temp = a[j];
a[j]= a[j+1];
a[j+1] = temp;
}
}
}
printf("\nAfter the bubble sort\n");
for(i = 0; i < n; i++)
{
printf("%d ",a[i]);
}

return 0;
}



Post a Comment

Thanks for comment.

Previous Post Next Post