Download presentation

Presentation is loading. Please wait.

Published byMegan Lamb Modified over 2 years ago

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

2
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.

3
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 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.

4
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)

5
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)

6
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

7
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.

8
CS126 ZV2005 Merge Sort New Merged array

9
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

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

11
CS126 ZV2005 Merge Sort11 Merge Sort Algorithm - RECURSION 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

12
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

13
CS126 ZV2005 Merge Sort13 Another example or Merge Sort – – – – split Merge Values are recursively split into unsorted lists that are then recursively merged into ascending order 0 –

14
CS126 ZV2005 Merge Sort14 Task in pairs :- Trace the steps that a merge sort takes when sorting the following array into ascending order

15
CS126 ZV2005 Merge Sort15 Answer

16
CS126 ZV2005 Merge Sort16 Demonstration mukundan/dsal/MSort.html

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google