2
def my_sort(array):
    length_of_array = range(1, len(array))
    for i in length_of_array:
        value = array[i]
        last_value = array[i-1]
        if value<last_value:
            array[i]=last_value
            array[i-1]=value
            my_sort(array)
    return array

I know what the function does in general. Its a sorting alogarithm.... But i dont know how what each individual part/section does.

2 Answers2

3

Well, I have to say that the best way to understand this is to experiment with it, learn what it is using, and, basically, learn Python. :)

However, I'll go through the lines one-by-one to help:

  1. Define a function named my_sort that accepts one argument named array. The rest of the lines are contained in this function.

  2. Create a range of numbers using range that spans from 1 inclusive to the length of array non-inclusive. Then, assign this range to the variable length_of_array.

  3. Start a for-loop that iterates through the range defined in the preceding line. Furthermore, assign each number returned to the variable i. This for-loop encloses lines 4 through 9.

  4. Create a variable value that is equal to the item returned by indexing array at position i.

  5. Create a variable last_value that is equal to the item returned by indexing array at position i-1.

  6. Test if value is less than last_value. If so, run lines 7 through 9.

  7. Make the i index of array equal last_value.

  8. Make the i-1 index of array equal value.

  9. Rerun my_sort recursively, passing in the argument array.

  10. Return array for this iteration of the recursive function.

When array is finally sorted, the recursion will end and you will be left with array all nice and sorted.

I hope this shed some light on the subject!

Community
  • 1
  • 1
0

I'll see what I can do for you. The code, for reference:

def my_sort(array):
    length_of_array = range(1, len(array))
    for i in length_of_array:
        value = array[i]
        last_value = array[i-1]
        if value<last_value:
            array[i]=last_value
            array[i-1]=value
            my_sort(array)
    return array

def my_sort(array): A function that takes an array as an argument.

length_of_array = range(1, len(array)) We set the variable length_of_array to a range of numbers that we can iterate over, based on the number of items in array. I assume you know what range does, but if you don't, in short you can iterate over it in the same way you'd iterate over a list. (You could also use xrange() here.)

for i in length_of_array:
    value = array[i]
    last_value = array[-1]

What we're doing is using the range to indirectly traverse the array because there's the same total of items in each. If we look closely, though, value uses the i as its index, which starts off at 1, so value is actually array[1], and last_value is array[1-1] or array[0].

    if value<last_value:
        array[i]=last_value
        array[i-1]=value

So now we're comparing the values. Let's say we passed in [3, 1, 3, 2, 6, 4]. We're at the first iteration of the loop, so we're essentially saying, if array[1], which is 1, is less than array[0], which is 3, swap them. Of course 1 is less than 3, so swap them we do. But since the code can only compare each item to the previous item, there's no guarantee that array will be properly sorted from lowest to highest. Each iteration could unswap a properly swapped item if the item following it is larger (e.g. [2,5,6,4] will remain the same on the first two iterations -- they will be skipped over by the if test -- but when it hits the third, 6 will swap with 4, which is still wrong). In fact, if we were to finish this out without the call to my_sort(array) directly below it, our original array would evaluate to [1, 3, 2, 3, 4, 6]. Not quite right.

        my_sort(array)

So we call my_sort() recursively. What we're basically saying is, if on the first iteration something is wrong, correct it, then pass the new array back to my_sort(). This sounds weird at first, but it works. If the if test was never satisfied at all, that would mean each item in our original list was smaller than the next, which is another way (the computer's way, really) of saying it was sorted in ascending order to begin with. That's the key. So if any list item is smaller than the preceding item, we jerk it one index left. But we don't really know if that's correct -- maybe it needs to go further still. So we have to go back to the beginning and (i.e., call my_sort() again on our newly-minted list), and recheck to see if we should pull it left again. If we can't, the if test fails (each item is smaller than the next) until it hits the next error. On each iteration, this teases the same smaller number leftward by one index until it's in its correct position. This sounds more confusing than it is, so let's just look at the output for each iteration:

[3, 1, 3, 2, 6, 4]
[1, 3, 3, 2, 6, 4]
[1, 3, 2, 3, 6, 4]
[1, 2, 3, 3, 6, 4]
[1, 2, 3, 3, 4, 6]

Are you seeing what's going on? How about if we only look at what's changing on each iteration:

[3, 1, ...          # Wrong; swap. Further work ceases; recur (return to beginning with a fresh call to my_sort()).
[1, 3, 3, 2, ...    # Wrong; swap. Further work ceases; recur
[1, 3, 2, ...       # Wrong; swap. Further work ceases; recur
[1, 2, 3, 3, 6, 4   # Wrong; swap. Further work ceases; recur
[1, 2, 3, 3, 4, 6]  # All numbers all smaller than following number; correct.

This allows the function to call itself as many times as it needs to pull a number from the back to the front. Again, each time it's called, it focuses on the first wrong instance, pulling it one left until it puts it in its proper position. Hope that helps! Let me know if you're still having trouble.

Chase Ries
  • 2,094
  • 2
  • 15
  • 16