answersLogoWhite

0

The algorithm for bubble sort, also know as pair exchange...

  1. Set a swap flag false
  2. Loop for the first N-1 elements
  3. Compare each element with the following element
  4. If the two elements are in the correct order, continue to next loop iteration
  5. Otherwise, swap the two elements and set the swap flag true
  6. At loop end, if the swap flag is true, repeat starting at step 1
  7. Otherwise, sort completed

Bubble sort is so named because out of order elements "bubble" to the end of the array, moving one step per inner loop iteration.

As stated above, the algorithm is slow because it takes a while for a significantly out of order element to reach its final point when it needs to come closer to the beginning of the array. It can be improved by introducing a "distance" parameter, initially set to one half of the array size, and using that distance in step 3 to choose the second element. After the algorithm is completed for that distance, the distance is halved, and we iterate the entire algorithm until the distance is only one. (This variation is more formally known as merge exchange, but it still retains the "bubble" characteristic.)

User Avatar

Wiki User

15y ago

What else can I help you with?

Related Questions

Is bubble sort a stable sorting algorithm?

Yes, bubble sort is a stable sorting algorithm.


What is the running time of bubble sort algorithm?

The running time of the bubble sort algorithm is O(n2), where n is the number of elements in the array being sorted.


What is the running time of the bubble sort algorithm?

The running time of the bubble sort algorithm is O(n2), where n is the number of elements in the array being sorted.


Can you modify the bubble sort algorithm to search to sort an array of characters instead of array of integers?

The bubble sort algorithm can be applied to an array of characters. Every character can be translated to an integer equivalent via the ascii table


Which sorting algorithm is more efficient for small datasets: bubble sort or selection sort?

Selection sort is more efficient for small datasets compared to bubble sort.


What is the average case time complexity of the Bubble Sort algorithm?

The average case time complexity of the Bubble Sort algorithm is O(n2), where n is the number of elements in the array being sorted.


Bubble sort Extra or in space?

Bubble sort is an "in place" algorithm. Other than a temporary "switch" variable, no extra space is required.


What are the advantages for bubble sort?

Bubble sort has no practical applications other than that it is often cited as an example of how not to write an algorithm. Insert sort is the best algorithm for sorting small lists of items and is often used in conjunction with quick sort to sort larger lists. Like insert sort, bubble sort is simple to implement and is a stable sort (equal items remain in the same order they were input). However, insert sort uses copy or move operations rather than swaps (which is actually three operations per swap) and is therefore quicker. The only time a bubble sort will work quicker than insert sort is when the array is already sorted, which renders the entire algorithm redundant. A modified algorithm that specifically tests if an array is sorted or not would be more efficient than a single-pass bubble sort.


What is the best case scenario for the bubble sort algorithm in terms of time complexity?

The best case scenario for the bubble sort algorithm is when the list is already sorted. In this case, the time complexity is O(n), where n is the number of elements in the list.


Is it trueThe biggest advantage of the bubble sort algorithm is that values move only by one element at a time toward their final destination in the array?

This is false. The movement described is a disadvantageof bubble sort.


In the bubble sort algorithm the second loop iterates ---------- for each of the unsorted array elements?

n-1 times


Why is the Bubble Sort algorithm also referred ro as Sink Sort?

Bubble sort got its name because if you could watch the way your data was changing, on each iteration you would see the greatest number "bubble" to the top.Similarly, you could said that you would see the lowest number "sink" to the bottom.