{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# CS163_Week11 - CS163 Data structures Week 11 Sorting Review...

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

CS163: Data structures Week 11: Sorting & Review 8/2008

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

View Full Document
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
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...

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

View Full Document
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.
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

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

View Full Document
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
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).

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

View Full Document
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
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.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 39

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

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

View Full Document
Ask a homework question - tutors are online