hw02solutions - CSE 331 Spring 2008 Homework 2 Solutions 1....

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 331 Spring 2008 Homework 2 Solutions 1. We begin with the unsorted list {5, 9, 16, 8, 2, 32, 4, 12}. Mergesort recursively breaks this list in half until it has formed lists with only one element in them. The following diagram shows the breakup occurring in parallel. In an actual program, of course, the splitting and merging steps would occur in sequence, but this is much easier to visualize. Once the sequence has been broken down, the sublists are merged into sorted lists. The merging continues until a single sorted list has been generated from the input sequence. 2. Quicksort normally sorts the entire input sequence. For this problem, however, we are only asked to sort the largest 0.05 N elements in the sequence. We can make the sorting go faster if we try to ignore the other 0.95 N elements as much as possible. On each iteration of quicksort, we will still need to partition the array. Quicksort simply will not work without doing this. However, once the array is partitioned, we can save some time. We 5, 9, 16, 8, 2, 32, 4, 12 5, 9, 16, 8 2, 32, 4, 12 5, 9 16, 8 2, 32 4, 12 5 9 16 8 2 32 4 12 2, 4, 5, 8, 9, 12, 16, 32 5, 8, 9, 16 2, 4, 12, 32 5, 9 8, 16 2, 32 4, 12 5 9 16 8 2 32 4 12
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
only need to sort the top 0.05 N elements in the array. If the partition with the larger elements contains 0.05 N elements or more, we will only sort that partition. None of the elements in the lower-valued partition will be in the top 0.05 N elements, so we can completely ignore them. When there are fewer than 0.05 N elements in the higher-valued partition, we will sort both the upper and lower partitions. However, when we sort the lower-valued partition, we will tell it to sort only the top (0.05 N – length of higher-valued partition – 1) elements in the partition. This way, we can keep the number of elements we sort to the bare minimum. Unfortunately, while it will often be faster than a full quicksort, the modified algorithm is still Ω(N log N), even in the best case. The upper 0.05 N elements in the array need to be sorted. This sorting procedure will take Ω((0.05 N) log (0.05 N)) time. The constant factors do not affect the overall complexity in this case. Ω((0.05 N) log (0.05 N)) = Ω(N log N). Even if we can somehow pick out the 0.05 N largest elements in constant time, it will still take Ω(N log N) time to sort them. 3. Insertion sort is stable. The inner loop swaps each element with the elements preceding it. This continues until the element being inserted runs into an element that is less than or equal to it. At this point, the swapping process stops. Therefore, if two elements are equal, the one that comes later in the sequence will never be moved before the one that comes earlier in the sequence. Bubble sort is also stable.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/25/2008 for the course CSE 331 taught by Professor M.mccullen during the Spring '08 term at Michigan State University.

Page1 / 5

hw02solutions - CSE 331 Spring 2008 Homework 2 Solutions 1....

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online