Stable Algorithm: Insertion, Merge, Heap, Quicksort?

  • Context: MHB 
  • Thread starter Thread starter mathmari
  • Start date Start date
  • Tags Tags
    Algorithm Stable
Click For Summary

Discussion Overview

The discussion revolves around the stability of various sorting algorithms, specifically Insertion Sort, Merge Sort, Heap Sort, and Quicksort. Participants explore how to determine whether these algorithms can be implemented as stable and delve into the formal proof of stability for Insertion Sort.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants propose that Insertion Sort is stable because it maintains the relative order of equal elements during sorting.
  • One participant suggests that to prove stability, one must show that the original order is invariant throughout the algorithm's execution.
  • Another participant questions the formulation of the invariant used to demonstrate stability, specifically whether the condition should involve $a
  • There is a suggestion that both conditions could be acceptable, but clarification is sought on how this affects the termination of the proof.
  • Participants discuss the need to justify why the order of elements does not change during the maintenance phase of the algorithm.

Areas of Agreement / Disagreement

There is no consensus on the stability of all algorithms discussed, and multiple viewpoints regarding the formulation of the invariant and its implications remain unresolved.

Contextual Notes

Participants express uncertainty about the correct formulation of the invariant and its implications for proving stability. The discussion includes various interpretations and potential improvements to the proof structure.

mathmari
Gold Member
MHB
Messages
4,984
Reaction score
7
Hey! :o

Which of the following sorting algorithms are (or can be implemented as) stable:
Insertion Sort, Merge Sort, Heap Sort, Quicksort ??

How can we determine it?? (Wondering)
 
Technology news on Phys.org
For a stable sorting algorithm it stands the following:

If we have for example the sequence 1212. After sorting the first 1 will still be before the second 1 and the 2 that is now at the second position wil still be before the 2 of the last position...

At Insertion Sort each element x will be compared with all the elements on its right side. These elements won't get before x, since all the elements at the left side of x are sorted and since it is still on the right side it is bigger or equal that x. So, Insertion Sort is stable. Is this correct?? (Wondering)

How could we formulate it formally?? (Wondering)
 
mathmari said:
Hey! :o

Which of the following sorting algorithms are (or can be implemented as) stable:
Insertion Sort, Merge Sort, Heap Sort, Quicksort ??

How can we determine it?? (Wondering)

Take a look: Sorting_algorithm#Comparison_of_algorithms

It shows which of those algorithms is stable. (Wasntme)
mathmari said:
For a stable sorting algorithm it stands the following:

If we have for example the sequence 1212. After sorting the first 1 will still be before the second 1 and the 2 that is now at the second position wil still be before the 2 of the last position...

At Insertion Sort each element x will be compared with all the elements on its right side. These elements won't get before x, since all the elements at the left side of x are sorted and since it is still on the right side it is bigger or equal that x. So, Insertion Sort is stable. Is this correct?? (Wondering)

Yes, Insertion Sort is stable. (Nod)

How could we formulate it formally?? (Wondering)

It think your formulation is good enough, unless you want to give a proof.

To prove an algorithm is not stable, it suffices to give a counter example.

To show an algorithm is stable, such as is the case for Insertion Sort, you'd need to go through each step of the algorithm and show that the original sort order is an invariant. (Sweating)

See e.g. Insertion Sort for a proof that the algorithm actually sorts. It could be extended by considering at every step if the original sort order is retained.
 
I like Serena said:
To show an algorithm is stable, such as is the case for Insertion Sort, you'd need to go through each step of the algorithm and show that the original sort order is an invariant. (Sweating)

See e.g. Insertion Sort for a proof that the algorithm actually sorts. It could be extended by considering at every step if the original sort order is retained.

Is the invariant the following?? (Wondering)

At the beginning of each iteration of the for loop, if $A[a]=A, a<b \leq j-1$, then $A[a]$ appears before $A$.

Initialization:
We have to show that the invariant holds before the first iteration of the loop where $j=2$. In this case, the subarray $A[1 \dots j-1]$ consists of only the element $A[1]$, so there are no $a<b \leq j-1=1$. So, the invariant holds trivially.

Maintenance:
We have to check if the property maintains at each iteration. The body of the outer for loop shifts the elements $A[j-1], A[j-2], A[j-3], \dots$ one position to the right until the right position for $A[j]$ is found, at which the value of $A[j]$ will be inserted, but it doesn't change their order. SO, if the invariant holds before an iteration of the loop, it still holds before the next iteration.

Termination:
We have to check what happens when the loop stops. The outer for loop stops when $j=n+1$. Setting $j=n+1$ at the invariant loop, we have that for each $A[a]=A, a<b \leq n$, it implies that $A[a]$ appears before $A$ at the initial subarray. But the subarray $A[1 \dots n]$ is the whole array. So, Insertion Sort is stable.
Is this correct?? (Wondering) Could I improve something?? (Wondering)
 
