Is Bubblesort Algorithm Correct?

  • MHB
  • Thread starter evinda
  • Start date
In summary, the conversation is about proving the correctness of the bubble sort algorithm. The speaker suggests using induction to prove it and explains the process for the base case and the induction hypothesis. They also reference a video that demonstrates how the algorithm works. Finally, they discuss how the algorithm compares and swaps elements in the array.
  • #1
evinda
Gold Member
MHB
3,836
0
Hello! (Wave)

Could you help me to prove the correctness of the following algorithm?
Code:
Bubblesort(A){
  int i, j;  
  for i from 1 to n {
      for j from n-1 downto i { 
           if (A[j] > A[j+1])
               swap(A[j], A[j+1]) 
           } 
  }
}
We could do this using induction, right? (Thinking)
So, for [m] i=1 [/m] we compare [m] A[n-1] [/m] with [m] A[n] [/m] and if it holds that [m] A[n-1]>A[n] [/m] then we swap these two values.
So at the base case do we just say that the last two elements of the array are sorted? :confused:
What do we suppose at the induction hypothesis? (Thinking)
 
Technology news on Phys.org
  • #2
Please observe how the bubble sort algorithm works on this video: https://www.youtube.com/watch?v=JP5KkzdUEYI (yours might be a variant that sorts from the smallest value, I haven't checked, but the two variants are equivalent up to reordering). Pay particular attention to the "limit" vertical line, and how everything on the right side of the line is automatically sorted. Note that at each iteration, the algorithm finds the largest element on the left of the line, and bubbles it up by repeated swaps. There's your induction hypothesis. Can you write down the inductive proof now that you have the intuition?​
 
  • #3
So if we have for example this array:View attachment 3846

for [m]i=1 [/m] we will do the following:View attachment 3847

So this means that for [m] i=1 [/m] we compare pairwise the elements [m] A[n], A[n-1] [/m], [m] A[n-1],A[n-2] [/m], $\dots$, [m]A[2],A[1][/m] and if $A>A[i+1], i \in \{ 1, \dots, n-1 \}$ then we swap their values, right? (Thinking)
 

Attachments

  • array3.png
    array3.png
    934 bytes · Views: 49
  • array3.png
    array3.png
    3.9 KB · Views: 55

Related to Is Bubblesort Algorithm Correct?

1. What is Bubblesort and how does it work?

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

2. How efficient is Bubblesort compared to other sorting algorithms?

Bubblesort has a worst-case time complexity of O(n^2), meaning that it is less efficient than other sorting algorithms such as Quicksort or Merge Sort which have a worst-case time complexity of O(nlogn). This makes Bubblesort less ideal for sorting large lists.

3. Can Bubblesort handle duplicate elements in a list?

Yes, Bubblesort can handle duplicate elements in a list. It compares adjacent elements and swaps them if they are in the wrong order, regardless of whether they are duplicates or not.

4. How can you determine if Bubblesort has correctly sorted a list?

You can determine if Bubblesort has correctly sorted a list by checking if the elements are in ascending or descending order after the sorting process. You can also use a comparison-based sorting algorithm such as Quicksort or Merge Sort to verify the correctness of Bubblesort's sorting.

5. Are there any cases where Bubblesort may not work correctly?

Yes, Bubblesort may not work correctly if the list contains elements of different data types, such as both numbers and strings. It also may not work correctly if the list is already partially sorted, as it may not switch elements if they are already in the correct order.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
34
Views
3K
Replies
9
Views
1K
  • Programming and Computer Science
Replies
9
Views
537
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
4
Views
749
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
9
Views
2K
Back
Top