200 likes | 355 Views
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.
E N D
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
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
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.
Key for table on the next slide • Blue • Predecessors • Red • Value being compared
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.
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
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.
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
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
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
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.
Key for table on the next slide • Red • Values being compared
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.