Trying to understand insertion sorting algorithm

I read several books on Python, data structures, and analysis and algorithm development. I want to really understand coding and get out of it and become an effective programmer. It is hard to ask the book to clarify, so my question is about stackoverflow. I really find Algorithms and recursion complicated ... I posted some code (insertion sort) below that I'm trying to understand what exactly is going on. I understand, as a rule, what should happen, but I really do not understand how and why.

From trying to parse the Python Idle code fragments, I know that:

key (holds variables) = 8, 2, 4, 9, 3, 6

So what:

i (holds the length) = 7 ( 1, 2, 3, 4, 5, 6, 7)

I do not know why 1 is used in the first line: range (1, len (mylist)). Any help is appreciated.

mylist = [8, 2, 4, 9, 3, 6]

for j in range(1,len(mylist)):
    key = mylist[j]
    i = j
    while i > 0 and mylist[i-1] > key:
        mylist[i] = mylist[i - 1]
        i -= 1
        mylist[i] = key
+5
7

.

. "" . , , . , :

[10, 20, 30, 50, 15]

, 15 . , ?

    key = mylist[4]
    mylist[4] = mylist[3]
    mylist[3] = key

15 50, :

[10, 20, 30, 15, 50]

. :

while ???:
    key = mylist[i]
    mylist[i] = mylist[i-1]
    mylist[i-1] = key
    i -= 1

, . , . , ?

, :

[10, 20, 30, 50, 15]
[10, 20, 30, 15, 50]
[10, 20, 15, 30, 50]
[10, 15, 20, 30, 50]
# stop! we are sorted now!

? , , , 15 , . , . :

key = mylist[i]
while key < mylist[i-1]:
    mylist[i] = mylist[i-1]
    mylist[i-1] = key
    i -= 1

, , :

[10, 20, 1]  [10, 1, 20]  [1, 10, 20]  # ERROR!!

- . , < mylist [i-1], , = 0, . ...

, , . while, :

key = mylist[i]
while i > 0 and key < mylist[i-1]:
    mylist[i] = mylist[i-1]
    mylist[i-1] = key
    i -= 1

, . ? .

 [8, 2, 4, 9, 3, 6]

1 :

 [8, 2, 4, 9, 3, 6]

2 :

 [2, 8, 4, 9, 3, 6]

3

 [2, 4, 8, 9, 3, 6]

, ..

 [2, 4, 8, 9, 3, 6]
 [2, 4, 8, 9, 3, 6]
 [2, 3, 4, 8, 9, 6]
 [2, 3, 4, 6, 8, 9]

?

for j in range(len(mylist)):
    i = j
    key = mylist[i]
    while i > 0 and key < mylist[i-1]:
        mylist[i] = mylist[i-1]
        mylist[i-1] = key
        i -= 1 

, , , .

for j in range(1, len(mylist)):
    i = j
    key = mylist[i]
    while i > 0 and key < mylist[i-1]:
        mylist[i] = mylist[i-1]
        mylist[i-1] = key
        i -= 1 

, ,

for j in range(1, len(mylist)):
    key = mylist[j]
    i = j
    while i > 0 and key < mylist[i-1]:
        mylist[i] = mylist[i-1]
        i -= 1 
        mylist[i] = key
+12

, . , , , .., n , .

,

3  1  4

:

| 3  1  4

3 . , :

3 | 1  4

1 . 1 :

3 1 | 4

. , 1 , . 1 3:

1 3 | 4

1 , . , i > 0; (i) , .

, , 4 . , :

1 3 4

.

, : 1? . , . , , . ,

2  7  1  8

: :

| 2  7  1  8

, :

2 | 7  1  8

, .

. , , , .

, !

+6

while. i, 1, i . , i 0, . 0, i -1, python, . 1.

, . , . - .

+2

, :

i = j

mylist:

mylist[i - 1]

0. 0, , -1.

+1

i = j, myList[i-1]. , j j >= 1.

: j = 0 , myList[j-1] - ( = j). - while i > 0, , , . myList[j-1], j >= 1.

+1

The j-iteration inserts the j-th element into the sorted elements up to j. Therefore, it makes no sense to start with j = 0. In the case j = 1, the subcomputer is lower myList[0:1], which is sorted in all directions, and the loop inserts myList[1]into the sublistmyList[0:2]

+1
source

Watch the animated InsertionSort HERE

+1
source

All Articles