

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 3
This page cannot be seen from the preview
Don't miss anything!


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
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
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
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.
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.
For each of the following statements, answer whether it is TRUE or FALSE, with proper justifica- tion.
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.
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.