Question

Working on some stuff and I came into an odd issue that I am having trouble figuring out. I am sorting a list with 10,000 values in it two ways, one with the usage of quick select, and another with the usgae of insertion sort. The goal of this is to find a median and then using said median I have to find the total distance between the median value and all of the values. The median calculations work perfectly fine, but the total calculations, for reasons I cannot understand return different values. The inputs into the function that calculates the total are the list and the median. The median stays the same between both programs and the values of the list do as well however one of the lists is sorted and the other list is not sorted.

Here is what I am using to calculate the total (formatting on this is fine it just copies into here weird)...

def ttlDist(lst, med):
    total = 0
    for i in lst:
        total = abs(med - i)
    print(total)

With one list being sorted and another not being sorted why would I be getting drastically different values? For refrence the distance I get when using insertion sort is 49846.0 but the distance I get when using quickselect is 29982

Was it helpful?

Solution

You're not accumulating anything; you're replacing total with a new value each time through the loop. So, at the end of the loop, total is the value from the last element of lst. A sorted list and an unsorted list will generally have different last elements.

What you probably wanted is:

total += abs(med - i)

Or, more simply, replace the whole function with:

total = sum(abs(med-i) for i in lst)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top