|
|
#13 (permalink) |
|
TestMagic Guru-in-Training
![]() ![]() ![]() Join Date: Oct 2003
Location: Albania
Posts: 534
![]() |
Q15) On average, how many exchanges are performed by insertion sort on n elements? How about selection sort? How about bubble sort?
According to Sedgewick (and I’m not sure I trust the answer for bubble) Bubble: O(n*n/2) Nonevent here is what I get for bubble, maybe you can trust me, forget Sedgewick ![]() elements in bubble (smallest elem on top) x x1 probability of that x1 will bubble over x is 1/2 x x1 x2 x3 Code:
probability of that x1 will bubble over x is 1/2, probability that x2 will bubble 1 spot = 1/2, (if x2 < x1, x2 bubles 1 spot) probability that x2 will bubble 2 spots = 1/2, (if x2 < x, x2 bubles 2 spots) probability that x3 will bubble 1 spot = 1/2, (if x3 < x2, x3 bubles 1 spot) probability that x3 will bubble 2 spots = 1/2, (if x3 < x1, x3 bubles 2 spots) probability that x3 will bubble 3 spots = 1/2, (if x3 < x, x3 bubles 3 spot) ............. Now we can see a trend develeping 1/2 + 2/2 +3/2 + 4/2 ................... = 1/2 * sum(k=1 to n-1)(k) = 1/2(n-1)(n-2) = O(n*n/4) |
|
|
|
|
|
#14 (permalink) | |
|
TestMagic Guru-in-Training
![]() ![]() ![]() Join Date: Oct 2003
Location: USA
Posts: 508
![]() |
Quote:
Then S = sum(k=1 to n-1)[x/2] = 1/2 * sum(k=1 to n-1)(k) = 1/2(n-1)(n-2)/2 = n*n/4 Moreover, if I experimentally attempt to verify that the answer is n*n/4 using an applet such as http://www.eca.com.ve/cs/it_page/Sor...ble%20sort.htm it invariably comes back as something very close to n*n/4 rather than n*n/2. Finally, the usual bubble sort typically has n*(n-1)/2 =approx n*n/2 comparisons. I would expect that half of the comparisons would lead to swaps. That yields n*n/4. These are the reasons I'm skeptical of Sedgewick. It just doesn't appeal to my gut. What's your take? |
|
|
|
|
|
|
#15 (permalink) |
|
TestMagic Guru
![]() ![]() ![]() ![]() Join Date: Jul 2003
Location: Brazil
Posts: 1,360
![]() |
That's true nonevent99. Looks like O(n*n/2) is a thigh upper bound for the worst case scenario. Your link shows that worst case (10 elements and 45 swaps). But in average, you'll get O(n*n/4) as you pointed out.
Wood |
|
|
|
|
|
#18 (permalink) |
|
Retired
![]() ![]() ![]() ![]() Join Date: Feb 2006
Posts: 2,256
![]() |
*** bump ***
_ _ _ _ SIG _ _ _ _
Admit Profiles, CS Internships, TopCoder, Programming Challenges Applying to Ph.D. Programs in Computer Science GRE Computer Science Subject Test: ETS Booklet (solutions at Yahoo GRECS group), MFT, Titanium Bits, Guide, Ullman CS Book, Algorithms, Computer Architecture, Old Links more CS practice: Stanford Comps GATE CS/IT: 2009 Solutions, GATEForum, Yahoo, Freshers, Q & A, Mock Exams & Solutions, GATEMentor |
|
|
|
|
|
#19 (permalink) |
|
Eager!
Join Date: Oct 2007
Posts: 31
![]() |
Q3) How about {ww% : w is in {0,1}*}, % meaning "reverse"
PDA, S-> e | 1S1 | 0S0 (Thanks for the correction, Wood) I check our notes in my class, WW% cannot be recognizable by any DPA. I think if we can figure out the CFL, it doesn't means it can be recognizable by DPA. NPDA can recognize some CFL that PDA cannot. Am I right? |
|
|
|
Contact TestMagic TestMagic Forums Archive Privacy Statement
TestMagic Locations
Legal
Privacy
SEO by vBSEO 3.2.0
Copyright © 2009 TestMagic
Ad Management by RedTyger