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: 87
  • array3.png
    array3.png
    3.9 KB · Views: 109
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...
Back
Top