
def bubble_sort(alist):
    count = 0
    count2 = 0
    numpasses = len(alist) - 1

    for i in range(1,numpasses+1):
        for j in range(0, len(alist) - i):
            count += 1
            if alist[j] > alist[j+1]:
                count2 += 1
                # swap the elements at j and j+1
                temp = alist[j+1]
                alist[j+1] = alist[j]
                alist[j] = temp

    return (count, count2)



def selection_sort(alist):
    count = 0
    count2 = 0
    numpasses = len(alist) - 1

    for i in range(1,numpasses+1):
        maxidx = 0
        for j in range(1, len(alist) - i + 1):
            count += 1
            if alist[j] > alist[maxidx]:
                maxidx = j  # this keeps track of where the max value is

        # swap the element at maxidx with the element at the "end"
        count2 += 1
        temp = alist[maxidx]
        alist[maxidx] = alist[ len(alist) - i ]
        alist[ len(alist) - i ] = temp

    return (count, count2)


mylist = [1,7,4,5,2,15,12,50,40,30,11,12,17,16,22,21]
print(f'Before sorting with bubblesort:\n   {mylist}')

(numcompares, numswaps) = bubble_sort(mylist)

print(f'After sorting with bubblesort:\n   {mylist}')
print(f'Number of compares = {numcompares}, Number of copies = {3*numswaps}')

mylist = [1,7,4,5,2,15,12,50,40,30,11,12,17,16,22,21]
print(f'Before sorting with selectionsort:\n   {mylist}')

(numcompares, numswaps) = selection_sort(mylist)

print(f'After sorting with selectionsort:\n   {mylist}')
print(f'Number of compares = {numcompares}, Number of copies = {3*numswaps}')

