I have been staring at this problem for an hour havent figured it out. Any help would be appreciated

조회 수: 2 (최근 30일)
Write a script to compare the respective times tq(n) and tb(n) that it takes to sort a random vector of length n as a function of n for n ranging from 1 to numbers as large as 100000 or more. Prepare a well labelled plot of the ratio of tb(n)/tq(n) versus n, where tb is time for the bubble sort and tq is the time for the quicksort employed by MATLAB. On the same plot, plot the ratio of the averages mean(tb(:))/mean(tq(:)) versus n.

채택된 답변

Star Strider
Star Strider 2014년 12월 13일
Assuming you already know what the bubble sort algorithm is and have programmed it, and can use the MATLAB sort function, all you need then are the tic and toc functions (the timeit function is also a possibility) to do the timing.
For the vectors, use any of the random number functions: rand, randi, randn, or any of the others for your vector of numbers to be sorted. Obviously, use the same random number vector for both algorithms to do the comparison. I would use a loop to generate a new random number vector each time, do the timing of the two algorithms, and store the results, but there may be other methods.
  댓글 수: 3
Image Analyst
Image Analyst 2014년 12월 14일
startTime = tic
bubbleResults = BubbleFunction()
elapsedTimeBubble = toc(startTime)
startTime = tic
quickResults = QuickFunction()
elapsedTimeQuick = toc(startTime)
Star Strider
Star Strider 2014년 12월 14일
Thank you, IA. (I had a glitch and forced reboot a few minutes ago.)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Seth
Seth 2014년 12월 14일
Could you further elaborate on how to write a loop that generates a new random vector and then relating it to the timing functions? I am lost
  댓글 수: 3

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by