Insertion Sort in Python

preview_player
Показать описание
Basic implementation of insertion sort algorithm
Рекомендации по теме
Комментарии
Автор

I'm trying my luck to be a good programmer in Python by watching this Playlist.See me in 7 years or so

rockingamingwiththesahit
Автор

holy sh&t, Khan is also a programmer?

janicknorman
Автор

def insertion_sort(A):
    for j in range(1, len(A)):
        key = A[j]
        i = j - 1
        while i >= 0 and A[i] > key:
            A[i + 1] = A[i]
            i = i - 1
        A[i+1] = key
    return A

malteeaser
Автор

def insertionSort(alist):
for index in range(1, len(alist)):
currentvalue=alist[index]
position=index
while position>0 and

position-=1
alist[position]=currentvalue

yusufdemir
Автор

Wow I just noticed this Computer Science playlist, I'm going to be studying this in university next year, so this will be some nice preparation! Thanks a lot! =)

TheTenaciousE
Автор

Sure, there may be a shorter (i.e., more dry way ) to do this, but the explanation is clear. Thanks.

kamaboko
Автор

please add more lectures on python programming as the content quality was amazing

navneetbharadwaj
Автор

plz upload some more vidoes of i want to learn everything from a to z in python..and u teach awesome!

ayushmishra-oncs
Автор

@mauroprovatos the part of the list to the left of index never decreases left-to-right when beginning the outer for loop (because the list is sorted from left to right). You can think of the inner while loop as marching what was originally list[index] to the left as far as it needs. For each step of that loop the same value is getting assigned from right to left. So list[i+1] == value (equals) will be true. If the left list part weren't already sorted then the algorithm wouldn't be correct.

rckd
Автор

Back in the day when even python required 2 lines for swapping variables. :P

jasmeetsingh
Автор

please please please give more lectures on algorithms :)

BYZ
Автор

This code doesn't work on my Apple device.

techguy
Автор

by list[i+1] = list [i
wont the initial value of list[i+1] be lost forevah?

mauroprovatos
Автор

a = [5, 1, 7, 2, 8, 3, 9]
a.sort()
print a

done.

DrStevoXD
Автор

The if statement in the loop is obsolete. It could and should be included in the while condition. This way the break statement would be avoided.

hkdobrev
Автор

I dont understand why you decrease i... it begins with value zero then later you minus 1... confused

lesleymitchell
Автор

Thank you so much for explaining this in detail. There's actually a minor problem with your code. The while loop doesn't break if the second item is greater than first item.
So one suggestion is that you decrement i by 1 at the end of the while loop besides the decrement of i within if function.

angeluffviiz
Автор

Is this available with strings instead of numbers?

Giorgio
Автор

def sort(a):
A=[]
for i in range(len(a)):
A.append(min(a))
a.remove(min(a))
return A
this is even simpler

TimeDilationAuthority
Автор

I am looking for more sorting algorithms like Bubble Sort, Selection Sort, Merge Sort, Quick Sort ... etc

BiranchiNarayanNayak