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 .
- #include <stdio.h>
- void bubbleSort(int arr[], int n) {
- int i, j;
- for (i = 0; i < n - 1; i++) {
- // Last i elements are already in place
- for (j = 0; j < n - i - 1; j++) {
- // Swap if the element found is greater than the next element
- if (arr[j] > arr[j + 1]) {
- int temp = arr[j];
- arr[j] = arr[j + 1];
- arr[j + 1] = temp;
- }
- }
- }
- }
- int main() {
- int arr[] = {5, 2, 8, 1, 4, 9, 7}; // user place any list .
- int n = sizeof(arr) / sizeof(arr[0]);
- bubbleSort(arr, n);
- printf("Sorted array: ");
- for (int i = 0; i < n; i++) {
- printf("%d ", arr[i]);
- }
- printf("\n");
- return 0;
- }
- OUTPUT :
- Sorted array: 1 2 4 5 7 8 9
2. Program of Bubble sort using Python.
- def bubble_sort(arr):
- n = len(arr)
- # Traverse through all array elements
- for i in range(n):
- # Last i elements are already in place
- for j in range(0, n-i-1):
- # Swap if the element found is greater than the next element
- if arr[j] > arr[j+1]:
- arr[j], arr[j+1] = arr[j+1], arr[j]
- return arr
- my_list = [5, 2, 8, 1, 4, 6, 9, 7, 3] # user place any list
- sorted_list = bubble_sort(my_list)
- print(sorted_list)
- OUTPUT :
- Sorted_list is = [1, 2, 3, 4, 5, 6, 7, 8, 9]