Notification texts go here Contact Us Click here

Bubble sort in Data structure with Program

Please wait 0 seconds...
Scroll Down and click on Go to Link for destination
Congrats! Link is Generated




Bubble sort  is a simple sorting algorithm that repeatedly steps through a list, compares adjacent elements, and swaps them if they are in the wrong order. The process is repeated until the list is sorted. 

The bubble sort algorithm :

1. Start with an unsorted list of elements.

2. Compare the first element with the second element. If the first element is greater than the second element, swap them.

3. Move to the next pair of adjacent elements and compare them. Continue this process until you reach the end of the list.

4. At this point, the largest element will be at the end of the list.

5. Repeat steps 2-4 for the remaining elements, excluding the last element that is already sorted.

6. Continue this process until the entire list is sorted.


Here's an example of bubble sort in action. Let's say we have an unsorted list of numbers: [5, 2, 8, 1, 4].


Pass 1:

- Compare 5 and 2: Since 5 is greater, swap them. List becomes [2, 5, 8, 1, 4].

- Compare 5 and 8: No swapping required. List remains [2, 5, 8, 1, 4].

- Compare 8 and 1: Since 8 is greater, swap them. List becomes [2, 5, 1, 8, 4].

- Compare 8 and 4: Since 8 is greater, swap them. List becomes [2, 5, 1, 4, 8].


Pass 2:

- Compare 2 and 5: No swapping required. List remains [2, 5, 1, 4, 8].

- Compare 5 and 1: Since 5 is greater, swap them. List becomes [2, 1, 5, 4, 8].

- Compare 5 and 4: Since 5 is greater, swap them. List becomes [2, 1, 4, 5, 8].


Pass 3:

- Compare 2 and 1: Since 2 is greater, swap them. List becomes [1, 2, 4, 5, 8].

- Compare 2 and 4: No swapping required. List remains [1, 2, 4, 5, 8].


Pass 4:

- Compare 1 and 2: No swapping required. List remains [1, 2, 4, 5, 8].


The final sorted list is [1, 2, 4, 5, 8]. Bubble sort has a time complexity of  O(n^2), where n is the number of elements in the list. It is not an efficient algorithm for large lists, but it is easy to understand and implement.


1. Program of Bubble sort using C Language .

  1. #include <stdio.h>
  2.  
  3. void bubbleSort(int arr[], int n) {
  4.     int i, j;
  5.     for (= 0; i < n - 1; i++) {
  6.         // Last i elements are already in place
  7.         for (= 0; j < n - i - 1; j++) {
  8.             // Swap if the element found is greater than the next element
  9.             if (arr[j] > arr[+ 1]) {
  10.                 int temp = arr[j];
  11.                 arr[j] = arr[+ 1];
  12.                 arr[+ 1] = temp;
  13.             }
  14.         }
  15.     }
  16. }
  17.  
  18. int main() {
  19.     int arr[] = {5, 2, 8, 1, 4, 9, 7};          // user place any list .
  20.     int n = sizeof(arr) / sizeof(arr[0]);
  21.  
  22.     bubbleSort(arr, n);
  23.  
  24.     printf("Sorted array: ");
  25.     for (int i = 0; i < n; i++) {
  26.         printf("%d ", arr[i]);
  27.     }
  28.     printf("\n");
  29.  
  30.     return 0;
  31. }
  32.  
  33. OUTPUT :
  34.         Sorted array: 1 2 4 5 7 8 9 
  35.  


2. Program of Bubble sort using Python.


  1. def bubble_sort(arr):
  2.     n = len(arr)
  3.  
  4.     # Traverse through all array elements
  5.     for i in range(n):
  6.         # Last i elements are already in place
  7.         for j in range(0, n-i-1):
  8.             # Swap if the element found is greater than the next element
  9.             if arr[j] > arr[j+1]:
  10.                 arr[j], arr[j+1] = arr[j+1], arr[j]
  11.  
  12.     return arr
  13.  
  14.  
  15. my_list = [5, 2, 8, 1, 4, 6, 9, 7, 3]      # user place any list 
  16. sorted_list = bubble_sort(my_list)
  17. print(sorted_list)
  18.  
  19. OUTPUT :
  20.        Sorted_list is =  [1, 2, 3, 4, 5, 6, 7, 8, 9]

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.