1 / 16

Zabin Visram Room CS115

Zabin Visram Room CS115. Lecture Structure Summary of simple sorts Complex Sorts Merge Sort. Summary of simple sorts. Bubble sort (or exchange sort) is generally not a good practical method of sorting. For random data in an array:

clark
Télécharger la présentation

Zabin Visram Room CS115

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. 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/mukundan/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 Sort

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

  5. 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 Divide Conquer Combine Merge(n) Mergesort(n/2) Mergesort(n/2) Merge sort(n) CS126 ZV2005 Merge Sort

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

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

  8. 0 3 5 7 9 0 2 4 6 2 New Merged array 3 3 5 7 9 0 2 4 6 3 5 7 9 0 2 4 6 CS126 ZV2005 Merge Sort

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

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

  11. Merge Sort Algorithm - RECURSION 35 28 18 45 62 48 30 38 split 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 35 28 18 45 62 48 30 38 split split 35 28 18 45 split 62 48 30 38 split split split 35 28 18 45 62 48 30 38 merge merge 28 35 18 45 merge merge 48 62 30 38 merge 18 28 35 45 merge 30 38 48 62 merge 18 28 30 35 38 45 48 62 CS126 ZV2005 Merge Sort

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

  13. Another example or Merge Sort split Values are recursively split into unsorted lists that are then recursively merged into ascending order 0 –1 58 3 42 4 40 2 1 43 3 65 40 2 1 43 3 65 0 –1 58 3 42 4 40 2 1 43 3 65 0 –1 58 3 42 4 2 1 3 65 -1 58 42 4 Merge 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 CS126 ZV2005 Merge Sort

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

  15. Answer 9 6 2 4 8 7 5 3 9 6 2 4 8 7 5 3 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 Sort

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

More Related