Python Forum
Bubble sort quiz: why the result is not the same?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bubble sort quiz: why the result is not the same?
#11
here the math proof: (i continue not understand why our results are different from 1/14880).

https://www.cut-the-knot.org/Probability...orts.shtml
Reply
#12
Thank you for providing the theoretical solution.

The original question was:
Quote:1)Find the probability that the number that begins as r20 will end up, after one bubble pass, in the 30th place.
2)Find the probability that the number that begins as r20 will end up, after second bubble pass, in the 30th place.

We interpreted the above (I think correctly) as two independent questions. From the solution, the intent was really:
Quote:What is the probability of question 2, if question r20 is in the 30th place after pass 1.

Here is the code for my solution, which now matches the theoretical results.

import random

def bubble_sort_one_pass(a, pass_number):
    n = len(a)
 
    # Subtract 1 because arrays are '0 based' and Pass 1 uses index 0
    i = pass_number - 1
    for j in range(0, n-i-1):
        if a[j] > a[j+1] :
            a[j], a[j+1] = a[j+1], a[j]
    return(a)
    
def generateList():
    L = []
    remaining = [i for i in range(1,41)]
    while len(L) < 40:
        random_N = random.choice(remaining)
        L.append(random_N)
        remaining.remove(random_N)
    return L
 
N = 1000000
totalOnePass = 0
totalTwoPass = 0
for i in range(N):
    L = generateList()
    f = L[19]

    pass_number = 1
    L = bubble_sort_one_pass(L, pass_number)
    if f == L[29]:
        totalOnePass += 1
        #
        pass_number = 2
        L = bubble_sort_one_pass(L, pass_number)
        if f == L[29]:
            totalTwoPass += 1
            
print("Probability after One Pass is {} {} {} {} ".format(totalOnePass, float(N/930  ), float(totalOnePass / N), float(1/930)))
print("Probability after Two Pass is {} {} {} {} ".format(totalTwoPass, float(N/14880), float(totalTwoPass / N), float(1/14880)))
Output edited for prettyprint
Output:
Actual Theoretical Actual Theoretical Counts Counts Probability Probability Probability after One Pass is 1109 1075.3 0.001109 0.001075 Probability after Two Pass is 64 67.2 6.400e-05 6.7202e-05
Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  QUIZ GUI Form_When admin panel is open, main quiz form is getting freeze Uday 4 750 Aug-25-2023, 08:24 PM
Last Post: deanhystad
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,320 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  Python Networkx: Visualize an edge weight with a bubble/circle uvw 0 1,999 Sep-01-2021, 06:26 AM
Last Post: uvw
  Bubble sort on randomized integers bellevie 4 5,355 May-16-2017, 05:28 PM
Last Post: bellevie

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020