Python: Insertion Sort algorithm

preview_player
Показать описание
Insertion Sort algorithm explained in animated demo, with example Python 3 code implementation.

PYTHON SORTING ALGORITHMS

#Python
Рекомендации по теме
Комментарии
Автор

A quick tip for anyone: you don’t need a temp variable when swapping two variables. In python, you can write the following code:
x, y = y, x
This swaps both of the variables in one line because python allows recursive referencing of variables, and it allows multiple identifiers to be assigned in one line.

hunterrees
Автор

2:57
correction required:: for j in range(i-1, -1, -1):

check for test case:: A=[10, 9, 8, 5, 3, 7, 92, 185, 78, 1, 0, 65]

shivamkumarrai
Автор

sir there is an error in the first code as in there 0th index will not be included hence first element would not be sorted it should be - for j in range(i-1, -1, -1) thanks sir for posting such videos and making people good learners you have helped me to gain interest and made me capable of finding bugs
xoxoxoxo :)

vanshgulati
Автор

best teacher of sorting algorithms in python i found on youtube so far... Thank you Joe James.

soseofficial
Автор

Thanks for all these videos, Joe. I've been programming in Python for about a year now, but none of the books I've read teach anything about algorithms or data structures. I finally feel like I'm learning REAL computer science. Cheers.

randyrichardson
Автор

Thanks for the videos Sir, two corrections required in the code are, Arr[j]=currNum inside the if loop so that the currnum value is retained, second is for range is (i-1, -1, -1)

sathyanarayanankulasekaran
Автор

this video is still usefull after 5 thank you sir, great explanation

hrithikrana
Автор

Yeah, really great job, man. I know this is a bit old but it's still a goodie. Having been an educator in a different area of study for many years, I honestly have to say that what charmed me the most was that you explained each line and what was happening in the algorithm by referring to what is left or right. So many other youtubers have forgotten to keep this compass rose available for listeners and simple rushed through the code.

yourdadishere
Автор

Thank you for the video.
I think the stop parameter of the second for-loop should be -1, not 0.

for j in range(i-1, -1, -1)

abhishekparida
Автор

For those who are struggling to keep up with this video, this is the correct and working code for the nested loop version:
def insertion(lst):
for i in range(1, len(lst)):
print(i)
for j in range(i-1, -1, -1):
print(j)
if lst[j]>lst[j+1]:
lst[j], lst[j+1]=lst[j+1], lst[j]
print(lst)
else:
break
return lst


# "Optimized" insertion sort algo based on nesterd loops
def insertion(A):
for i in range(1, len(A)):
curNum=A[i]
for j in range(i-1, -1, -1):
if A[j] > curNum:
A[j+1] = A[j]
A[j]=curNum
else:
break
return A
#The code below will t

drustan
Автор

Thanks, I did the code and realized the first index was never sorted. Took me a while to solve the issue on my own, but later realized that the answer was down in the comment section lol.

AH-xbeb
Автор

Been trying this several times to sort in ascending order and below is code which worked for me, hope it helps someone else. By the way Joe thanks for the excellent videos.

def insertion_sort(A):
for i in range(1, len(A)+1):
for j in range(i-1, 0, -1 ):
if A[j] > A[j-1]:
break
else:
A[j-1], A[j] = A[j], A[j-1]
return A

francisssemwanga
Автор

set 0 in the for loop parameter for the ending index to -1 and it should work fine

kerodfresenbetgebremedhin
Автор

Mr.Joe u very beautifully explained the insertion sort 😊

shikhatripathi
Автор

consideration : The left of the current number is already sorted
You can use binary search method for comparision and then swap that will decrease time complexity
O(n^2) O(nlogn)

arpankumarlahiri
Автор

There are 2 problems in this python code with the 2 for loops.
for j in range(i - 1, 0, -1) is not valid. You are missing the first 2 numbers. It should be for j in range(i - 1, -1, -1)
The if min_value < a_list[j]: ... else also does not work if you have only 2 numbers to swap. If you have 2 numbers, the code never enters the else and the first value is not overwritten by the min_value

yesme
Автор

Another way of doing the same:

def InsertionSort(Sortee):
for index, item in enumerate(Sortee):
if index==0:
continue
ti=index
while item<Sortee[ti-1]:
Sortee[ti], Sortee[ti-1] = Sortee[ti-1], Sortee[ti]
ti=ti-1
if ti==0:
break

shanmalik
Автор

thank you Joe James for these lovely tutorials you have helped me a lot in getting better at Python

harshulgoel
Автор

Fast and simple to understand!
Thanks for making such wonderful content!! 😃

darshankokal
Автор

At 2:27 it will be
for j in range(i-1, -1, -1):

virl