Stable Algorithm: Insertion, Merge, Heap, Quicksort?

In summary, the conversation discussed the stability of sorting algorithms, specifically Insertion Sort. The group determined that Insertion Sort is a stable algorithm and went through a proof to show this. The only suggestion for improvement was to include the actual algorithm in the discussion. The conversation ended with a question about the correctness of the invariant and whether it should be $a<b \leq j-1$ or $a<b \leq j$.
  • #1
mathmari
Gold Member
MHB
5,049
7
Hey! :eek:

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
  • #2
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)
 
  • #3
mathmari said:
Hey! :eek:

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.
 
  • #4
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)
 
  • #5
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)
 
  • #6
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)
 
  • #7
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)
 
  • #8
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
 
  • #9
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)
 

FAQ: Stable Algorithm: Insertion, Merge, Heap, Quicksort?

What is a stable algorithm?

A stable algorithm is one that preserves the relative order of elements with equal values during the sorting process. This means that if two elements have equal values, they will remain in the same order in the sorted output as they were in the original input.

What is the difference between insertion sort and merge sort?

Insertion sort is a simple sorting algorithm that builds the final sorted list one item at a time. It is efficient for small datasets but becomes increasingly slow as the dataset size grows. Merge sort, on the other hand, is a divide and conquer algorithm that divides the dataset into smaller subproblems, sorts them, and then merges the sorted sublists to get the final sorted output. It is more efficient than insertion sort for larger datasets.

What is a heap data structure and how is it used in sorting algorithms?

A heap is a specialized tree-based data structure that is commonly used to implement priority queues. It follows the heap property where the parent node is always greater than or equal to its child nodes. In sorting algorithms, heaps are used in heap sort, which is an efficient sorting algorithm that uses the heap data structure to sort elements in place.

What is the time complexity of quicksort?

The average time complexity of quicksort is O(n log n), making it one of the fastest sorting algorithms. However, in the worst case, it can have a time complexity of O(n^2), which occurs when the pivot element is repeatedly chosen as the smallest or largest element in the list. This can be avoided by choosing a pivot element using a randomized algorithm.

Which sorting algorithm is best for different types of datasets?

The best sorting algorithm depends on the characteristics of the dataset. For small datasets, insertion sort may be the best option due to its simplicity. For larger datasets, merge sort or heap sort may be more efficient. Quicksort is a good choice for most datasets, but it may struggle with datasets that contain a high number of duplicate elements. It is important to consider the time and space complexity of each algorithm when choosing the best one for a specific dataset.

Similar threads

Replies
2
Views
1K
Replies
1
Views
1K
Replies
2
Views
1K
Replies
1
Views
1K
Replies
10
Views
4K
Replies
6
Views
1K
Back
Top