User Tools

Site Tools


gibson:teaching:fall-2013:math445:hw2

====== Differences ====== This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
gibson:teaching:fall-2013:math445:hw2 [2013/09/16 18:15]
gibson created
gibson:teaching:fall-2013:math445:hw2 [2013/09/17 05:35] (current)
gibson
Line 1: Line 1:
-====== Math 445 HW2 ======+====== Math 445 HW2: Bubble sort ======
  
-**Problem 1.** Write a Matlab function ''​mysort''​ that takes an input vector ''​x'',​ rearranges its components so they are in increasing order, and then returns the sorted vector. For example, ''​mysort([0.38 0.57 0.22])''​ should return ''​[0.22 0.48 0.57]''​. Your ''​mysort''​ function should use the "​bubble sort" algorithm and just basic comparisons and control-flow operations. That is, no cheating by using Matlab'​s ''​sort''​ function! ​+**Problem 1.** Write a Matlab function ''​mysort''​ that takes an input vector ''​x'',​ rearranges its components so they are in increasing order, and then returns the sorted vector. For example, ''​mysort([0.38 0.57 0.22])''​ should return ''​[0.22 0.38 0.57]''​. Your ''​mysort''​ function should use the "​bubble sort" algorithm and just basic comparisons and control-flow operations. That is, no cheating by using Matlab'​s ''​sort''​ function! ​
  
 **Problem 2.** Test ''​mysort''​ on a random vector of length 4. That is, set ''​x = rand(4,​1)''​ and then ''​y = mysort(x)''​. Verify by eye that ''​x''​ and ''​y''​ have the same set of elements and that ''​y''​ is sorted correctly. If there are any problems, go back and fix your ''​mysort''​ function. **Problem 2.** Test ''​mysort''​ on a random vector of length 4. That is, set ''​x = rand(4,​1)''​ and then ''​y = mysort(x)''​. Verify by eye that ''​x''​ and ''​y''​ have the same set of elements and that ''​y''​ is sorted correctly. If there are any problems, go back and fix your ''​mysort''​ function.
  
-**Problem 3.** Run ''​mysort''​ on a random vector of length 1000. Checking this by eye would take too long and would not be reliable. Come up with a simple way to test the correctness of the sort with Matlab code. +**Problem 3.** Run ''​mysort''​ on a random vector of length 1000. Checking this by eye would take too long and would not be reliable. Come up with a simple way to test the correctness of a large sort with Matlab code, and then use it to verify correctness.  
 + 
 +**Problem 4.** Determine whether your ''​mysort''​ function is faster or slower than Matlab'​s ''​sort''​ function by using the ''​tic''​ and ''​toc''​ functions. Compare for a vectors of a few different size (say 100, 1000, and 10,000)Does length make any difference?
gibson/teaching/fall-2013/math445/hw2.1379380518.txt.gz · Last modified: 2013/09/16 18:15 by gibson