# CS126 ZV2005 Merge Sort1 Zabin Visram Room CS115 Lecture Structure Summary of simple sorts Complex Sorts Merge Sort.

## Presentation on theme: "CS126 ZV2005 Merge Sort1 Zabin Visram Room CS115 Lecture Structure Summary of simple sorts Complex Sorts Merge Sort."— Presentation transcript:

CS126 ZV2005 Merge Sort1 Zabin Visram Room CS115 Lecture Structure Summary of simple sorts Complex Sorts Merge Sort

CS126 ZV2005 Merge Sort2 Summary of simple sorts Bubble sort (or exchange sort) is generally not a good practical method of sorting. For random data in an array: Selection sort requires fewer moves than bubble sort but a constant number of comparisons. Thus if the elements are large and costly to move but have small sort keys then selection sort is likely to give best performance.

CS126 ZV2005 Merge Sort3 Summary of simple sorts If the elements are easy to move - but have complex sort keys - additional comparisons involved in selection become significant, - then insertion is likely to be better. You can compare insertion sort and selection sort and bubble sort by demonstration http://www.cosc.canterbury.ac.nz/people/mu kundan/dsal/appldsal.html In a linked list the data are not moved while sorting and so insertion sort, which minimises the number of comparisons, is preferable.

CS126 ZV2005 Merge Sort4 Merge Sort MergeSort is a sorting algorithm which produces a sorted sequence by sorting its two halves and merging them. MergeSort algorithm is based on a divide and conquer strategy. First the sequence to be sorted is decomposed into two halves (Divide). Each half is sorted independently (Conquer). Then the two sorted halves are merged to a sorted sequence (Combine)

CS126 ZV2005 Merge Sort5 Divide & Conquer Strategy The divide & conquer strategy for solving a problem consists of three steps 1) Divide – the problem is decomposed into subproblems 2) Conquer – the subproblems are solved 3) Combine - the solutions of the subproblem are recombined to the solution of the original problem Merge(n) Divide Conquer Combine Mergesort(n/2) Merge sort(n)

CS126 ZV2005 Merge Sort6 Merge Sort – Divide & Conquer Algorithm The divide & conquer technique is used to sort a list. The algorithm partitions the lists into two sublists,sorts the sublists, and then combines the sorted sublists into one sorted list

CS126 ZV2005 Merge Sort7 Merge Sort – overview consider the idea of subdividing the sorting of a long list into sorting shorter lists and then put these together in a way that keeps the combined list still sorted. The essential idea of merge sort is to make repeated use of a method which merges two lists (each of which is already sorted into ascending order) into a third list in ascending order.

CS126 ZV2005 Merge Sort8 35790246 3579 0246 0 2 3 35790246 New Merged array

CS126 ZV2005 Merge Sort9 Brief overview of method The basic merge method compares the first items in the two (already sorted) lists and places the lower of the two in the new list. One of the original lists now has a new lowest value and this is again compared with the lowest of the other list, and the lower of the two placed next in the new list. This process is repeated until one of the lists is exhausted. Then the remaining values from the other list are placed in order in the new list and the merge is completed. It is a divide and conquer algorithm & has a recursive version

CS126 ZV2005 Merge Sort10 The merge sort chops the list into two sublists of nearly equal size For example List : 35 28 18 45 62 48 30 38 The merge sort algorithm partitions this list into two sublists First sublist : 35 28 18 45 Second sublist : 62 48 30 38 The two sublists are sorted using the same algorithm (i.e merge sort) First sublist : 18 28 35 45 Second sublist : 30 38 48 62 Next the merge sort algorithm combines – that is merges – the two sorted sublists into one sorted list

CS126 ZV2005 Merge Sort11 Merge Sort Algorithm - RECURSION 35 28 18 45 62 48 30 38 35 28 18 45 35 2818 45 35281845 18 4528 35 18 28 35 45 18 28 30 35 38 45 48 62 62 4830 38 62483038 30 3848 62 30 38 48 62 62 48 30 38 If the list is of a size greater than 1 { 1) Divide the list into two sublists 2) Merge sort the first sublist 3) Merge sort the second sublist 4) Merge the first sublist and the second sublist } Thus we are using recursion to implement the merge sort algorithm split merge

CS126 ZV2005 Merge Sort12 Merge Sort – Array based to sort an array – we can divide the array into two subarrays of equal length And finally merge the two subarrays

CS126 ZV2005 Merge Sort13 Another example or Merge Sort 40 2 1 43 3 65 40 2 1 43 3 65 0 –1 58 3 42 4 40 1 2 43 3 65 0 –1 58 3 4 42 1 2 40 3 43 65 –1 0 58 3 4 42 1 2 3 40 43 65 –1 0 3 4 42 58 -1 0 1 2 3 3 4 40 42 43 58 65 2653158424 split Merge Values are recursively split into unsorted lists that are then recursively merged into ascending order 0 –1 58 3 42 4

CS126 ZV2005 Merge Sort14 Task in pairs :- Trace the steps that a merge sort takes when sorting the following array into ascending order 9 6 2 4 8 7 5 3

CS126 ZV2005 Merge Sort15 Answer 9 6 2 4 8 7 5 3 6 9 2 4 7 8 3 5 2 4 6 9 3 5 7 8 2 3 4 5 6 7 8 9

CS126 ZV2005 Merge Sort16 Demonstration http://www.cosc.canterbury.ac.nz/people/ mukundan/dsal/MSort.html

Similar presentations