Apr-25-2018, 01:44 PM
Your code seems a little complicated, but I agree with your answers.
For 1,000,000 passes I got the following results using my code:
For 1,000,000 passes I got the following results using my code:
Output:Pass 1 1090 counts Frequency 0.00109
Pass 2 2042 counts Frequency 0.00204
You do not need two algorithms. You could use something like: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(float(totalOnePass / N))) print("Probability after Two Pass is {}".format(float(totalTwoPass / N)))Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00