# hw6 final - A1 The number of movements to bubble sort in...

This preview shows pages 1–4. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: A1. The number of movements to bubble sort, in ascending order, the following lists: a. [0 4 3 5 7 9 1] 6 movements b. [7 6 5 4 3 1] 15 movements c. [1 4 7 8 9 0] 5 movements A2. After the first pass the highest number is in the last spot. After the second pass the highest number is in the last spot and the second highest number is in the second to last spot. Once you have the highest number of the list, you can automatically A3. tic, insertsort(rand(100,1)); toc Elapsed time is 0.006000 seconds. tic, insertsort(rand(500,1)); toc Elapsed time is 0.009000 seconds. tic, insertsort(rand(1000,1)); toc Elapsed time is 0.030000 seconds. tic, insertsort(rand(5000,1)); toc Elapsed time is 0.642000 seconds. tic, insertsort(rand(10000,1)); toc Elapsed time is 2.520000 seconds. tic, insertsort(rand(100,1)); t1 = toc; tic, insertsort(rand(500,1)); t2 = toc; tic, insertsort(rand(1000,1)); t3 = toc; tic, insertsort(rand(5000,1)); t4 = toc; tic, insertsort(rand(10000,1)); t5 = toc; t = [t1 t2 t3 t4 t5]; r = [100 500 1000 5000 10000]; plot(r,t,'r-') title('Time to run insertsort.m'); xlabel('Number of random numbers'); ylabel('Time (sec)'); p = polyfit(r,t,2); r2 = linspace(min(r),max(r),1000); y = polyval(p,r2); hold on plot(r2,y,'b--') legend('data points','function',2) str=sprintf('time elapsed for a quantity of randomly sorted numbers\nto be sorted in increasing order along with a quadratic model fit') title(str) ylabel('time (seconds)') xlabel('quantity of randomly sorted numbers') 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 0.5 1 1.5 2 2.5 3 time elapsed for a quantity of randomly sorted numbers to be sorted in increasing order along with a quadratic model fit quantity of randomly sorted numbers time (seconds) data points function A4. function [R] = insertsortn(R,n) for j = 2:length(R) i=j-1; temp=R(j); if 1>n for k=[1:n:i,i] if temp<R(k) for p=k-n:k if temp<R(k) R(k+1:j)=R(k:j-1); R(k)=temp; end end end end else for k = 1:i if temp < R(k) R(k+1:j)=R(k:j-1); R(k)=temp; break end end end end R = [100 500 1000 5000 10000]; for n=1:4 for j=1:length(R) tic u=rand(R(j),1); insertsortn(u,n); t(n,j) = toc*1000; end end toc fprintf('---------------------times in mSecs-------------------------\n')...
View Full Document

{[ snackBarMessage ]}

### Page1 / 9

hw6 final - A1 The number of movements to bubble sort in...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online