Insertion Sort - Data Structures & Algorithms Tutorial Python #16

Поделиться
HTML-код
  • Опубликовано: 14 дек 2024

Комментарии • 83

  • @codebasics
    @codebasics  2 года назад +2

    Do you want to learn python from me with a lot of interactive quizzes, and exercises? Here is my project-based python learning course: codebasics.io/courses/python-for-beginner-and-intermediate-learners

  • @meatball5544
    @meatball5544 3 года назад +7

    The world changed, ppl studied and revolutionalized the world's technology; every day, a new algorithm is released and ppl try to improve themselves to become better person. Every second a person dies and borns, and yet, here I am, as always, trying to learn everything just before the night of exams... some things never changed... and your videos have helped me a lot. Thank you for the hard work!

  • @SatyamSingh-zm8es
    @SatyamSingh-zm8es 4 года назад +4

    Thank You Sir. I just completed the whole Data Structure and Algorithms Playlist. Your videos are very informative and well explained.

  • @ridhamsuhagiya4886
    @ridhamsuhagiya4886 3 года назад +8

    well I tried it by myself first ,wrot a 30-35 lines of code still was'nt able to pull this off , and you did it in within 5-6 lines truly amazing love your videos.

  • @karthikeyani07
    @karthikeyani07 4 года назад +4

    why am I paying to attend university , when I get classes like this !! This series is really helpful!!

    • @codebasics
      @codebasics  4 года назад

      👍😃

    • @jonathanmeza5807
      @jonathanmeza5807 3 года назад

      i'm watching his data structures and algorithms playlist before taking a data structures and algorithms course.... failed it the first time but i think that i will pass this time, his videos are super helpful

    • @karthikeyani07
      @karthikeyani07 3 года назад

      @@jonathanmeza5807 All the best !

    • @kanikakansalpathshala1624
      @kanikakansalpathshala1624 2 года назад

      ruclips.net/video/lzS_DiMlR2Q/видео.html

    • @kanikakansalpathshala1624
      @kanikakansalpathshala1624 2 года назад

      ruclips.net/video/lzS_DiMlR2Q/видео.html

  • @josiahdavid7735
    @josiahdavid7735 2 года назад

    My best part was the very last commentary, I'm cracking up. Greate Video!!

  • @ChetanAnnam
    @ChetanAnnam 3 года назад +2

    4:45 sampoornesh babu rocks

  • @npvlong
    @npvlong 4 года назад +1

    Just finished the whole series. Very informative. Thank you very much.

  • @mohammadpatel2315
    @mohammadpatel2315 3 года назад +1

    my solution to exercise
    def insertion_sort(element):
    total = 0
    for i in range(len(element)):
    for x in range(i):
    if element[i] < element[x]:
    element.insert(x,element.pop(i))
    break
    if i%2 ==0:
    index = i/2
    print(element[int(index)])
    else:
    index = i//2
    print((element[int(index)] + element[int(index)+1]) /2)

  • @rishavbhattacharjee7182
    @rishavbhattacharjee7182 4 года назад +2

    Very helpful Sir. Looks like you are continuing both the Dara Structures and Deep Learning series. Thank you sir😁

    • @codebasics
      @codebasics  4 года назад +2

      yup those are my two active series right now

  • @jiyabyju
    @jiyabyju Год назад

    In every video,i will laugh in middle..that is the beauty of your videos...loved it..

  • @nimishakhadilkar1491
    @nimishakhadilkar1491 2 года назад +1

    Thank you Sir. 🙏🙏
    Very helpful and detailed explaination and also easy to understand. 🙏🙏

  • @nvasudeva
    @nvasudeva 4 года назад +1

    If this would have been pretty big unsorted list then which algorithm you would have suggested?

  • @binary_110
    @binary_110 4 месяца назад

    your powerpoint skills seems so good, can you make a powerpoint tutorial on how you make these slides in powerpoint it would be really helpful

  • @valentinemadu8595
    @valentinemadu8595 Год назад +1

    beautiful piece......Just found it difficult understanding the question in the exercise

  • @muhammedjafer5961
    @muhammedjafer5961 2 года назад

    exercise
    def insertion_sort(arr):
    for i in range(1, len(arr)):
    count = i
    while count > 0 and arr[count - 1] >= arr[i]:
    count -= 1
    arr.insert(count, arr.pop(i))
    a = len(arr)
    return arr[int(a / 2)] if a % 2 == 1 else (arr[int(a / 2)] + arr[int((a / 2) - 1)]) / 2
    # for median
    if __name__ == '__main__':
    elements = [9, 5, 3, 2, 1, 10, 11, 12]
    print('median: ' ,insertion_sort(elements))
    print(elements)

  • @swatiahlawat5292
    @swatiahlawat5292 4 года назад +2

    Hi Dhaval, We can write sort function in this way as well, it passed the edge cases, let me know if you found any bug in this: def insertion_sort(lst):
    for i in range(len(lst)):
    for j in reversed(range(i)):
    if lst[j+1] < lst[j]:
    lst[j+1], lst[j] = lst[j], lst[j+1]
    return lst

    • @sidharthp2128
      @sidharthp2128 3 года назад

      is this bubble sort?

    • @justsomerandomguy6719
      @justsomerandomguy6719 2 года назад

      Hey, I know this was posted a while ago but can you confirm if this is the right way to do insertion sort?

    • @marshai8611
      @marshai8611 2 года назад +1

      @@justsomerandomguy6719 running nested loops is not optimal his method is correct however it's not optimized.

    • @justsomerandomguy6719
      @justsomerandomguy6719 2 года назад +1

      @@marshai8611 okay. Thank you for your insight.

  • @vinaynaik953
    @vinaynaik953 4 года назад

    It's most beautiful video on DSA actually your explanation makes us motivated learn best practice of Software Industry

  • @somasreebiswas8617
    @somasreebiswas8617 4 года назад +3

    Hi Sir, your videos are explanatory and quite informative. I request you to kindly share other sorts such as shell sort, comb sort, counting sorts etc as well.

  • @a.human.
    @a.human. 2 года назад +1

    Self-note: tricky concept, come back and review again.

  • @vivekpujaravp
    @vivekpujaravp Год назад

    You are a phenomenal teacher, thank you so much. And hilarious lol

  • @FarhanKhan-io3vt
    @FarhanKhan-io3vt 3 года назад

    we can also use two pointer approach that will be more optimizing and better approachable method

  • @awsomeslayer1
    @awsomeslayer1 3 года назад +4

    I can't tell you what pain I went through to solve the exercise on my own. Took me 3 hours of trial and error.

  • @MuhammadZain-dt3px
    @MuhammadZain-dt3px 7 месяцев назад

    I am confused why worst case performance is n^2 ?

  • @lisamathur9206
    @lisamathur9206 7 месяцев назад

    another approach :
    def insertion_sort(arr):
    new_arr = [arr[0]]
    for i in arr[1:]:
    if new_arr[0] > i:
    new_arr.insert(0,i)
    continue
    elif new_arr[len(new_arr)-1] < i:
    new_arr.append(i)
    continue
    else:
    for idx , k in enumerate(new_arr):
    if k > i:
    new_arr.insert(idx,i)
    break
    return new_arr

  • @travelindiatreasures
    @travelindiatreasures Год назад

    I am 60 years old in Canada. I started learning coding 2 months ago. Does this work?
    import statistics
    my_list = []
    elements = [2,1,5,7,2,0,5]
    for i in range(len(elements)):
    my_list.append(elements[i])
    print(statistics.median(my_list))
    Thank you so much.

  • @sippared9126
    @sippared9126 3 года назад

    Is below code is sufficient

  • @RAMAYATRI
    @RAMAYATRI 3 года назад +1

    How is it different than Bubble Sort ?

    • @adetolaadedeji7273
      @adetolaadedeji7273 9 месяцев назад

      Bubble sort checks from the first element if the first number is greater than the second then swaps the first number with the second and continues like that

  • @anirbanc88
    @anirbanc88 2 года назад

    i love your exercises!!!!!!!!!!! sir thank you

  • @meralmaradia4774
    @meralmaradia4774 2 года назад

    Hello Sir, can you please create a video developing of project using only DSA ?

  • @mohammadpatel2315
    @mohammadpatel2315 3 года назад

    my code for insertion sort:
    def insertion_sort(element):
    for i in range(len(element)):
    for x in range(i):
    if element[i] < element[x]:
    element.insert(x,element.pop(i))
    break

    • @puneethv4138
      @puneethv4138 3 года назад

      This gives u time Complexity of O(n^2) ,

  • @randompersononinternet4952
    @randompersononinternet4952 3 года назад +1

    4:42 jai samppornesh babu 😂😂

  • @alizain9638
    @alizain9638 2 года назад

    For the exercise, I'm not that sure if i'm correct or not. Shouldn't the quotient of len(array) % 2 == 0 instead of 1. Can someone please confirm which is correct?

  • @amarnathprasad1
    @amarnathprasad1 4 года назад +3

    Sir, thank you for such an amazing series so far. In this video's exercise, I don't think the problem statement is consistent with the output expected or the solution given. Can you please check it and let me know if I'm wrong?

  • @goikarBaab
    @goikarBaab 2 года назад +1

    very good explanation. can you please provide the PPT

  • @salikmalik7631
    @salikmalik7631 4 года назад

    I confused, We have builtin sort function in python so why we need that too much code.

    • @jaheerkalanthar816
      @jaheerkalanthar816 3 года назад

      My dear friend,This class only for understanding how the sorting function works,
      In future we cannot implement this method because it is O(n**2)

  • @debojitmandal8670
    @debojitmandal8670 3 года назад +1

    But why j = j-1

  • @alwysrite
    @alwysrite 4 года назад

    ok so we give the exercise a try and then how do we check your code solution after? is there a link?

    • @codebasics
      @codebasics  4 года назад +1

      Check video description. When you .md file with exercise description at the end there is blue solution link. You just click on that

  • @rajatpal2613
    @rajatpal2613 3 года назад

    Can any one pls explain the solution of the problem which was given i was not able to solve it.
    and cant understand the solution code provided by daval sir

    • @jaheerkalanthar816
      @jaheerkalanthar816 3 года назад

      import statistics
      unSortedArray=[2,1,5,7,2,0,5]
      index=0
      for i in range(len(unSortedArray)):
      current=unSortedArray[i]
      j=i-1
      while j>=0 and unSortedArray[j]>current:
      unSortedArray[j+1]=unSortedArray[j]
      j=j-1
      index+=1
      print(statistics.median(unSortedArray[:index]))
      unSortedArray[j+1]=current
      print(unSortedArray)
      For beginner

  • @muhammedcanozdemir6447
    @muhammedcanozdemir6447 3 месяца назад

    we double the array size without using an extra array, what a spectacular idea right

  • @rogerthat7190
    @rogerthat7190 Год назад

    Thank you sir

  • @SyaerulRochman
    @SyaerulRochman Год назад

    thank you so much

  • @ritumahato6289
    @ritumahato6289 4 года назад

    Sir please make more videos on ds algo topic

  • @datasciencetoday7127
    @datasciencetoday7127 5 месяцев назад

    anyone else learning these in the era of CHATGPT ?

  • @anythingit2399
    @anythingit2399 3 года назад

    Sir your exercise solutions should contain comments else its difficult to interpret your thought process

  • @mohammadmohsinmohammedmohs9717
    @mohammadmohsinmohammedmohs9717 4 года назад

    sir please continue uploadin

    • @codebasics
      @codebasics  4 года назад

      Yes next one on merge sort coming soon

  • @hihojin8678
    @hihojin8678 3 года назад +1

    Why do we reduce j by 1 in the end? 🥺

    • @said26navyajnair83
      @said26navyajnair83 3 года назад

      As the loop is going backwards for checking the elements we are reducing j by 1

  • @lokeshreddy5718
    @lokeshreddy5718 4 года назад

    Is this the last topic Or still topics are their to upload please reply?

    • @codebasics
      @codebasics  4 года назад

      I will be uploading mor videos

  • @dailywebmoments
    @dailywebmoments 4 года назад

    7:48 lmao😂😂😂😂😂

  • @sippared9126
    @sippared9126 3 года назад +1

    #implementing the insertion sort in python
    a=list(map(int,input().split()))
    m=[]
    for i in a:
    c=0
    for j in range(len(m)):
    if m[j]>i:
    m.insert(j,i)
    print(m)
    c+=1
    break
    if c==0:
    m.append(i)
    print(m)
    print(m)

  • @yourcodingerror3816
    @yourcodingerror3816 4 года назад +3

    Sir, Hindi m video bnao

  • @dendi1076
    @dendi1076 2 года назад

    that moving gif is creepy and annoying,

  • @kunaltripathi4649
    @kunaltripathi4649 2 года назад

    you really need to stop seeing memes!!!!!!!!

  • @justforprofitstudyonly2307
    @justforprofitstudyonly2307 2 года назад

    i watched it at 1.75 x and it still feels slow , come on man, just get to the f* point.

  • @trishul_guy
    @trishul_guy 7 месяцев назад

    thank you sir