Download presentation
Presentation is loading. Please wait.
Published byBruce Taylor Modified over 9 years ago
1
1 Data Structures CSCI 132, Spring 2014 Lecture 30 Comparison trees, Merge Sort
2
2 Insertion Sort 1. Start at beginning of list and continue with each successive item. 2. For each item, insert it into appropriate position in sorted list. 7 3 8 5 2 3 7 8 5 2 3 5 7 8 2 2 3 5 7 8 sortedunsorted
3
3 Comparison Trees for Sorting Insertion sort comparison tree
4
4 Selection sort 5 12 7 6 2 10 5 10 7 6 2 12 5 2 7 6 10 12 5 2 6 7 10 12 2 5 6 7 10 12 Selection sort decreases the number of moves made when sorting a contiguous list. Algorithm: Find the maximum entry in unsorted sublist Swap it to the end of the sublist. sorted unsorted
5
5 Comparison Trees for Sorting Selection sort comparison tree
6
6 Average number of comparisons for sorting Number of comparisons in tree: Worst case: height of tree Average case: (External path length) / (number of leaves) What is external path length for Insertion Sort tree? What is average number of comparisons for insertion sort tree? What is external path length for Selection Sort tree? What is average number of comparisons for selection sort tree?
7
7 Comparison Trees for Sorting
8
8 Lower Bound for Sorting Number of comparisons in tree: Worst case: height of tree Average case: (External path length) / (number of leaves) What is the best we can do for sorting n items? For n items, there are n! possible orderings = number of leaves Height of tree is lg(n!) = worst case number of comparisons External path length is at least (n!)lg(n!) Average number of comparisons is at least lg(n!) lg(n!) ~ nlg(n) + O(n) (Note: Can sort faster if the sort is not based on comparisons).
9
9 Divide and Conquer Sorting It is easier and faster to sort a short list than a long one. Can divide a list into sublists, sort the sublists and then combine the sorted sublists to create the full sorted list. In pseudocode: void Sortable_list::sort( ) { if (list has length greater than 1) { partition list into lowlist, highlist lowlist.sort(); highlist.sort(); combine(lowlist, highlist); } }
10
10 Merge Sort Divide the list into 2 equal halves. Keep dividing until list length = 1. Merge sublists into sorted list: 27 15 12 6 19 8 15 27 12 6 19 8 12 15 27 6 8 19 6 8 12 15 19 27
11
11 Implementation of Merge Sort template void Sortable_list :: merge_sort( ) { recursive_merge_sort(head); } template void Sortable_list :: recursive_merge_sort(Node * &sub_list) { if (sub_list != NULL && sub_list->next != NULL) { Node *second_half = divide_from(sub_list); recursive_merge_sort(sub_list); recursive_merge_sort(second_half); sub_list = merge(sub_list, second_half); } }
12
12 divide_from( ) returns a pointer to the middle of the list 531172 position midpointsecond_half sub_list 531172 positionmidpoint sub_list To find the midpoint, move the midpoint pointer one step for every two steps moved by the position pointer. Return second_half, which points to the node following the midpoint.
13
13 divide_from( ) template Node *Sortable_list :: divide_from(Node *sub_list) { Node *position, // traverses the entire list *midpoint, // moves at half speed of position to midpoint *second_half; if ((midpoint = sub_list) == NULL) return NULL; // List is empty. position = midpoint->next; while (position != NULL) { // Move position twice for midpoint’s one move. position = position->next; if (position != NULL) { midpoint = midpoint->next; position = position->next; } } second_half = midpoint->next; midpoint->next = NULL; return second_half; }
14
14 merge( ) combines two sorted sublists 3511 27 first second 3511 27 first second combined last_sorted Compare values in first node of the two lists. Add whichever is smaller to combined list. Keep track of last_sorted, first and second pointers. Return combined.next
15
merge( ) template Node *Sortable_list :: merge(Node *first, Node *second) { Node *last_sorted; // points to the last node of sorted list Node combined; // dummy first node, points to merged list last_sorted = &combined; while (first != NULL && second != NULL) { // Attach node with smaller key if (first->entry entry) { last_sorted->next = first; last_sorted = first; first = first->next; // Advance to the next unmerged node. } else { last_sorted->next = second; last_sorted = second; second = second->next; } } if (first == NULL) // After one list ends, attach the remainder of the other. last_sorted->next = second; else last_sorted->next = first; return combined.next; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.