# Merge Sort CSC 172 Sorting Lists with Merge

• Slides: 85

Merge. Sort CSC 172

Sorting Lists with Merge. Sort Note: a list of length 1 is sorted If two lists are sorted, how can you combine them into one sorted list? How can you divide one list into two?

Recursive Mergesort Basis: A list of length 1 is sorted Induction: for lists of >= 1 element 1. Split the list into two equal (as possible) parts 2. Recursively sort each part 3. Merge the result of the two recursive sorts One at a time, select the smaller element from the two fronts of each list Physical demo

Node split public static Node split(Node head){ Node second. Node; if (head == null) return null; else if (head. get. Next() == null) return null; else { second. Node = head. get. Next(); head. set. Next(second. Node. get. Next()); second. Node. set. Next(split(second. Node. get. Next()); return second. Node; } }

A linked List 6 1 3 2 5 9

A linked List in Split head 6 1 3 2 5 9

A linked List in Split second. Node = head. get. Next(); Split 0 head second. Node 6 1 3 2 5 9

A linked List in Split head. set. Next(second. Node. get. Next()); Split 0 head second. Node 6 1 3 2 5 9

First recursive call in Split second. Node. set. Next(split(second. Node. get. Next()); Split 0 head second. Node 6 1 3 2 5 9

New Split split(second. Node. get. Next()); Split 1 head second. Node 6 1 3 2 5 9

New Split head. set. Next(second. Node. get. Next()); Split 1 head second. Node 6 1 3 2 5 9

Second recursive Split second. Node. set. Next(split(second. Node. get. Next()); Split 1 head second. Node 6 1 3 2 5 9

Second recursive Split second. Node. set. Next(split(second. Node. get. Next()); Split 2 head second. Node 6 1 3 2 5 9

Second recursive Split head. set. Next(second. Node. get. Next()); Split 2 head second. Node 6 1 3 2 5 9 second. Node. set. Next(split(second. Node. get. Next()); ? What gets returned? return second. Node;

Backing up second. Node. set. Next(split(second. Node. get. Next()); Split 1 head second. Node 6 1 3 2 5 9

Backing up second. Node. set. Next(split(second. Node. get. Next()); Split 1 head second. Node 6 1 3 What gets returned? 2 5 9 return second. Node;

Backing up second. Node. set. Next(split(second. Node. get. Next()); Split 1 head second. Node 6 1 3 What gets returned? 2 5 9 return second. Node;

Backing up second. Node. set. Next(split(second. Node. get. Next()); Split 1 head second. Node 6 1 3 What gets returned? 2 5 9 return second. Node;

Node Merge public static Node merge(Node list 1, Node list 2){ if (list 1 == null) return list 2; else if (list 2 == null) return list 1; else if (list 1. get. Data. compare. To(list 2. get. Data()) < 0) { list 1. set. Next(merge(list 1. get. Next(), list 2); return list 1; } else { list 2. set. Next(merge(list 1, list 2. get. Next()); return list 2; } }

Two linked Lists 1 3 7 2 8 9

Two linked Lists in merge 1 3 7 2 8 9 list 1 merge 0 list 2

Two linked Lists in merge 0 th 1 3 7 2 8 9 list 1 merge 1 list 2

Two linked Lists in merge 0 th 1 3 7 2 8 9 list 1 merge 2 list 2 1 st

Two linked Lists in merge 0 th 2 nd 1 3 7 2 8 9 list 1 merge 3 list 2 1 st

Two linked Lists in merge 0 th 2 nd 3 rd 1 3 7 2 8 9 list 1 merge 4 list 2 1 st

Two linked Lists in merge 0 th 2 nd 1 3 7 2 8 9 list 1 merge 3 list 2 1 st

Two linked Lists in merge 0 th 2 nd 1 3 7 2 8 9 list 1 merge 2 list 2 1 st

Two linked Lists in merge 0 th 2 nd 1 3 7 2 8 9 list 1 merge 2 list 2 1 st

Two linked Lists in merge 0 th 2 nd 1 3 7 2 8 9 list 1 merge 0 list 2 1 st

Node Merge. Sort public static Node merge. Sort(Node list){ Node second. List; if (list == null) return null; else if (list. get. Next() == null) return list; else { second. List = split(list); return merge(merge. Sort(list), merge. Sort(second. List)); } }

Mergesort 3 1 4 1 5 9 2 6

Mergesort: Split 6 3 1 4 1 5 9 2

Mergesort: Split 6 3 1 4 1 5 9 2

Mergesort: Split 9 6 3 1 4 1 5 2

Mergesort: Split 9 6 3 1 4 1 5 2

Mergesort: Split 1 9 6 3 1 4 5 2

Mergesort: Split 1 9 6 3 1 4 5 2

Mergesort: Split 1 1 9 6 3 4 5 2

Mergesort: Split 1 1 9 6 3 4 5 2

Mergesort: Split 6 1 1 9 3 4 5 2

Mergesort: Split 6 1 1 9 3 4 5 2

Mergesort: Split 1 6 1 9 3 4 5 2

Mergesort: Split 1 6 1 9 3 4 5 2

Mergesort: Split 6 1 1 9 3 4 5 2

Mergesort: Split 6 1 1 9 3 4 5 2

Mergesort: Merge 6 1 1 9 3 4 5 2

Mergesort: Merge 6 1 1 9 3 4 5 2

Mergesort: Merge 1 6 1 9 3 4 5 2

Mergesort: Split 1 6 1 9 3 4 5 2

Mergesort: Split 1 6 9 1 3 4 5 2

Mergesort: Split 1 6 9 1 3 4 5 2

Mergesort: Merge 1 6 9 1 3 4 5 2

Mergesort: Merge 1 6 9 1 3 4 5 2

Mergesort: Merge 1 6 1 9 3 4 5 2

Mergesort: Merge 6 1 1 9 3 4 5 2

Mergesort: Merge 6 1 1 9 3 4 5 2

Mergesort: Merge 1 1 6 9 3 4 5 2

Mergesort: Merge 1 1 6 9 3 4 5 2

Mergesort: Split 1 1 6 9 3 4 5 2

Mergesort: Split 1 1 6 9 2 3 4 5

Mergesort: Split 1 1 6 9 2 3 4 5

Mergesort: Split 1 1 6 9 4 2 3 5

Mergesort: Split 1 1 6 9 4 2 3 5

Mergesort: Split 1 1 6 9 2 4 3 5

Mergesort: Split 1 1 6 9 2 4 3 5

Mergesort: Merge 1 1 6 9 2 4 3 5

Mergesort: Merge 1 1 6 9 2 3 5 4

Mergesort: Merge 1 1 6 9 2 4 3 5

Mergesort: Split 1 1 6 9 2 4 3 5

Mergesort: Split 1 1 6 9 2 4 5 3

Mergesort: Split 1 1 6 9 2 4 5 3

Mergesort: Merge 1 1 6 9 2 4 5 3

Mergesort: Merge 1 1 6 9 2 4 5 3

Mergesort: Merge 1 1 6 9 2 4 3 5

Mergesort: Merge 1 1 6 9 4 2 3 5

Mergesort: Merge 1 1 6 9 4 2 3 5

Mergesort: Merge 1 1 6 9 2 3 4 5

Mergesort: Merge 1 1 6 9 2 3 4 5

Mergesort: Merge 1 6 9 1 2 3 4 5

Mergesort: Merge 6 9 1 1 2 3 4 5

Mergesort: Merge 6 9 1 1 2 3 4 5

Mergesort: Merge 6 9 1 1 2 3 4 5

Mergesort: Merge 6 9 1 1 2 3 4 5

Mergesort: Merge 6 9 1 1 2 3 4 5

Mergesort: Merge 1 1 2 3 4 5 6 9