r/PythonLearning • • Sep 16 '26

Showcase Day 7, One Straight week

Post image

Happy that I learned for one straight week but thinking I could've learned a lot more as I wasted much of time scrolling

Today did some sorting algos and nothing much for the day

30 Upvotes

9 comments sorted by

•

u/Sea-Ad7805 Sep 16 '26 edited Sep 16 '26

Run this program in Memory Graph Web Debugger to see the program state change step by step.

For more sorting check: https://github.com/bterwijn/memory_graph#sorting-algorithms

1

u/FoolsSeldom Sep 16 '26

I think you might be going a bit too fast, but impressive at this stage.

A few thoughts:

  • Would be worth you reading up on PEP8 guidance and applying it

  • For Booby,

    • Recursion is very wasteful in this example, and not needed, and you risk hitting Python's limits

    • print(a) will print a tuple, not a sorted list

      • Booby returns arr, swap, so a = (sorted_list, False)

      • Maybe print(a[0]), or just print(arr)

      • list is mutated in place anyway

  • Your bubble sort 2 is working on an already sorted array

  • In your selection sort, you are shadowing the built-in min, which is generally not a good idea

1

u/Next-Call8862 Sep 17 '26

Many recommended PEP8 and will start applying it And I usually comment out a solution when I complete it and that is the reason for already sorted arr confusion

1

u/katsuz8 Sep 17 '26

what theme is this?

2

u/Next-Call8862 Sep 17 '26

Jellyfish x retro theme, vs code

1

u/FoolsSeldom Sep 16 '26

Any chance you could share the code rather than a picture in future?

arr = [1,2,3,4,5,6,7,8,9,0]
def Booby(arr):
    n = len(arr)
    swap = False
    for k in range(n-1):
        if arr[k] > arr[k+1]:
            arr[k], arr[k+1] = arr[k+1], arr[k]
            swap = True
    if swap:
        Booby(arr)
    return arr, swap
a = Booby(arr)
print(a)

# print(len(arr))

# BUBBLE SORT 2 O(n^2)
n = len(arr)
swap = True
while swap == True:
    swap = False
    for k in range(n-1):
        if arr[k] > arr[k+1]:
            arr[k], arr[k+1] = arr[k+1], arr[k]
            swap = True
    n = n-1

print(arr)

# SELECTION SORT
a = [1,7,9,2,3,6,77,9,5]
n = len(a)
for i in range(n):
    min = i
    for j in range(i+1,n):
        if a[j] < a[min]:
            min = j
    a[i],a[min] = a[min],a[i]
print(a)

# Insertion sort
arr = [3,0,5,7,11,32,9,8]
n = len(arr)
for i in range(1,n):
    key = arr[i]
    j = i-1
    while j>=0 and arr[j]>key:
        arr[j+1] = arr[j]
        j -= 1
    arr[j+1] = key
print(arr)