Tutorial 05

advertisement
Data Structure
(Semester 1, 2014-2015)
Tutorial 05
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
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.
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.
4. Give a permutation for the values 0 through 7 that cause Quicksort (section 7.4) to
have its worst case behavior.
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
Data Structure – Tutorial 5
1
Download