mathmari said:
Is the invariant the following?? (Wondering)

At the beginning of each iteration of the for loop, if $A[a]=A, a<b \leq j-1$, then $A[a]$ appears before $A$.

Initialization:
We have to show that the invariant holds before the first iteration of the loop where $j=2$. In this case, the subarray $A[1 \dots j-1]$ consists of only the element $A[1]$, so there are no $a<b \leq j-1=1$. So, the invariant holds trivially.

Maintenance:
We have to check if the property maintains at each iteration. The body of the outer for loop shifts the elements $A[j-1], A[j-2], A[j-3], \dots$ one position to the right until the right position for $A[j]$ is found, at which the value of $A[j]$ will be inserted, but it doesn't change their order. SO, if the invariant holds before an iteration of the loop, it still holds before the next iteration.

Termination:
We have to check what happens when the loop stops. The outer for loop stops when $j=n+1$. Setting $j=n+1$ at the invariant loop, we have that for each $A[a]=A, a<b \leq n$, it implies that $A[a]$ appears before $A$ at the initial subarray. But the subarray $A[1 \dots n]$ is the whole array. So, Insertion Sort is stable.
Is this correct?? (Wondering) Could I improve something?? (Wondering)


Looks good to me! (Whew)

The only possible improvement I see is to add the actual algorithm so that it is clear what you're talking about. (Nerd)
 
I am thinking again about the invariant and I got stuck...

At the statement:

At the beginning of each iteration of the for loop, if $A[a]=A, a<b \leq j-1$, then $A[a]$ appears before $A$.

is it correct to suppose $a<b \leq j-1$ ?? (Wondering) Or should it be $a<b \leq j$ ?? (Wondering)
 
mathmari said:
I am thinking again about the invariant and I got stuck...

At the statement:

At the beginning of each iteration of the for loop, if $A[a]=A, a<b \leq j-1$, then $A[a]$ appears before $A$.

is it correct to suppose $a<b \leq j-1$ ?? (Wondering) Or should it be $a<b \leq j$ ?? (Wondering)


That depends on the actual algorithm that you're using.
Which algorithm is it? (Wondering)
 
I like Serena said:
That depends on the actual algorithm that you're using.
Which algorithm is it? (Wondering)

It is the following:

Code:
INSERTION-SORT(A)
1  for j ← 2 to length[A]
2      do key ← A[j]
3      Insert A[j] into the sorted sequence A[1 . . . j−1]
4      i ← j − 1
5      while i > 0 and A[i] > key
6           do A[i + 1] ← A[i]
7           i ← i − 1
8      A[i + 1] ← key
 
mathmari said:
I am thinking again about the invariant and I got stuck...

At the statement:

At the beginning of each iteration of the for loop, if $A[a]=A, a<b \leq j-1$, then $A[a]$ appears before $A$.

is it correct to suppose $a<b \leq j-1$ ?? (Wondering) Or should it be $a<b \leq j$ ?? (Wondering)


I believe it does not matter. Both are fine. (Smile)

However, looking at your invariant, I do see another problem. It should be 'originally appeared' instead of 'appears'. (Nerd)
 
  • #10
I like Serena said:
I believe it does not matter. Both are fine. (Smile)

Ok... But how will the termination be when we take $a<b \leq j$?? (Wondering) In this case $j=n+1$, so $a<b \leq n+1$. $A[a]=A,a<b \leq n+1$, it implies that $A[a]$ appears before $A$ at the initial subarray.

Can we formulate it in that way?? (Wondering) Or is it a problem to have $a<b \leq n+1$ ?? (Wondering)
I like Serena said:
However, looking at your invariant, I do see another problem. It should be 'originally appeared' instead of 'appears'. (Nerd)

Yes, you're right! (Smile)
We have to check if the property maintains at each iteration. The body of the outer for loop shifts the elements $A[j−1],A[j−2],A[j−3],\dots$ one position to the right until the right position for $A[j]$ is found, at which the value of $A[j]$ will be inserted, but it doesn't change their order. o, if the invariant holds before an iteration of the loop, it still holds before the next iteration.

At the maintenance do I have to justify why their order doesn't change ?? (Wondering) Do I have to mention the condition $A>key$ ?? (Wondering)
 
  • #11
Could you give me some hints how the statement of the invariant for the Merge Sort will look like?? (Wondering)
 
  • #12
mathmari said:
Could you give me some hints how the statement of the invariant for the Merge Sort will look like?? (Wondering)

What's the algorithm? (Wondering)
 

Similar threads

Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K