Skip to main content

Bubble Sort

  

Bubble sort is a simple sorting algorithm that works by repeatedly swapping adjacent elements if they are in the wrong order. It is named after the way bubbles rise to the top of a liquid, as it sorts the array by repeatedly moving the largest or smallest elements to the top of the array.

The algorithm works by iterating through the array, comparing each adjacent pair of elements and swapping them if they are in the wrong order. This process is repeated until the array is fully sorted.

Let's say we have an array of integers [5, 3, 8, 4, 2]. The first pass of the algorithm would compare 5 and 3, swap them to get [3, 5, 8, 4, 2]. It would then compare 5 and 8 and not swap them, as they are in the correct order. It would then compare 8 and 4, swap them to get [3, 5, 4, 8, 2]. It would then compare 8 and 2, swap them to get [3, 5, 4, 2, 8]. The first pass is now complete, and the largest element, 8, is at the end of the array.

The second pass would start with comparing 3 and 5, not swapping them. It would then compare 5 and 4, swap them to get [3, 4, 5, 2, 8]. It would then compare 5 and 2, swap them to get [3, 4, 2, 5, 8]. It would then compare 5 and 8, not swapping them. The second pass is now complete, and the second largest element, 5, is in its correct position.

The third pass would start with comparing 3 and 4, not swapping them. It would then compare 4 and 2, swap them to get [3, 2, 4, 5, 8]. It would then compare 4 and 5, not swapping them. It would then compare 5 and 8, not swapping them. The third pass is now complete, and the third largest element, 4, is in its correct position.

The fourth pass would start with comparing 3 and 2, swap them to get [2, 3, 4, 5, 8]. It would then compare 3 and 4, not swapping them. It would then compare 4 and 5, not swapping them. It would then compare 5 and 8, not swapping them. The fourth pass is now complete, and the array is fully sorted.

Bubble sort has a worst-case time complexity of O(n^2), which means it is not efficient for large arrays. However, it is simple to understand and implement, making it useful for small arrays or as a teaching tool.

ALGORITHM:

The algorithm of bubble sort can be described in the following steps:

  1. Start by iterating through the array from the first element to the second last element. This will be used to compare each element with its adjacent element.
  2. For each pair of adjacent elements, compare them and swap them if they are in the wrong order. In other words, if the first element is greater than the second element, swap them.
  3. After the first iteration, the largest element will be at the end of the array. Repeat the process for the remaining unsorted elements, from the first element to the second last element again.
  4. Continue this process until the entire array is sorted.
  5. The sorted array is now ready for use.

PSEUDO CODE:

Here's the algorithm of bubble sort in pseudocode:


In this algorithm, we use two nested loops to iterate through the array and compare each adjacent element. The outer loop iterates from the first element to the second last element, and the inner loop iterates from the first element to the second last element minus the number of iterations of the outer loop. This is because, after each iteration of the outer loop, the largest element is moved to the end of the array, and we don't need to compare it again.

The swap function is used to swap the two elements if they are in the wrong order. This function can be implemented using a temporary variable or without it using bitwise operators.

Bubble sort is a simple sorting algorithm, but it has a worst-case time complexity of O(n^2), which makes it inefficient for large arrays. However, it is easy to understand and implement, and can be useful for small arrays or as a teaching tool.

PYTHON CODE:

Here's an example Python code implementation of the Bubble Sort algorithm:


In this code, the bubble_sort() function takes an array arr as input and performs the bubble sort algorithm on it. The function uses two nested loops to traverse through all array elements and compare adjacent elements to determine whether a swap is needed. If a swap is needed, the function exchanges the two elements using the Python tuple syntax (a, b) = (b, a).

To test the code, we create an example array arr and call the bubble_sort() function on it. After the function completes, we print out the sorted array.

JAVA CODE:

Here's an example Java code implementation of the Bubble Sort algorithm:


In this code, the bubbleSort() method takes an integer array arr as input and performs the bubble sort algorithm on it. The method uses two nested loops to traverse through all array elements and compare adjacent elements to determine whether a swap is needed. If a swap is needed, the method exchanges the two elements using a temporary variable temp.

