Question

A sorting method is stable if for any i<j such that initially E[i] = E[j], the...

A sorting method is stable if for any i<j such that initially E[i] = E[j], the sort moves E[i] to E[k] and moves E[j] to E[m] for some k and m such that k < m. Which of the following algorithms are stable? For each that is not, give an example in which relative order of two equal keys is changed.

Insertion Sort

Maxsort

QuickSort

Heapsort

MergeSort

Radix Sort

Homework Answers

Answer #1

from the given sorting techniques Insertion Sort, Max Sort and Radix Sort are stable .

Note:- In merge sort stablity is depends on the merge algorithm that is implemented as a part of the merge sort algorithm.

Heap sort unstable example

Consider array 21 20a 20b 12 11 8 7 (already in max-heap format)

here 20a = 20b just to differentiate the order we represent them as 20a and 20b

While heapsort first 21 is removed and placed in the last index then 20a is removed and placed in last but one index and 20b in the last but two index so after heap sort the array looks like

7 8 11 12 20b 20a 21.

It does not preserve the order of elements and hence can't be stable

Quick sort unstable example

Quick Sort is not stable because it swaps non-adjacent elements.

The most succinct example:

Given [2, 2, 1], the ‘2’ values will not retain their initial order.

Merge sort unstable example

It depends on the merge algorithm that is implemented as a part of the merge sort algorithm.

 

def merge(L,R):

i = 0

j = 0

answer = []

while i<len(L) and j<len(R):

if L[i]<=R[j]:

answer.append(L[i])

i += 1

else:

answer.append(R[j])

j += 1

if i<len(L):

answer.extend(L[i:])

if j<len(R):

answer.extend(R[j:])

return answer

print merge([1,1,2],[1,1,1,2,3])

but if you changed the 6th line to be -> if L[i]<R[j]
then the merge sort implemented by your algo would become unstable.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
. A sorting algorithm is stable if the relative order of any two equal entries in...
. A sorting algorithm is stable if the relative order of any two equal entries in the given array stays the same: when two records a[i] and a[j] are equal in content, and i < j, then the algorithm sorts the array in a way that the record originally stored in a[i], still appears to the left of the record originally stored in a[j], when the array is sorted. Which of the algorithms Insertion Sort, Shellsort, Heapsort, and Mergesort (as...
Problem 3 (4 marks). A sorting algorithm is stable if the relative order of any two...
Problem 3 . A sorting algorithm is stable if the relative order of any two equal entries in the given array stays the same: when two records a[i] and a[j] are equal in content, and i < j, then the algorithm sorts the array in a way that the record originally stored in a[i], still appears to the left of the record originally stored in a[j], when the array is sorted. Which of the algorithms Insertion Sort, Shellsort, Heapsort, and...
Problem 3 (4 marks). A sorting algorithm is stable if the relative order of any two...
Problem 3 . A sorting algorithm is stable if the relative order of any two equal entries in the given array stays the same: when two records a[i] and a[j] are equal in content, and i < j, then the algorithm sorts the array in a way that the record originally stored in a[i], still appears to the left of the record originally stored in a[j], when the array is sorted. Which of the algorithms Insertion Sort, Shellsort, Heapsort, and...
Edsger Dijkstra studied the following problem which he called the Problem of the Dutch National Flag....
Edsger Dijkstra studied the following problem which he called the Problem of the Dutch National Flag. We are given an array of pebbles, some blue, some red, some white. We want to rearrange them in the order of the Dutch flag, that is, first come the red, then the white, and finally the blue pebbles. Which sorting method is best suited for this task, that is, most efficient? Group of answer choices Heapsort Insertion sort Mergesort Quicksort Selection sort Shellsort...
Do a theta analysis and count the number of computations it performed in each function/method of...
Do a theta analysis and count the number of computations it performed in each function/method of the following code: import java.io.*; import java.util.Scanner; class sort { int a[]; int n; long endTime ; long totalTime; long startTime; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public sort(int nn) // Constructor { a = new int[nn]; n = nn; endTime= 0; totalTime =0; startTime =0; } public static void main(String args[]) throws IOException { System.out.print("\nEnter number of students: "); int nn =...
Complete the java code as per the comments public class Sorting {    ///////////////////////////////////////////////    //...
Complete the java code as per the comments public class Sorting {    ///////////////////////////////////////////////    // STEP 1 -- Make sorting methods generic    ///////////////////////////////////////////////       /**    * Re-orders the contents given array using the insertion sort algorithm.    *    * @param data The array to be sorted.    */    //TODO: Make me generic to work on any kind of Comparable data!    public static void insertionSort(int[] data)    {        int insert; // temporary...
I only need parts E, F, I and J. Don't worry about the others. Thanks. A...
I only need parts E, F, I and J. Don't worry about the others. Thanks. A model airplane weighing 520 g is attached to a rope of length 1.9 m and allowed to fly at a constant height 1.3 m above the ground where the rope is attached to a rock. The propeller puts out a constant force 1.4 N. At time t = 0 s, the plane moves counterclockwise (looking down at it) at a speed 2 m/s. (a)...
QUESTION 19 A key distribution and authentication method used by every operating system. It uses a...
QUESTION 19 A key distribution and authentication method used by every operating system. It uses a shared secret key and can also be used for single sign-on operations. 2 points    QUESTION 20 Authentication method that allows a user to authenticate once and use multiple services without having to re-authenticate. 2 points    QUESTION 21 Protocol that establishes the security association for the Authentication Header (AH) or the Encapsulating Security Payload (ESP) in IPsec, and provides keys for both AH...
Consider permutations of the 26-character lowercase alphabet Σ={a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}. In how many of these permutations do a,b,c...
Consider permutations of the 26-character lowercase alphabet Σ={a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}. In how many of these permutations do a,b,c occur consecutively and in that order? In how many of these permutations does a appear before b and b appear before c?
Finish the code wherever it says TODO /**    * Node type for this list. Each...
Finish the code wherever it says TODO /**    * Node type for this list. Each node holds a maximum of nodeSize elements in an    * array. Empty slots are null.    */    private class Node {        /**        * Array of actual data elements.        */        // Unchecked warning unavoidable.        public E[] data = (E[]) new Comparable[nodeSize];        /**        * Link to next node.       ...