CS163_Week11 - CS163: Data structures Week 11: Sorting...

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

View Full Document Right Arrow Icon
    CS163: Data structures 8/2008
Background image of page 1

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

View Full DocumentRight Arrow Icon
dbtien – Data structures CS163 today: Sorting Algorithms mergesort quicksort As we learn about each sorting algorithm, we will discuss its efficiency Review for the FINAL
Background image of page 2
dbtien – Data structures Mergesort The mergesort is considered to be a divide and conquer sorting algorithm (as is the quicksort ). The mergesort is a recursive approach which is very efficient. The mergesort can work on arrays, linked lists, or even external files. At first glance, it doesn’t seem like a sorting algorithm at all. ..
Background image of page 3

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

View Full DocumentRight Arrow Icon
dbtien – Data structures Mergesort The mergesort is a recursive sorting algorithm that always gives the same performance regardless of the initial order of the data. For example, you might divide an array in half - sort each half - then merge the sorted halves into 1 data structure. To merge, you compare 1 element in 1 half of the list to an element in the other half, moving the smaller item into the new data structure.
Background image of page 4
dbtien – Data structures Mergesort The sorting method for each half is done by a recursive call to merge sort. That is why this is a divide and conquer method. Mergesort(list,starting place, ending place) if the starting place is less than the ending place middle place = (starting + ending) div 2 mergesort(list, starting place, middle place) mergesort(list,middle place+1, ending place) merge the 2 halves of the list
Background image of page 5

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

View Full DocumentRight Arrow Icon
dbtien – Data structures Mergesort Original List             Recursive calls              to Mergesort Time to Merge             38 16 27 39 12 27 38 16 27 39 12 27 38 16 27 39 12 27 38 16 39 12 16 38 12 39 16 27 38 12 27 39 12 16 27 27 38 39
Background image of page 6
dbtien – Data structures Mergesort If we implemented this approach using arrays --- If the total number of items in your list is m. ..then for each merge we must do m-1 comparisons. For example, if there are 6 items we must do five comparisons. In addition, there are m moves from the original location to some temporary location (and back).
Background image of page 7

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

View Full DocumentRight Arrow Icon
dbtien – Data structures Mergesort Even though this seems like a lot, you will see that this is actually faster than either the selection sort or the insertion sort. Although the mergesort is extremely efficient with respect to time, it does require that an equal "temporary" array be used which is the same size as the original array. If temporary arrays are not used. ..this approach ends up being no better than any of the others
Background image of page 8
dbtien – Data structures Mergesort If we implement the mergesort using linked lists, we do not need to be concerned with the amount of time needed to move data Instead, we just need to concentrate on the number of comparisons. When lists get very long, the
Background image of page 9

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

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/03/2010 for the course CS 16x taught by Professor Tang during the Spring '10 term at Aachen University of Applied Sciences.

Page1 / 39

CS163_Week11 - CS163: Data structures Week 11: Sorting...

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

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