Question

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 Mergesort (as presented in class) are stable, which are not?

Homework Answers

Answer #1

Stable Algorithms:

Insertion Sort and Merge Sort.

Unstable Algorithms:

Shellsort and Heapsort

Explanation:

A Stable sorting algorithm is, if two objects in the list with equal values appear in the same order in sorted list as they are in the input list to be sorted. Some sorting algorithms are stable by nature like Insertion sort, Merge Sort, Bubble Sort, etc. Other algorithms are not, like Heap Sort, Quick Sort, Shellsort etc.

Example :

  • Sortable list : (9,8), (1,8), (9,7), (4,1), (2,6)
  • Stable sorted list be like : (1,8), (2,6), (4,1), (9,8), (9,7) on every sorting.
  • Unstable sorted list be like : (1,8), (2,6), (4,1), (9,8), (9,7) OR (1,8), (2,6), (4,1), (9,7), (9,8)
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
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...
. 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 2 (2+3 marks). Assume your sorting algorithms have to deal with lists that can potentially...
Problem 2 (2+3 marks). Assume your sorting algorithms have to deal with lists that can potentially contain duplicates. Assume the same sorting algorithms as discussed in class / in the textbook. (a) What is the running time of Insertion Sort when all elements in a given list of length N are equal? Explain your answer. (b) Give a Θ-bound on the running time of Mergesort for the case that all elements in a given list of length N are equal...