Created
November 25, 2019 18:52
-
-
Save hsauers5/b643cb7179b2d17592c7d2077790620f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def check_if_sorted(arr): | |
n = len(arr) | |
for i in range(n): | |
for j in range(0, n-i-1): | |
if arr[j] > arr[j+1]: | |
return False | |
arr[j], arr[j+1] = arr[j+1], arr[j] | |
return True | |
def first_pass_bubble_sort(arr): | |
n = len(arr) | |
# for i in range(n): | |
# for j in range(0, n-i-1): | |
for j in range(0, n-1): | |
if arr[j] > arr[j+1]: | |
arr[j], arr[j+1] = arr[j+1], arr[j] | |
return arr | |
def generate_all_permutations(my_set): | |
if len(my_set) == 0: | |
return [] | |
if len(my_set) == 1: | |
return [my_set] | |
this_permutation = [] | |
for i in range(len(my_set)): | |
m = my_set[i] | |
temp_set = my_set[:i] + my_set[i+1:] | |
for p in generate_all_permutations(temp_set): | |
this_permutation.append([m] + p) | |
return this_permutation | |
my_set = [1,2,3,4,5] | |
all_perms = generate_all_permutations(my_set) | |
perms_count = len(all_perms) | |
first_pass_count = 0 | |
others_count = 0 | |
for perm in all_perms: | |
sorted_perm = first_pass_bubble_sort(perm) | |
if check_if_sorted(sorted_perm): | |
first_pass_count += 1 | |
else: | |
others_count += 1 | |
print(perms_count) | |
print(first_pass_count) | |
print(others_count) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment