{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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
Image of page 1

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

View Full Document Right 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. All swapping occurs between adjacent elements. If the earlier of the two elements is greater than the later of the two, they will be swapped so the greater element now comes later in the list. If two adjacent elements are equal, they will not be swapped. Since only adjacent elements are swapped, it is impossible for an element to change places with an equal element.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern