MHB Is Bubblesort Algorithm Correct?

  • Thread starter Thread starter evinda
  • Start date Start date
AI Thread Summary
The discussion focuses on proving the correctness of the bubble sort algorithm using mathematical induction. The algorithm iterates through the array, comparing adjacent elements and swapping them if they are out of order. The initial step involves verifying the base case for the last two elements of the array, establishing that they are sorted if necessary. The induction hypothesis suggests that after each complete pass through the array, the largest unsorted element is correctly positioned at the end of the sorted section. The participants discuss how to articulate the inductive proof, emphasizing the process of pairwise comparisons and swaps to ensure the array is sorted progressively. A video link is provided to illustrate the algorithm's operation, reinforcing the understanding of how elements are sorted through successive iterations.
evinda
Gold Member
MHB
Messages
3,741
Reaction score
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
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?​
 
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: 86
  • array3.png
    array3.png
    3.9 KB · Views: 107
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top