MergeSort algoritmos de complegidada (1).ppt

esuti 6 views 22 slides Sep 12, 2024
Slide 1
Slide 1 of 22
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22

About This Presentation

MergeSort algoritmos de complegidada (1).ppt


Slide Content

1
Merge Sort
•Review of Sorting
•Merge Sort

2
Sorting Algorithms
•Selection Sort uses a priority queue P implemented
with an unsorted sequence:
–Phase 1: the insertion of an item into P takes O(1) time;
overall O(n) time for inserting n items.
–Phase 2: removing an item takes time proportional to the
number of elements in P, which is O(n): overall O(n
2
)
–Time Complexity: O(n
2
)

3
Sorting Algorithms (cont.)
•Insertion Sort is performed on a priority queue P which is a
sorted sequence:
–Phase 1: the first insertItem takes O(1), the second O(2), until the
last insertItem takes O(n): overall O(n
2
)
–Phase 2: removing an item takes O(1) time; overall O(n).
–Time Complexity: O(n
2
)
•Heap Sort uses a priority queue K which is a heap.
–insertItem and removeMin each take O(logk), k being the
number of elements in the heap at a given time.
–Phase 1: n elements inserted: O (nlogn) time
–Phase 2: n elements removed: O (nlogn) time.
–Time Complexity: O (nlog n)

4
Divide-and-Conquer
•Divide and Conquer is more than just a military strategy; it is also a
method of algorithm design that has created such efficient algorithms
as Merge Sort.
•In terms or algorithms, this method has three distinct steps:
–Divide: If the input size is too large to deal with in a straightforward manner,
divide the data into two or more disjoint subsets.
–Recur: Use divide and conquer to solve the subproblems associated with the
data subsets.
–Conquer: Take the solutions to the subproblems and “merge” these solutions
into a solution for the original problem.

5
Merge-Sort
•Algorithm:
–Divide: If S has at leas two elements (nothing needs to be done if S has zero or
one elements), remove all the elements from S and put them into two sequences,
S
1
and S
2
, each containing about half of the elements of S. (i.e. S
1
contains the
first n/2 elements and S
2 contains the remaining n/2 elements.

Recur: Recursive sort sequences S
1
and S
2
.

Conquer: Put back the elements into S by merging the sorted sequences S
1 and
S
2
into a unique sorted sequence.
•Merge Sort Tree:
–Take a binary tree T
–Each node of T represents a recursive call of the merge sort algorithm.
–We associate with each node v of T a the set of input passed to the invocation v
represents.
–The external nodes are associated with individual elements of S, upon which no
recursion is called.

6
Merge-Sort

7
Merge-Sort(cont.)

8
Merge-Sort (cont.)

9
Merge-Sort (cont.)

10
Merge-Sort (cont.)

11
Merge-Sort (cont.)

12
Merge-Sort (cont.)

13
Merge-Sort(cont.)

14
Merge-Sort (cont.)

15
Merge-Sort (cont.)

16
Merge-Sort (cont.)

17
Merging Two Sequences

18
Merging Two Sequences (cont.)

19
Merging Two Sequences (cont.)

20
Merging Two Sequences (cont.)

21
Merging Two Sequences (cont.)

22
Merging Two Sequences (cont.)
Tags