1 / 20

Algorithms

Algorithms. Sorting and Merging. Learning Objectives. Explain the difference between insertion sort and merge sort. Describe algorithms for implementing insertion sort and merge sort methods. Sorting Methods. Insertion Sort Quick Sort Merge Sort. 1. Insertion Sort. Sorting.

Télécharger la présentation

Algorithms

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. Algorithms Sorting and Merging

  2. Learning Objectives • Explain the difference between insertion sort and merge sort. • Describe algorithms for implementing insertion sort and merge sort methods.

  3. Sorting Methods • Insertion Sort • Quick Sort • Merge Sort

  4. 1. Insertion Sort

  5. Sorting • Ordering values from a single list in an ascending or descending order. • E.g. • Ascending numeric • 3 5 6 8 12 16 25 • Descending alphabetic • Will Rose Mattu Juni Hazel Dopu Anne

  6. Example – Sort the list below into ascending order • 20 47 12 53 32 84 85 96 45 18 • Start with 2nd number: 47 > 20 so no change in the order is made. • 12 < 47 , 12 < 20 so insert 12 before 20: • 12 20 47 53 32 84 85 96 45 18 • This is continued until the last number is inserted in its correct position.

  7. Key for table on the next slide • Blue • Predecessors • Red • Value being compared

  8. Example Continued

  9. General Method • Compare each value in turn with the values before it in the list. • Insert the value into its correct position. • Repeat until the last value is compared with the rest of the list.

  10. 2. Quick Sort

  11. Quick Sort • http://upload.wikimedia.org/wikipedia/commons/9/9c/Quicksort-example.gif • http://en.wikipedia.org/wiki/File:Sorting_quicksort_anim.gif • http://en.wikipedia.org/wiki/Quicksort

  12. Insertion Vs Quick Sort • The Quicksort method is one of the fastest sorting algorithms for sorting large lists ofdata.  • The Insertionsort method is a fast sorting algorithm for sorting very small lists that are already somewhat sorted. 

  13. 3. Merge Sort

  14. Merging • Taking two lists which have been sorted into the same order and putting them together to form a single sorted list. • E.g. • Bharri Emi Kris Mattu Parrash Roger Will • and • Annis Chu Liz Medis Ste • Merge to: • Annis Bharri Chu Emi Kris Liz Mattu Medis Parrash Roger Ste Will

  15. Example • 2 4 7 10 15 & • 3 5 12 14 18 26 • Compare the first values in each list. • 2 < 3 so put it in a new list. • New = 2

  16. Example Continued • Since 2 came from the 1st list we now use the next value in the first list and compare it with the 1st number from the second list (as we have not yet used it). • 3 < 4 so 3 is placed in the new list. • New = 2 3

  17. Example Continued • As 3 came from the 2nd list we use the next number in the 2nd list and compare it with the next unused number in the 1st list. • Repeat until one of the lists is exhausted then copy the rest of the other list into the new list.

  18. Key for table on the next slide • Red • Values being compared

  19. Example Continued

  20. Method • Compare first values in each list. • Place smaller value in new list. • Take next value in the list which the smaller value came from and compare with the 1st value of the other list. • Place smaller value in new list. • Take next value in the list which the smaller value came from and compare with the next unused value of the other list. • Repeat until one list is exhausted then copy the rest of the other list into the new list.

More Related