To test the code, we create an example integer array arr and call the bubbleSort() method on it. After the method completes, we print out the sorted array using the Arrays.toString() method to display the array as a string.

C CODE:

Here's an example C code implementation of the Bubble Sort algorithm:



In this code, the bubble_sort() function takes an integer array arr and its length n as input and performs the bubble sort algorithm on it. The function uses two nested loops to traverse through all array elements and compare adjacent elements to determine whether a swap is needed. If a swap is needed, the function exchanges the two elements using a temporary variable temp.

To test the code, we create an example integer array arr and call the bubble_sort() function on it, passing its length as a parameter. After the function completes, we print out the sorted array using a for loop to display each element in the array.

C++ CODE:

Here's an example C++ code implementation of the Bubble Sort algorithm:



In this code, the bubble_sort() function takes an integer array arr and its length n as input and performs the bubble sort algorithm on it. The function uses two nested loops to traverse through all array elements and compare adjacent elements to determine whether a swap is needed. If a swap is needed, the function exchanges the two elements using a temporary variable temp.

To test the code, we create an example integer array arr and call the bubble_sort() function on it, passing its length as a parameter. After the function completes, we print out the sorted array using a for loop to display each element in the array. We use the cout object from the iostream library to print the output.

JAVASCRIPT CODE:

Here's an example JavaScript code implementation of the Bubble Sort algorithm:


In this code, the bubbleSort() function takes an array arr as input and performs the bubble sort algorithm on it. The function uses two nested loops to traverse through all array elements and compare adjacent elements to determine whether a swap is needed. If a swap is needed, the function exchanges the two elements using a temporary variable temp.

To test the code, we create an example array arr and call the bubbleSort() function on it. After the function completes, we print out the original and sorted arrays using the console.log() method to display the arrays as strings in the console.

Comments

Popular posts from this blog

Selection Sort

Selection sort is a simple sorting algorithm that sorts an array by repeatedly finding the minimum element from the unsorted portion of the array and swapping it with the first element of the unsorted portion. It is called selection sort because it repeatedly selects the smallest element of the unsorted portion of the array and moves it to its correct position in the sorted portion of the array. The time complexity of selection sort is O(n^2), where n is the number of elements in the array. This makes it inefficient for large arrays and it is not a good choice for sorting large datasets. However, selection sort is easy to understand and implement, and it is useful for educational purposes and for small arrays. One of the advantages of selection sort is that it makes fewer swaps than other sorting algorithms such as bubble sort. This makes it more efficient in terms of memory writes, which can be important in some situations. Another advantage of selection sort is that it is a stable so

Counting Sort

Counting sort is a sorting algorithm that operates by counting the frequency of each element in the input array, and then using these frequencies to compute the final sorted output. The algorithm works by first creating an array of counters, with each counter corresponding to a distinct element in the input array. The counters are initialized to zero, and then the input array is scanned once to count the number of occurrences of each element. This count information is stored in the corresponding counter in the counter array. Once the count information is obtained, the algorithm uses it to determine the final position of each element in the sorted output array. This is done by iterating over the counter array, and for each counter value, writing the corresponding element to the sorted output array the appropriate number of times. The time complexity of counting sort is O(n+k), where n is the length of the input array and k is the range of the elements. The space complexity is also O(n+k

Pigeonhole Sort

Pigeonhole sort is a sorting algorithm that works by distributing elements of an input sequence into a set of pigeonholes or buckets. It is an efficient sorting algorithm for small ranges of values where the number of elements to be sorted is roughly the same as the range of values. Pigeonhole sort is an example of a bucket sort algorithm. The pigeonhole sort algorithm works by first determining the range of values in the input sequence. This range is used to create a set of pigeonholes or buckets, each of which can hold one or more elements from the input sequence. Each element in the input sequence is then placed into its corresponding pigeonhole based on its value. If two or more elements have the same value, they can be placed in the same pigeonhole. Once all the elements have been placed into their corresponding pigeonholes, the elements are sorted within each pigeonhole. Finally, the sorted elements are combined into a single output sequence. Pigeonhole sort has a time complexity