Data Structure (Semester 1, 2014-2015) Tutorial 05 - Answer Prepared by Dr. Patrick Chan Date: November-2014 1. Which of the following operations can be performed faster on sorted data than on unsorted data? i) Finding an item with a minimum value ii) Computing an average of values iii) Finding the median iv) Finding the value which appears most frequently in the data Answer: i) yes ii) no iii) yes iv) no 2. In out implementation of bubble sort, a sorted array was scanned bottom-up to bubble up the smallest element. Write a program which scans top-down to bubbles down the largest element. Answer: template <class Elem, class Comp> void bubsort(Elem A[], int n) { for (int i=0; i<n-1; i++) for (int j=0; j<n-1-i; j++) if (Comp::gt(A[j], A[j+1])) swap(A, j, j+1); } 3. The cocktail shaker sort designed by Donald Knuth is a modification of bubble sort in which the direction of bubbling changes in each iteration. In first iteration, the smallest element is bubbled up; in the next, the largest is bubbled down; in the next, the second smallest is bubbled up; and so forth. Implement this new algorithm. Answer: template <class Elem, class Comp> void cocktailsort(Elem A[], int n) { for (int i=0; i<(n-1)/2; i++) { for (int j=n-1; j>i; j--) if (Comp::lt(A[j], A[j-1])) swap(A, j, j-1); for (int j=i+1; j<n-1-i; j++) if (Comp::gt(A[j], A[j+1])) swap(A, j, j+1); } } Data Structure – Tutorial 5 1 4. Give a permutation for the values 0 through 4 that cause Quicksort (section 7.4) to have its worst case behavior. Answer: 4 3 0 2 1 1st pass a b c d e a b 0 d e a b e d 0 0 b e d a 2nd pass b e d a b 1 d a b a d 1 1 a d b 3rd pass a d b a 2 b a b 2 2 b a 4th pass b a 3 a a 3 3 a 5th pass a : c should be pivot, let c = 0 : Assume c is the smallest a - e : e should be pivot, let e = 1 : Assume e is the smallest among a b d e : d should be pivot, let d = 2 : Assume d is the smallest among a b d : b should be pivot, let b = 3 : Assume b is the smallest among a b : Let a = 4 Therefore a b c d e 4 3 0 2 1 Data Structure – Tutorial 5 2 5. A sorting algorithm is said to be stable if the original ordering for duplicate keys is preserved. Which of the following methods are stable? i) Bubble Sort vi) Merge Sort ii) Selection Sort vii) Heap Sort iii) Insertion Sort viii) Bin Sort iv) Shell Sort ix) Radix Sort v) Quick Sort Answer: i) Bubble Sort is stable. A swap is done only if the lower element’s value is LESS. ii) Selection Sort is NOT stable. The new low value is set only if it is actually less than the previous one, but the direction of the search is from the bottom of the array. The algorithm will be stable if “less than” in the check becomes “less than or equal to” for selecting the low key position. iii) Insertion Sort is stable. A swap is done only if the lower element’s value is LESS. iv) Shell Sort is NOT stable. The sublist sorts are done independently, and it is quite possible to swap an element in one sublist ahead of its equal value in another sublist. Once they are in the same sublist, they will retain this (incorrect) relationship. v) Quick-sort is NOT stable. After selecting the pivot, it is swapped with the last element. This action can easily put equal records out of place. vi) Conceptually (in particular, the linked list version) Mergesort is stable. The array implementations are NOT stable, since, given that the sublists are stable, the merge operation will pick the element from the lower list before the upper list if they are equal. This is easily modified to replace “less than” with “less than or equal to.” vii) Heapsort is NOT stable. Elements in separate sides of the heap are processed independently, and could easily become out of relative order. viii) Binsort is stable. Equal values that come later are appended to the list. ix) Radix Sort is stable. While the processing is from bottom to top, the bins are also filled from bottom to top, preserving relative order. Data Structure – Tutorial 5 3