CS 1050: Sorting Algorithms & Big O Notation - Problem Set 4 Solutions, Assignments of Computer Science

Solutions to problem set 4 for cs 1050: constructing proofs, focusing on sorting algorithms (merge sort, bubble sort, and insertion sort) and big o notation. It demonstrates the algorithms on given lists and analyzes their complexity.

Typology: Assignments

Pre 2010

Uploaded on 08/04/2009

koofers-user-5lo
koofers-user-5lo ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 1050: Constructing Proofs
Solutions to Problem Set 4
Due on Wed, Nov 8
Problem 1 (10 points)
Demonstrate the MERGE SORT algorithm on the following list (you need not count the number
of comparisons):
2,20,6,4,17,12,9,11,3,10,5,18,7,13,1,8
2|20|6|4|17|12|9|11|3|10|5|18|7|13|1|8
2,20|4,6|12,17|9,11|3,10|5,18|7,13|1,8
2,4,6,20|9,11,12,17|3,5,10,18|1,7,8,13
2,4,6,9,11,12,17,20|1,3,5,7,8,10,13,18
1,2,3,4,5,6,7,8,9,10,11,12,1,3,17,18,20
Problem 2 (10 points)
Demonstrate the BUBBLE SORT algorithm on the following list (you need not count the number
of comparisons):
9,5,10,7,4,2
9,5,10,7,4,2
5,9,10,7,4,2 โ€”โ€” comparison 9 <5 changed the array
5,9,7,4,2,10 โ€”โ€” comparisons 10 <7,10 <4,10 <2 changed the array
5,7,4,2,9,10 โ€”โ€” comparisons 9 <7,9<4,9<2 changed the array
5,4,2,7,9,10 โ€”โ€” comparisons 7 <4,7<2 changed the array
4,2,5,7,9,10 โ€”โ€” comparisons 5 <4,5<2 changed the array
2,4,5,7,9,10 โ€”โ€” comparison 4 <2 changed the array
Problem 3 (10 points)
Demonstrate the INSERTION SORT algorithm on the following list (you need not count the number
of comparisons):
20,6,4,17,12,9,11,3
20|6,4,17,12,9,11,3
6,20|4,17,12,9,11,3
4,6,20|17,12,9,11,3
4,6,17,20|12,9,11,3
4,6,12,17,20|9,11,3
1
pf3

Partial preview of the text

Download CS 1050: Sorting Algorithms & Big O Notation - Problem Set 4 Solutions and more Assignments Computer Science in PDF only on Docsity!

CS 1050: Constructing Proofs

Solutions to Problem Set 4

Due on Wed, Nov 8

Problem 1 (10 points)

Demonstrate the MERGE SORT algorithm on the following list (you need not count the number of comparisons): 2 , 20 , 6 , 4 , 17 , 12 , 9 , 11 , 3 , 10 , 5 , 18 , 7 , 13 , 1 , 8

2 | 20 | 6 | 4 | 17 | 12 | 9 | 11 | 3 | 10 | 5 | 18 | 7 | 13 | 1 | 8 2 , 20 | 4 , 6 | 12 , 17 | 9 , 11 | 3 , 10 | 5 , 18 | 7 , 13 | 1 , 8 2 , 4 , 6 , 20 | 9 , 11 , 12 , 17 | 3 , 5 , 10 , 18 | 1 , 7 , 8 , 13 2 , 4 , 6 , 9 , 11 , 12 , 17 , 20 | 1 , 3 , 5 , 7 , 8 , 10 , 13 , 18 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 1 , 3 , 17 , 18 , 20

Problem 2 (10 points)

Demonstrate the BUBBLE SORT algorithm on the following list (you need not count the number of comparisons): 9 , 5 , 10 , 7 , 4 , 2

5 , 9 , 10 , 7 , 4 , 2 โ€”โ€” comparison 9 < 5 changed the array 5 , 9 , 7 , 4 , 2 , 10 โ€”โ€” comparisons 10 < 7 , 10 < 4 , 10 < 2 changed the array 5 , 7 , 4 , 2 , 9 , 10 โ€”โ€” comparisons 9 < 7 , 9 < 4 , 9 < 2 changed the array 5 , 4 , 2 , 7 , 9 , 10 โ€”โ€” comparisons 7 < 4 , 7 < 2 changed the array 4 , 2 , 5 , 7 , 9 , 10 โ€”โ€” comparisons 5 < 4 , 5 < 2 changed the array 2 , 4 , 5 , 7 , 9 , 10 โ€”โ€” comparison 4 < 2 changed the array

Problem 3 (10 points)

Demonstrate the INSERTION SORT algorithm on the following list (you need not count the number of comparisons): 20 , 6 , 4 , 17 , 12 , 9 , 11 , 3

Problem 4 (10 points)

  1. Given a sorted list of m integers, and an additional integer b, show how binary search can be used to insert b into the sorted list so that the augmented list is also sorted. How many comparisons are needed? Give a reasonable upper bound.

Look at the middle integer in the sorted array of m integers. If it is less than b, b can be inserted only in the right half of the array as all the numbers in the left half will be less than b. However, if the middle integer is greater than b, then b can be inserted can be inserted only in the left half as all the numbers to the right will be greater than b. So, after one comparison, we are left with an array of size m/2 where we need to insert b. Again find the middle element in this sorted array of length m/2 and continue as before. Maximum number of comparisons required will be log 2 m.

  1. Given a list of n integers, we run the INSERTION SORT algorithm with the following mod- ification: when we insert an element in the new list, we use binary search instead of a linear search. What is the number of comparisons used by this algorithm? Give a reasonable upper bound.

According to the discussion above, inserting 1 element will take log 2 n comparisons. Since we are inserting n elements to get a sorted list, atmost nlog 2 n comparisons are needed.

Problem 5 (25 points)

For each of the following statements, answer whether it is TRUE or FALSE, with proper justifica- tion.

  1. n^3 + 7n^2 + 5n + 2 is O(n^3 ).

Proof. True โˆ€n โ‰ฅ 1 , 7 n^2 โ‰ค 7 n^3 โˆ€n โ‰ฅ 1 , 5 n โ‰ค 5 n^3 โˆ€n โ‰ฅ 1 , 2 โ‰ค 2 n^3 Therefore, n^3 + 7n^2 + 5n + 2 โ‰ค 15 n^3 โˆ€ n โ‰ฅ 1.

  1. 2n^ + 20n^3 is O(2n).

Proof. True 2 n^ is an exponential function in n while n^3 is a polynomial function in n. Exponential functions grow faster than polynomial functions.