1 / 14

Design and Analysis of Algorithms: Sorting (Insertion Sort, Merge Sort)

This chapter focuses on the design and analysis of sorting algorithms, specifically insertion sort and merge sort. Examples and analysis of these algorithms are provided.

kaylad
Download Presentation

Design and Analysis of Algorithms: Sorting (Insertion Sort, Merge Sort)

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. Design and Analysis of Algorithmsتصميم وتحليل الخوارزميات (311 عال) Chapter 2 Sorting (insertion Sort, Merge Sort)

  2. 9 7 6 15 16 5 10 11 16 5 10 6 7 9 15 11 10 16 16 11 5 5 15 10 10 5 7 6 6 7 9 7 6 9 9 15 15 16 11 11 15 16 10 5 6 7 9 11 16 16 5 5 10 10 6 9 7 7 9 6 15 15 11 11 10 15 16 5 6 7 9 11 Insertion Sort Execution Example 2

  3. An Example: Insertion Sort InsertionSort(A, n) {for i = 2 to n { key = A[i] next key j = i - 1; go left while (j > 0) and (A[j] > key) { find place for key A[j+1] = A[j] shift sorted right j = j – 1 go left } A[j+1] = key put key in place } } 3

  4. An Example: Insertion Sort i =  j =  key = A[j] =  A[j+1] =  30 10 40 20 InsertionSort(A, n) {for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key} } 1 2 3 4 4

  5. An Example: Insertion Sort i = 4 j = 1 key = 20A[j] = 10 A[j+1] = 20 10 20 30 40 InsertionSort(A, n) {for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key} } 1 2 3 4 Done! 5

  6. Insertion Sort InsertionSort(A, n) {for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key} } How many times will this while loop execute? 6

  7. Analysis of Insertion Sort • Best Case • If A is sorted: O(n) comparisons • Worst Case • If A is reversed sorted: O(n2) comparisons • Average Case • If A is randomly sorted: O(n2) comparisons 7

  8. Divide-and-Conquer • Recursive in structure • Divide the problem into several smaller sub-problems that are similar to the original but smaller in size • Conquerthe sub-problems by solving them recursively. If they are small enough, just solve them in a straightforward manner. • Combine the solutions to create a solution to the original problem 8

  9. An Example: Merge Sort • Divide: Divide the n-element sequence to be sorted into two subsequences of n/2 elements each • Conquer: Sort the two subsequences recursively using merge sort. • Combine: Merge the two sorted subsequences to produce the sorted answer. 9

  10. Merge Sort MergeSort(A, left, right) { if (left < right) { mid = floor((left + right) / 2); MergeSort(A, left, mid); MergeSort(A, mid+1, right); Merge(A, left, mid, right); } } // Merge() takes two sorted subarrays of A and // merges them into a single sorted subarray of A. // It requires O(n) time 10

  11. Merge Sort Revisited • To sort n numbers • if n = 1 done! • recursively sort 2 lists of numbers ën/2û and én/2ù elements • merge 2 sorted lists in O(n) time • Strategy • break problem into similar (smaller) subproblems • recursively solve subproblems • combine solutions to answer 11

  12. Merge Sort [8, 3, 13, 6, 2, 14, 5, 9, 10, 1, 7, 12, 4] [8, 3, 13, 6, 2, 14, 5] [9, 10, 1, 7, 12, 4] [8, 3, 13, 6] [2, 14, 5] [9, 10, 1] [7, 12, 4] [8, 3] [13, 6] [2, 14] [5] [9, 10] [1] [7, 12] [4] [8] [3] [13] [6] [2] [14] [9] [10] [7] [12] 12

  13. Merge Sort [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13,14] [2, 3, 5, 6, 8, 13, 14] [1, 4, 7, 9, 10,12] [3, 6, 8, 13] [2, 5, 14] [1, 9, 10] [4, 7, 12] [3, 8] [6, 13] [2, 14] [5] [9, 10] [1] [7, 12] [4] [8] [3] [13] [6] [2] [14] [9] [10] [7] [12] 13

  14. Analysis of Merge Sort • Divide: computing the middle takes O(1) • Conquer: solving 2 sub-problem takes 2T(n/2) • Combine: merging n-element takes O(n) • Total: T(n) = O(1) if n = 1 T(n) =2T(n/2)+ O(n) + O(1) if n > 1 • T(n) = O(n log n) Merge sort is Solved by a recurrence. 14

More Related