Shell Sort - Data Structures & Algorithms Tutorial Python #18

preview_player
Показать описание
Shell sort is a customization over insertion sort. Insertion sort requires many swaps and comparisons if heavy elements are located towards the end of an array. Shell sort will initially sort subarrays that are equal distance apart. The distance here is referred to as a gap. We will than keep on reducing the gap until it is 1. With gap=1 it becomes insertion sort but this time due to optimization we have performed earlier, sorting the array would require very less comparisons and element swaps.

🔖Hashtags🔖

#Shellsort #datastructures #algorithms #python #Shellsortpython #sortalgorithm #pythonShellsort #Shellsort #datastructure #Shellsortcode #Shellsortprogram

DISCLAIMER: All opinions expressed in this video are of my own and not that of my employers'.
Рекомендации по теме
Комментарии
Автор

Below is my code to the exercise: works nicely without creating a separate array to keep track of the index, deletes duplicates on the fly. Thanks for the video and the short exercises at the end of each video.
def shell_sort(arr):

size = len(arr)
i = 0
gap = size//2

while gap > 0:
i = 0
while(i<size):
# for i in range(0, size, gap):
for x in range(0, i, gap):
if arr[i] < arr[x]:
arr.insert(x, arr.pop(i))
break
elif arr[i] == arr[x]:
arr.pop(i)
size = size-1
i-=1
break
i+=gap
print(arr)
gap-=1
print(arr)

mohammadpatel
Автор

This is by far the best video explaining Shell Sort

RaGa_BABA
Автор

After surfing innumerable sources and not understanding a single shit, I am blessed to find you!

itskathan
Автор

Why should someone pay to udemy, coursera etc etc. When we have good teacher like him? Definitely would recommend this to family and friends

surajvijay
Автор

Your ability to communicate these concepts is totally out of this world! Thank you very much for the work that you're doing.

gitahinganga
Автор

You're an amazing teacher, thank you so much. All of the other videos were not helpful

micahjacobson
Автор

Very detailed explanation sir, thank you love from nigeria 🇳🇬

GeneralShark-
Автор

Thank you so much for your effort :>
I'm studying for graduate school and I'm getting a lot of help from your videos.

헬롱-gs
Автор

It's absolutely fantastic series on data structures in Python. Everything got covered except selection sort and hashing in details. Thank you sir...!

mahesht
Автор

A Big thanks for your It really helped me to understand the dsa concept in python

The code for the exercise. I hope it help others

def shellsort(arr):
size=len(arr)
gap=size//2
while(gap>0):
for i in range(gap, len(arr)):
anchor=arr[i]
j=i
while(j>=gap and anchor<arr[j-gap]):
arr[j]=arr[j-gap]
j=j-gap
arr[j]=anchor
gap=gap//2
return arr

element=[[1, 2, 3, 4, 5, 6, 1, 2, 3, 1, 2, 3, 1, 2, 3], [4564, 13, 6500, 13, 4564], [25, 12, 12, 12, 12, 25, 25], [], [25, 25, 25, 1]]
for x in element:
for i in x:
c=0
for j in x:
if i==j:
c+=1
if c>=2:
x.remove(j)
c-=1
for i in element:
print(shellsort(i))

rajnishpandey
Автор

for anyone struggling to understand the need for j, its essentially used to iterate through our shell/gap that we create each time

anton
Автор

Thank you so much sir . I have completed the exercise you have given for Shell sort but the main thing i learnt in this exercise is difference between "for loop and while loop" i.e Lazy evaluation . I solved this exercise by using while loop on deleting index on same array if any duplicate value occured.

RohitKumar-dzdh
Автор

This is the best video explaining Shell Sort

muhammada
Автор

guys i know it's weird to talk about that here in a programation video but i have been watching this playlist from a long ago and been reading some of your comments, and I am feeling toward you like kind of classroom mates haha, and I am gonna address my paroles to all people looking for the truth behind the universe and God and the purpose of life, my man and my sis !! there is an after-life and we will all face our God ''Allah'' after we die, that's a thing we should be aware of, so at that case we must review our self and our duties toward our God, our selves and our family, etc. you and me remember this point. if you heard about religions, maybe you had some stereo type about Islam that it's a bad religion but bro all other religions are based on people opinions and their desires except the Islam is the only true religion that if you follow you will succeed both in life and after-life.... So, if you are looking for the truth of the universe, I am gonna advise you to read Quran to see the truth all of it. the Quran is the God's paroles and instruction to succeed.... and also go read about the prophet Muhammed the best of all humanity, how he sacrificed all his life for us next generations to know the true meaning of life together with his friends, and how he turned the Saudi island from a small retarded village to a big nation with a great story of success .

younesizeria
Автор

better explained that in my university, it was looking so complicated but its actually easy

shakaliha
Автор

In for loop it will be (gap, size, gap) .else it will start with gap doing comparisons till end la. So it will same as insertion sort.

varunvarun
Автор

Realy appreciate your idea of lecturs of DSA in python

mohammadmohsinmohammedmohs
Автор

For exercise, can't I use set on the list to remove duplicates??
edit: I can't as it can't index addressed, though I used set(x) then list (X) in some places and it worked

Sayonora
Автор

def shell_sort(a):
size=len(a)
gap=size//2
while gap>0:
for x in range(gap, size):
j=x-gap
if a[j]>a[x]:
temp=a[j]
a[j]=a[x]
a[x]=temp
gap-=1
# it's work
this is right? or anything problem here....

anuragchourasiya
Автор

When the gap was reduced to 1,
Only 4 and 9 got sorted ... but 38 and 32 remained unsorted only ...

So in this example, the last step involves 2 swaps ?

Anushka-orih