User Tools

Site Tools


gibson:teaching:fall-2013:math445: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.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 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.txt · Last modified: 2013/09/17 05:35 by gibson