answersLogoWhite

0

What else can I help you with?

Continue Learning about Engineering

What are the characteristics of the bubble sort algorithm?

Bubble sort is a stable, in-place sort with a best, worst and average case of O(n!) for n elements, thus making it highly inefficient and entirely unsuitable for sorting large amounts of data. For this reason bubblesort is often cited as an example of how not to write an algorithm. The algorithm starts by accepting a zero-based array with n elements (0 to n-1). While n is greater than 1, the algorithm iterates an outer loop. On each iteration of the outer loop, an inner loop traverses from element index 1 to n-1. On each iteration of the inner loop, the element at the current index is compared with the element at the previous index. If they are out of order, the two elements are swapped. When the inner loop has finished, n is decremented. At this point element n is now its correct place and is ignored on the next iteration of the outer loop. When n is not greater than 1, all the elements are sorted and the outer loop terminates. In other words, the algorithm locates the largest value in the range 0 to n-1 and places it at index n-1 (bubbling it up the list with each swap). After decrementing n, everything from element n up is sorted and everything below n is unsorted, thus creating two subarrays split at n. On each pass, the sorted subarray gains a new element and the unsorted subarray loses an element. When there is only one element in the unsorted subarray, the entire array is sorted. The inefficiency of the algorithm is that it takes no account of elements at the end of the unsorted subarray that may already be in their correct position. The algorithm can be improved with the observation that the position of the last swap at the end of the inner loop means that everything from that point forwards is already sorted and don't need to be compared on the next pass. So rather than reducing the unsorted subarray by just one element, the subarray can be reduce by one or more elements. To achieve this we initialise a temporary variable with the value zero at the start of the outer loop, and whenever a swap occurs in the inner loop we assign the current index to the temporary variable. When the inner loop ends, the temporary variable indicates where the last swap occured and we can assign that value to n, which may reduce n by one or more elements on each pass. The only other change we need to make is that the outer loop now terminates when n is zero (the initial value of the temporary variable), which means no swaps occured on the last iteration of the inner loop, so everything must be sorted. Even with this optimisation the worst case is still O(n!) if the list is completely reversed. However, for an already-sorted list the best case becomes O(n). Since these two extremes are isolated cases, the average case is somewhere in between, around O(n!/2), which is still highly inefficient and unsuitable for large amounts of data. The following is an implemention of an optimal bubble sort in C++: template<typename _Ty> void bubble (std::vector<_Ty>& A) { unsigned size = A.size(); while (size) { unsigned temp = 0; // records last swap position for (unsigned index=1; index<size; ++index) { if (A[index]<A[index-1]) { std::swap (A[index],A[index-1]); temp=index; } } size = temp; } }


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


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.


What is the minimum number of comparisons in bubble sort?

A bubble sort may have a range from O(n-1) for a pre-sorted array, to O(n2-n) for a poorly implemented bubble sort algorithm. Given 20 elements, a best case scenario is 19 comparisons, and the worst case is 380 comparisons.


What are some potential inefficiencies when using the bubble sort algorithm?

Although bubble sort is one of the simplest sorting algorithms to understand and implement, its O(n2)complexity means it is far too inefficient for use on lists having more than a few elements. Even among simple O(n2)sorting algorithms, algorithms like insertion sort are usually considerably more efficient.

Related Questions

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.


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.


Is bubble sort a stable sorting algorithm?

Yes, bubble sort is a stable sorting algorithm.


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.


What is the best and worst case time complexity of the Bubble Sort algorithm?

The best-case time complexity of the Bubble Sort algorithm is O(n), where n is the number of elements in the array. This occurs when the array is already sorted. The worst-case time complexity is O(n2), which happens when the array is sorted in reverse order.


What are the characteristics of the bubble sort algorithm?

Bubble sort is a stable, in-place sort with a best, worst and average case of O(n!) for n elements, thus making it highly inefficient and entirely unsuitable for sorting large amounts of data. For this reason bubblesort is often cited as an example of how not to write an algorithm. The algorithm starts by accepting a zero-based array with n elements (0 to n-1). While n is greater than 1, the algorithm iterates an outer loop. On each iteration of the outer loop, an inner loop traverses from element index 1 to n-1. On each iteration of the inner loop, the element at the current index is compared with the element at the previous index. If they are out of order, the two elements are swapped. When the inner loop has finished, n is decremented. At this point element n is now its correct place and is ignored on the next iteration of the outer loop. When n is not greater than 1, all the elements are sorted and the outer loop terminates. In other words, the algorithm locates the largest value in the range 0 to n-1 and places it at index n-1 (bubbling it up the list with each swap). After decrementing n, everything from element n up is sorted and everything below n is unsorted, thus creating two subarrays split at n. On each pass, the sorted subarray gains a new element and the unsorted subarray loses an element. When there is only one element in the unsorted subarray, the entire array is sorted. The inefficiency of the algorithm is that it takes no account of elements at the end of the unsorted subarray that may already be in their correct position. The algorithm can be improved with the observation that the position of the last swap at the end of the inner loop means that everything from that point forwards is already sorted and don't need to be compared on the next pass. So rather than reducing the unsorted subarray by just one element, the subarray can be reduce by one or more elements. To achieve this we initialise a temporary variable with the value zero at the start of the outer loop, and whenever a swap occurs in the inner loop we assign the current index to the temporary variable. When the inner loop ends, the temporary variable indicates where the last swap occured and we can assign that value to n, which may reduce n by one or more elements on each pass. The only other change we need to make is that the outer loop now terminates when n is zero (the initial value of the temporary variable), which means no swaps occured on the last iteration of the inner loop, so everything must be sorted. Even with this optimisation the worst case is still O(n!) if the list is completely reversed. However, for an already-sorted list the best case becomes O(n). Since these two extremes are isolated cases, the average case is somewhere in between, around O(n!/2), which is still highly inefficient and unsuitable for large amounts of data. The following is an implemention of an optimal bubble sort in C++: template<typename _Ty> void bubble (std::vector<_Ty>& A) { unsigned size = A.size(); while (size) { unsigned temp = 0; // records last swap position for (unsigned index=1; index<size; ++index) { if (A[index]<A[index-1]) { std::swap (A[index],A[index-1]); temp=index; } } size = temp; } }


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


Bubble sort Extra or in space?

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


Algorithm of a programme of bubble sort?

The algorithm for bubble sort, also know as pair exchange...Set a swap flag falseLoop for the first N-1 elementsCompare each element with the following elementIf the two elements are in the correct order, continue to next loop iterationOtherwise, swap the two elements and set the swap flag trueAt loop end, if the swap flag is true, repeat starting at step 1Otherwise, sort completedBubble 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.)


What is complex sort?

Time complexity Best case: The best case complexity of bubble sort is O(n). When sorting is not required, all the elements are already sorted. Average case: The average case complexity of bubble sort is O(n*n). It occurs when the elements are jumbled, neither properly ascending nor descending. Worst case: The worst-case complexity of bubble sort is O(n*n). It occurs when the array elements are needed to be sorted in reverse order. Space complexity In the bubble sort algorithm, space complexity is O(1) as an extra variable is needed for swapping.


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.