Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insertion Sort clean-up possibility #4

Open
mburke05 opened this issue Dec 28, 2015 · 1 comment
Open

Insertion Sort clean-up possibility #4

mburke05 opened this issue Dec 28, 2015 · 1 comment

Comments

@mburke05
Copy link
Contributor

I was just thinking about use of the _ placeholder, and then beginning the start of the algorithm on range(2 rather than range(1. It seems sort of unpythonic and counter-intuitive to me. A very small thing, but I was thinking maybe it should or could be rewritten to not use the _ placeholder, which to my knowledge doesn't do anything here? Or we could rewrite it using something like this:

def insertion_sort(lst):
    for i in xrange(1, len(lst)): 
        position = i
        while position > 0 and lst[position] < lst[position-1]:
            lst[position], lst[position-1] = lst[position-1], lst[position]
            position = position - 1 
    return lst

etc.

Maybe I'm missing something though, is the "_" used to catch something I'm not seeing?

@scottnm
Copy link
Contributor

scottnm commented Dec 28, 2015

Ah you were definitely correct! the insertion sort placeholder was unnecessary. And it seems that the standard pseudocode for insertion sort does something similar to what you've written so I'm going to adopt something similar as well! Good catch! After doing some timing tests though I am not sure if it is faster to do the slicing that was in my original algorithm or just do a bunch of swaps like in your algorithm. I'll probably get around to timing it eventually

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants