====== Differences ====== This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
gibson:teaching:fall-2013:math445:hw2 [2013/09/15 18:15] gibson removed |
gibson:teaching:fall-2013:math445:hw2 [2013/09/17 05:35] (current) gibson |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Math 445 HW2: Bubble sort ====== | ||
- | Write a function ''mysort.m'' that will take in a vector of any size and 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! |
- | it from least to greatest. Use the strategy of comparing neighbors and swapping | + | |
- | them if necessary. If this process is done throughout the entire vector | + | **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. |
- | repeatedly enough times, it will work. Write your code so that it does not | + | |
- | go through the matrix too many times. | + | **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? |