Docsity

Sorting Algorithms: Heap Sort and Merge Sort, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
1. So far, we have looked at simple sorts consisting of nested loops. The # of inner loop iterations n*(n-1)/2 is
Ο
(n
2
).
Consider using a min-heap to sort a list. (methods:
BinHeap(), insert(item), delMin(), isEmpty(), size()
)
a) If we insert all of the list elements into a min-heap, what would we easily be able to determine?
1. Create an empty heap
Generl idea of Heap sort:
2. Insert all n array items into heap
3. delMin heap items back to array in sorted order
myList sorted list with n items
myList unsorted list with n items
heap with
n items
b) What is the overall O( ) for heap sort?
2. Another way to do better than the simple sorts is to employ divide-and-conquer (e.g., Merge sort and Quick Sort).
Recall the idea of Divide-and-Conquer algorithms. Solve a problem by:
dividing problem into smaller problem(s) of the same kind
solving the smaller problem(s) recursively
use the solution(s) to the smaller problem(s) to solve the original problem
In general, a problem can be solved recursively if it can be broken down into smaller problems that are identical in
structure to the original problem.
a) What determines the “size” of a sorting problem?
b) How might we break the original problem down into smaller problems that are identical?
c) What base case(s) (i.e., trival, non-recursive case(s)) might we encounter with recursive sorts?
d) How do you combine the answers to the smaller problems to solve the original sorting problem?
e) Consider why a recursive sort might be more efficient. Assume that I had a simple n
2
sorting algorithm with
n = 100, then there is roughly 100
2
/ 2 or 5,000 amount of work. Suppose I split the problem down into two smaller
sorting problems of size 50.
If I run the n
2
algorithm on both smaller problems of size 50, then what would be the approximate amount of
work?
If I further solve the problems of size 50 by splitting each of them into two problems of size 25, then what would
be the approximate amount of work?
Data Structures Lecture 17 Name:__________________
Lecture 17 Page 1
Docsity.com
pf3
pf4

Partial preview of the text

Download Sorting Algorithms: Heap Sort and Merge Sort and more Study notes Data Structures and Algorithms in PDF only on Docsity!

  1. So far, we have looked at simple sorts consisting of nested loops. The # of inner loop iterations n*(n-1)/2 is Ο(n^2 ). Consider using a min-heap to sort a list. (methods: BinHeap(), insert(item), delMin(), isEmpty(), size())

a) If we insert all of the list elements into a min-heap, what would we easily be able to determine?

  1. Create an empty heap

Generl idea of Heap sort:

  1. Insert all n array items into heap
  2. delMin heap items back to array in sorted order

myList sorted list with n items

myList unsorted list with n items

heap with n items

b) What is the overall O ( ) for heap sort?

  1. Another way to do better than the simple sorts is to employ divide-and-conquer (e.g., Merge sort and Quick Sort). Recall the idea of Divide-and-Conquer algorithms. Solve a problem by:  dividing problem into smaller problem(s) of the same kind  solving the smaller problem(s) recursively  use the solution(s) to the smaller problem(s) to solve the original problem

In general, a problem can be solved recursively if it can be broken down into smaller problems that are identical in structure to the original problem.

a) What determines the “size” of a sorting problem?

b) How might we break the original problem down into smaller problems that are identical?

c) What base case(s) (i.e., trival, non-recursive case(s)) might we encounter with recursive sorts?

d) How do you combine the answers to the smaller problems to solve the original sorting problem?

e) Consider why a recursive sort might be more efficient. Assume that I had a simple n^2 sorting algorithm with n = 100, then there is roughly 100^2 / 2 or 5,000 amount of work. Suppose I split the problem down into two smaller sorting problems of size 50.  If I run the n^2 algorithm on both smaller problems of size 50, then what would be the approximate amount of work?

 If I further solve the problems of size 50 by splitting each of them into two problems of size 25, then what would be the approximate amount of work?

Lecture 17 Page 1Docsity.com

  1. The general idea merge sort is as follows. Assume “n” items to sort.  Split the unsorted part in half to get two smaller sorting problems of about equal size = n/  Solve both smaller problems recursively using merge sort  “Merge” the solutions to the smaller problems together to solve the original sorting problem of size n

a) Fill in the merged Sorted Part in the diagram.

b) Describe how you filled in the sorted part in the above example?

  1. Merge sort is substantially faster than the simple sorts. Let’s analyze the number of comparisons and moves of merge sort. Assume “n” items to sort.

Compares (^) Unsorted size n # Moves

Sorted size n

Unsorted size n/

Sorted size n/

Unsorted size n/

Sorted size n/

n/

n/

n/

n/

n/

n/

n/

n/

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

2

2

2 2

2 2

2

2

2

2

2

2

2

2

2 2

2 2

2

2

a) On each level of the above diagram write the WORST-CASE number of comparisons and moves for that level.

b) What is the WORST-CASE total number of comparisons and moves for the whole algorithm (i.e., add all levels)?

c) What is the big-oh for worst-case?

Lecture 17 Page 2

10

10

10

20

20

20

35

35

35

40

40

40

45

45

45

60

60

60

25

25

25

50

50

50

Unsorted Part

Sorted Part

Unsorted Left Half

Sorted Left Half

Unsorted Right Half

Sorted Right Half

0

0

0

0

0

0

4

4

1

1

1

1

1

1

5

5

2

2

2

2

2

2

6

6

3

3

3

3

3

3

7

7

Docsity.com

e) Ideally, the pivot item splits the list into two equal size problems. What would be the big-oh for Quick Sort in the best case?

f) What would be the big-oh for Quick Sort in the average case?

g) The textbook’s partition code (Listing 5.15 on page 225) selects the first item in the list as the pivot item. However, the above partition code selects the middle item of the list to be the pivot. What advantage does selecting the middle item as the pivot have over selecting the first item as the pivot?

Lecture 17 Page 4Docsity.com