1 Data Structures CSCI 132, Spring 2014 Lecture 30 Comparison trees, Merge Sort

Preview:

Citation preview

1

Data Structures

CSCI 132, Spring 2014Lecture 30

Comparison trees, Merge Sort

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 7 8 5 2

3 5 7 8 2

2 3 5 7 8

sorted unsorted

3

Comparison Trees for Sorting

Insertion sort comparison tree

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

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.

sortedunsorted

5

Comparison Trees for Sorting

Selection sort comparison tree

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

Comparison Trees for Sorting

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

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, highlistlowlist.sort();highlist.sort();combine(lowlist, highlist);

}}

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

27 15 12 6 19 8

27 15 12 6 19 8

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

Implementation of Merge Sort

template <class Record>void Sortable_list<Record> :: merge_sort( ){

recursive_merge_sort(head);}

template <class Record>void Sortable_list<Record> :: recursive_merge_sort(Node<Record> * &sub_list){

if (sub_list != NULL && sub_list->next != NULL) {Node<Record> *second_half = divide_from(sub_list);recursive_merge_sort(sub_list);recursive_merge_sort(second_half);sub_list = merge(sub_list, second_half);

}}

12

divide_from( ) returns a pointer to the middle of the list

5 3 11 7 2

positionmidpoint second_half

sub_list

5 3 11 7 2

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

divide_from( )template <class Record>Node<Record> *Sortable_list<Record> :: divide_from(Node<Record> *sub_list) {

Node<Record> *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

merge( ) combines two sorted sublists

3 5 11

2 7

first

second

3 5 11

2 7

first

secondcombined

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

merge( )template <class Record>Node<Record> *Sortable_list<Record> :: merge(Node<Record> *first, Node<Record> *second) {

Node<Record> *last_sorted; // points to the last node of sorted listNode<Record> combined; // dummy first node, points to merged listlast_sorted = &combined;while (first != NULL && second != NULL) { // Attach node with smaller key

if (first->entry <= second->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;

}

Recommended