Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6561797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:37:54+00:00 2026-05-25T13:37:54+00:00

I’m reading some books on Python, data structures, and analysis and design of algorithms.

  • 0

I’m reading some books on Python, data structures, and analysis and design of algorithms. I want to really understand the in’s and out’s of coding, and become an efficient programmer. It’s difficult to ask the book to clarify, hence my question on stackoverflow. I’m really finding Algorithms and recursion challenging … I posted some code (insertion sort) below that I’m trying to understand exactly what’s happening. I understand, generally, what is supposed to happen, but I’m not really getting the how and why.

From trying to analyze pieces of the code on Python Idle, I know that:

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

and that:

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

I don’t 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:37:54+00:00Added an answer on May 25, 2026 at 1:37 pm

    Let me try to break this down.

    Start by considering a list. It is “almost” sorted. That is, the first few elements are sorted, but the last element is not sorted. So it looks something like this:

    [10, 20, 30, 50, 15]
    

    Obviously, the 15 is in the wrong place. So how do we move it?

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

    That’ll switch around the 15 and the 50 so now the list looks like:

    [10, 20, 30, 15, 50]
    

    But we’d like to do this several times in a loop. To do that we can do:

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

    That loop will go back one position at a time swapping the two elements. That’ll move the out of order position one place back each time. But how do we know when to stop?

    Let’s look again at our list and the moves we want to make:

    [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!
    

    But what is different that last time around? Every time we move the number one place back, it is because the 15 is less then the element on the left, meaning its not sorted. When that is no longer true we should stop moving. But we can easily deal with that:

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

    Ok, but happens if we now try to sort this list:

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

    At this point something bad happens. We try to check whether key < mylist[i-1] but when we’ve reached the beginning, i = 0, and this checks the end of the list. But we should stop moving to left at this point…

    If we reach the beginning of the list, we can’t move our pivot/key further so we should stop. We update our while loop to handle that:

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

    So now we have a technique for sorting an almost sorted list. But how can we use that to sort a whole list? We sort parts of the list at a time.

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

    First we sort the first 1 elements:

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

    Then we sort the first 2 elements:

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

    Then we sort the first 3 elements

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

    So on and so forth

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

    But how do we do we do that? With a for loop

    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 
    

    But we can skip the first time through, because a list of one element is obviously already sorted.

    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 
    

    A few minor changes which make no difference brings us back to your original code

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i want to parse a xhtml file and display in UITableView. what is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.