Learn Insertion Sort in 7 minutes 🧩

Поділитися
Вставка
  • Опубліковано 15 жов 2024
  • Data structures and algorithms insertion sort
    #insertion #sort #algorithm
    // Insertion sort = after comparing elements to the left,
    // shift elements to the right to make room to insert a value
    // Quadratic time O(n^2)
    // small data set = decent
    // large data set = BAD
    // Less steps than Bubble sort
    // Best case is O(n) compared to Selection sort O(n^2)
    music credits 🎼 :
    ===========================================================
    Take It Easy - by Bad Snacks
    link: • bad snacks: UA-cam Au...
    ===========================================================

КОМЕНТАРІ • 235

  • @BroCodez
    @BroCodez  3 роки тому +91

    public class Main{

    // Insertion sort = after comparing elements to the left,
    // shift elements to the right to make room to insert a value

    // Quadratic time O(n^2)
    // small data set = decent
    // large data set = BAD

    // Less steps than Bubble sort
    // Best case is O(n) compared to Selection sort O(n^2)

    public static void main(String[] args) {

    int array[] = {9, 1, 8, 2, 7, 3, 6, 5, 4};

    insertionSort(array);

    for(int i : array) {
    System.out.print(i + " ");
    }
    }
    private static void insertionSort(int[] array) {

    for(int i = 1; i < array.length; i++) {
    int temp = array[i];
    int j = i - 1;

    while(j >= 0 && array[j] > temp) {
    array[j + 1] = array[j];
    j--;
    }
    array[j + 1] = temp;
    }
    }
    }

    • @joyceasante8292
      @joyceasante8292 Рік тому +2

      Practicing...
      public class Main
      {
      public static void main(String[] args) {
      int array[] = {5,1,4,9,3,7,2,8,6};
      insertionSort(array);
      for(int i : array){
      System.out.print(i + " ");
      }
      }
      public static void insertionSort(int[]array){
      for(int i = 1; i < array.length; i++){
      int temp = array[i];
      int j = i - 1;
      while(j >= 0 && array[j] > temp){
      array[j+1] = array[j];
      j--;
      }
      array[j+1] = temp;
      }
      }
      }

    • @motivationwithhb5035
      @motivationwithhb5035 10 місяців тому

      What are the avantages of insertion sort

    • @III333III
      @III333III 6 місяців тому +1

      @@motivationwithhb5035 Time complexity usually: Best case for insertion is O(n) compared to Selection sort O(n^2). ie time it take for a computer to run calculations

  • @krzychhoo
    @krzychhoo 6 місяців тому +83

    This finally made insertion sort click for me (i have a test tomorrow, pray for me brothers)

    • @taminofink677
      @taminofink677 6 місяців тому +2

      How did it went?

    • @krzychhoo
      @krzychhoo 6 місяців тому

      @@taminofink677 i got a max grade.

    • @kazianup4480
      @kazianup4480 5 місяців тому

      I have my AP exam in 2 weeks

    • @wafflesaucey
      @wafflesaucey 5 місяців тому

      @@kazianup4480 sending luck, have mine wednesday.

    • @moshimoshi_04
      @moshimoshi_04 2 місяці тому

      Same bro

  • @sameerizaj5458
    @sameerizaj5458 3 місяці тому +7

    And it took me 2 hours to understand basic insertion sort not even a single person actually talk about temp everyone was just saying that place in the correct order, and this man taught me in 1 minute

  • @bh3302
    @bh3302 Рік тому +20

    Man I can not explain how much I love and appreciate the work you do on this channel.

  • @Amy-mo9ki
    @Amy-mo9ki Рік тому +18

    I think this is a good description of insertion sort:
    The full insertion sort algorithm works by dividing an array into two pieces, a sorted region on the left and an unsorted region on the right. Then, by repeatedly inserting elements from the unsorted half into the sorted half, the algorithm eventually produces a fully sorted array. The full steps of this process for an array, A, are shown below
    - Designate the leftmost element of *A* as the only element of the sorted side. This side is guaranteed to be sorted by default, since it now contains only one element.
    - Insert the first element of the unsorted side into the correct place in the sorted side, increasing the number of sorted elements by one.
    - Repeat step two until there are no unsorted elements left.
    Notice that this method doesn’t require us to create a new array to store the sorted values. All we have to do is keep track of how much of the original array is sorted. This makes insertion sort an in-place algorithm.

  • @matyasneilinger906
    @matyasneilinger906 3 роки тому +32

    OMG, yes! My favorite programming channel transformed into an even better one! Love your content, it really really helped me a lot in my studies and with my projects as well! Keep up the good work, you are awesome! Quality content at it's finest! ;)

    • @BroCodez
      @BroCodez  3 роки тому +12

      Thank you! Hopefully this channel will continue to evolve in the future!

  • @mmet0diev
    @mmet0diev 3 роки тому +7

    look at how much your channel grew, remembering when I came you were under 1k subs.
    Tho you definetlly deserve and earned them bro, you are literally the best programming tutoring channel I know of! Thank you for this amazing content bro!

    • @BroCodez
      @BroCodez  3 роки тому +4

      Thank you for the kind words Max! I'm glad you've been here since the era of 1k!

  • @polinakornilova8093
    @polinakornilova8093 Рік тому +4

    I never write comments, but thank you for all your work! This is the best explanation for a visual learner like me. It really helped to have your visualisation alongside code to see what is going on step by step.

  • @hassankushkush2701
    @hassankushkush2701 Рік тому +1

    bro is single handedly the reason I am clutching this class on my own, god bless your soul😅🙏🏾

  • @aditya_asundi
    @aditya_asundi 3 роки тому +22

    Congrats on the 100k !!!! I remember subscribing to you at 15k.

  • @Jeetsukii
    @Jeetsukii 11 місяців тому +1

    great tutorial! is there a reason we do j-- inside while loop and then use the [j+1] index instead of just removing the j-- and using the j index?

  • @VAIBHAVMALHOTRA19
    @VAIBHAVMALHOTRA19 20 днів тому

    I couldn't find explanation of this algorithm better than this one. Thanks bro now I can confidently answer what is insertion sort during interviews.

  • @LuoMan-z8c
    @LuoMan-z8c 8 місяців тому

    I love you bro!!!!!! so clear explanation !!!! I failed to figure out insertion sort on my teacher's class even though I spent more than 2 hours, but I figure it out only take 7 minutes by watching your video !! amazing !!

  • @ethan6708
    @ethan6708 9 місяців тому

    Thank you for this! I think where I'm struggling is understanding why we need to place a value in "temp" before we do a comparison. But your graphics and explanation are TOP NOTCH. A real service to the CS community worldwide. Thanks again!

  • @rebootlinux608
    @rebootlinux608 3 роки тому +3

    Omg bro you are awesome. You're a natural teacher thank you for the awesome content you really help me with my programming subject. God I wish I had teachers like you.

    • @BroCodez
      @BroCodez  3 роки тому +1

      Thank you for the kind words Linux!

    • @rebootlinux608
      @rebootlinux608 3 роки тому

      @@BroCodez no problem!

  • @sherriexoxo
    @sherriexoxo 3 місяці тому

    Thanks for the explanation I have been taking classes at uni about this topic but my teacher hasn't been able to explained right. Thanks for the content. It was so helpful.

  • @Saucevideos
    @Saucevideos 4 місяці тому

    Excelente explicación en el paso a paso, felicidades. Gracias por aportar a la nueva generación de desarrolladores.

  • @FabricioRWitt
    @FabricioRWitt 11 місяців тому

    This is the best explanation I found in UA-cam. Thanks!

  • @gurjotsinghpandher3908
    @gurjotsinghpandher3908 Місяць тому

    Great explanation!!! You explain by showing what exactly happens -- that's why it makes so much sense :)

  • @rdxdevil5083
    @rdxdevil5083 2 місяці тому +1

    you are my god of programming thank you bro love from india, you are genius

  • @kettenbach
    @kettenbach Рік тому

    These are truly some of the best visual explanations of the sorting algorithms I literally ever seen. Well done man. Very cool. 👍

  • @piyushbarve2618
    @piyushbarve2618 2 роки тому +3

    Hello can you please make videos on this topics.
    1. Dynamic Programming
    2. Backtracking

  • @nikhiltiwari20
    @nikhiltiwari20 8 місяців тому

    Hey bro 1 request please continue your series on dsa,your explaination is so good that even toughest question can be understood in 1 go.Please its a humble request

  • @motivationwithhb5035
    @motivationwithhb5035 10 місяців тому +1

    What are the avantages of insertion sort

  • @raghavm9314
    @raghavm9314 3 роки тому +1

    Please never stop upload waiting for your complete course on data structures and algorithms

    • @BroCodez
      @BroCodez  3 роки тому +1

      Thanks! I don't plan on stopping anytime soon 👍

  • @sametsahin-eh3qj
    @sametsahin-eh3qj 9 місяців тому +1

    doing the gods work fr fr

  • @tangaragsh
    @tangaragsh 15 днів тому

    i wish i know your channel earlier....thank you for the very clear concepts explanations!

  • @rewrose2838
    @rewrose2838 3 роки тому +1

    Hello bro, just wanted to say congratulations on reaching 100,000 subscribers 😁
    (I am glad I stumbled across this channel when I did, your tutorial playlists are the best on youtube)

    • @BroCodez
      @BroCodez  3 роки тому +2

      Thank you Rew Rose! I remember you from early days of this channel lol
      Thank you for sticking around since then!

    • @rewrose2838
      @rewrose2838 3 роки тому

      @@BroCodez 😂 I've been working through my college courses, and only now did I start learning Java Spring.
      (btw, your DS and algorithm videos have been very helpful so thank you and please continue the good work 😊)

  • @DamiansCraft
    @DamiansCraft 3 роки тому +1

    Thank you bro! It is a pleasure to see your tutorials! You are my source of inspiration and learning! Keep up!

    • @BroCodez
      @BroCodez  3 роки тому

      Thanks for the support skin!

  • @vuchinh9989
    @vuchinh9989 3 роки тому

    Hey man, Im from Vietnam
    Just came accross your channel and really like it!
    Thank you very much👍💪

  • @tasneemayham974
    @tasneemayham974 Рік тому

    "How about a 9 and a 1 and an 8" really got me smiling and singing it for the rest of the video!!
    This man is a legend!!!!

  • @manikumari4228
    @manikumari4228 3 місяці тому +1

    You are best my dear sir 🙌

  • @ArunKumar-vd8zt
    @ArunKumar-vd8zt 6 місяців тому

    🔥this guy video >>>> my 49$ DSA course

  • @ParaGames-o5h
    @ParaGames-o5h 5 місяців тому

    Thank you so much for making this video and also the other tutorials for algorithms! Great help!

  • @murtazatinwala4830
    @murtazatinwala4830 3 роки тому +1

    Sir your all full courses are awesome I have a request if you could please make a course on Android app development

    • @BroCodez
      @BroCodez  3 роки тому

      I hope to someday! However I'm not sure when exactly

  • @raufsaleem990
    @raufsaleem990 3 роки тому +2

    Hey man, just wanted to say, keep up the good work, ur videos have been helping me a lot this sem for data structures, thank you 😎

    • @BroCodez
      @BroCodez  3 роки тому +1

      Thanks for watching Rauf! It's motivating to me when they're helping people!

  • @bhishek369
    @bhishek369 11 місяців тому

    Brocode rockzz❤thanks to youtube's algorithm for suggesting this channel.

  • @auxy6858
    @auxy6858 4 місяці тому

    Passing my GCSES with this one 🔥

  • @sametsahin-eh3qj
    @sametsahin-eh3qj 9 місяців тому

    The way I subscribed immediately is crazy

  • @MarioArbaselu
    @MarioArbaselu 5 місяців тому

    regarding the condition j >= 0 in while, when j becomes

  • @Story_Arc782
    @Story_Arc782 Рік тому

    Your explanations are the best , glad i found your channel 🎉

  • @DetCoAnimeFan
    @DetCoAnimeFan 3 роки тому +1

    I knew this but not properly I guess. This video was very helpful. Btw I had to ask what is the RAM of your pc?

    • @BroCodez
      @BroCodez  3 роки тому +1

      I'm mainly use a laptop. It has 12GB of RAM

  • @snipo39
    @snipo39 2 роки тому

    This is the best video so far on the Insertion sort :)

  • @sanskarsongara2592
    @sanskarsongara2592 3 роки тому +1

    Yo bro, just wanna ask are you gonna drop C language videos anytime soon, wanna refresh my concepts of C that's all

    • @BroCodez
      @BroCodez  3 роки тому

      I'm not sure when exactly, but I do plan on releasing C videos sometime in the future

    • @mollyputih4750
      @mollyputih4750 3 роки тому

      @@BroCodez keep it up bro 👍👍

  • @bruce9067
    @bruce9067 3 роки тому

    CONGRATS ON REACHING 100K SUBS!!!! YOU ARE THE BEST, BRO!!!

    • @BroCodez
      @BroCodez  3 роки тому +1

      Thank you Brucc! I owe you guys for getting me here!

  • @artsynewb2391
    @artsynewb2391 3 роки тому

    it's generally so overwhelming to do dsa but istg you made it so easy and the concept crystal clear😌👍

    • @BroCodez
      @BroCodez  3 роки тому +1

      DS & A is intimidating.
      Thank you for the kind words artsyjaa!

  • @jiwonseo
    @jiwonseo 3 роки тому +1

    This was what I was waiting for.

  • @ethanminja4706
    @ethanminja4706 3 роки тому +1

    SOOOOOOOO Close to 100k
    UA-cam button on your way!

  • @jkking3213
    @jkking3213 3 роки тому +1

    congratulations for being 100k youtuber

    • @BroCodez
      @BroCodez  3 роки тому

      Thank you JK King! I owe you guys for all the support!

  • @Juliana-cx7qq
    @Juliana-cx7qq Рік тому

    I love you this is amazing and so quick and simple

  • @gouthamtadali5072
    @gouthamtadali5072 3 роки тому

    Cool video..please do videos on remaining sorting algorithms too.. like O(nlogn) in worst case...

  • @user-qm4sc2jv7f
    @user-qm4sc2jv7f Рік тому

    what did the last line array[j+1]=temp do?

  • @ANONYMOUS-hl3ih
    @ANONYMOUS-hl3ih 3 роки тому

    Your Content and code is AWESOME brother
    Keep it up

    • @BroCodez
      @BroCodez  3 роки тому +2

      Thank you anonymous!

  • @toanta4838
    @toanta4838 2 роки тому

    It's difficult but I have to try ,thank you!!!

  • @amirhamzah6146
    @amirhamzah6146 3 роки тому

    congrats for reaching 100k!!

    • @BroCodez
      @BroCodez  3 роки тому

      Thank you Muhammad! I owe you guys for getting me here!

  • @enigmatimson4565
    @enigmatimson4565 3 роки тому

    Man you're a legend, no joke !

  • @supernovic99
    @supernovic99 Рік тому

    This was really useful. Thanks!

  • @MayuriNimonkar
    @MayuriNimonkar 3 роки тому

    This was such nice explanation! Thank you!

  • @envektro2519
    @envektro2519 3 роки тому

    I hope that you will continue making this for all sorting algorithms there is.

  • @yon1623
    @yon1623 Рік тому

    leaving random comment causs you explained it better than my teacher

  • @ramfattah211
    @ramfattah211 Рік тому

    Thanks man. This was awesome explanation.

  • @berna8803
    @berna8803 Рік тому

    Thanks for the video man, really great explanation

  • @anushkakondkar5401
    @anushkakondkar5401 Місяць тому

    thanks! this really helped

  • @britznick
    @britznick 3 роки тому

    Newbie here confused about
    J - - dosn’t it set j to -1 after first loop?
    Thanks for vid btw

    • @BroCodez
      @BroCodez  3 роки тому +1

      j decreases by 1 during each iteration of the outer for-loop, then it stops at 0

  • @duaahammadd
    @duaahammadd Рік тому

    Thanks a million. This video is a life saver!♥

  • @poonamjaiswal7600
    @poonamjaiswal7600 6 місяців тому

    At 3:27 how did sysout become system.out.println
    Pl tell

  • @noorachman
    @noorachman 3 роки тому

    Yooo ur so close to 100k :000

  • @TonyMalik7
    @TonyMalik7 3 роки тому +1

    thank you much sir it is help full

    • @BroCodez
      @BroCodez  3 роки тому +2

      You're welcome Tony! Thanks for watching!

  • @hannibalbianchi1466
    @hannibalbianchi1466 3 роки тому

    Thank you sir it's a wonderful channel may I ask you if you can make Django course

    • @BroCodez
      @BroCodez  3 роки тому

      I hope to someday!

    • @aditya_asundi
      @aditya_asundi 3 роки тому

      @@BroCodez plz do I can't understand other Django courses.

  • @rishirumuhirwaarthur5195
    @rishirumuhirwaarthur5195 2 місяці тому

    this dude is really saying hey guys it's Bro and at the end of the video I called him a real bro

  • @aditya_asundi
    @aditya_asundi 3 роки тому +1

    Yo bro what language do you specialise in?

    • @BroCodez
      @BroCodez  3 роки тому

      I'm most experienced with Java since that was what was taught when I was in college, but I prefer C/C++

  • @niksddd
    @niksddd 3 роки тому

    Hello will you tell me from where we can practice for our code mean how to build our logic ?? 🙏🙏Plz tell

    • @BroCodez
      @BroCodez  3 роки тому

      Tutorials Point has an online compiler you can use to practice, here's the link:
      www.tutorialspoint.com/compile_java_online.php

    • @niksddd
      @niksddd 3 роки тому

      @@BroCodez no means practice questions or problems where I can find.

    • @niksddd
      @niksddd 3 роки тому

      No means where I can find the practice questions or problems ??
      I can't find the practice questions

    • @aditya_asundi
      @aditya_asundi 3 роки тому

      @@niksddd codewars.com or brilliant.org

  • @nozzi325
    @nozzi325 3 роки тому

    Thanks, man
    You're awesome!

  • @elionayzuridasilveira4140
    @elionayzuridasilveira4140 8 днів тому

    Thank you for this video Bro

  • @danielbarros5507
    @danielbarros5507 11 місяців тому

    best explanation easy.

  • @victorrezende6002
    @victorrezende6002 Рік тому

    Nice Class

  • @adeelahmaddev.9346
    @adeelahmaddev.9346 3 роки тому

    Congratulations on 100k🙌🙌

    • @BroCodez
      @BroCodez  3 роки тому

      Thank you Sheikh! I couldn't have done it without your support!

  • @muradsaleh4826
    @muradsaleh4826 5 місяців тому

    I didnt understand it, watched the first minute and then coded it in 2 minutes in c++ to see if i actually got it. thanks man!
    void arr2(int arr[]){
    for (int i = 1; i< 14; i++){
    int temp = arr[i];
    for (int j = i-1; j >= 0; j--){
    if (arr[j] > temp){
    arr[j+1] = arr[j];
    arr[j] = temp;
    }
    }
    }
    }

  • @andrewchen861
    @andrewchen861 Рік тому

    Bro always carries me in coding

  • @Tebibyte
    @Tebibyte Рік тому

    you are the flipping best

  • @zazatjijombo174
    @zazatjijombo174 21 день тому

    This is good!

  • @syllight9053
    @syllight9053 3 роки тому

    I commented to boost the algorithm!

    • @BroCodez
      @BroCodez  3 роки тому +1

      Thank you Syllight!

    • @syllight9053
      @syllight9053 3 роки тому

      @@BroCodez OMG You commented!
      I Just wanted to say thanks you for making these amazing courses for free!
      I hope the YT algorithm will help you reach 1 million soon!

    • @BroCodez
      @BroCodez  3 роки тому

      @@syllight9053 We'll get there sooner than later! Thanks for being awesome Syllight!

  • @babychic19
    @babychic19 3 роки тому

    Can you make Multiplication table using arraylist 2D array?

  • @jesutile8828
    @jesutile8828 Рік тому

    You are the best

  • @paullein4043
    @paullein4043 Рік тому

    Great video

  • @mrmysterious1724
    @mrmysterious1724 3 роки тому +1

    would this also be a valid solution:
    private static void insertionSort(int[] array) {
    for(int i=1; i < array.length ; i++){
    int temp = i;
    for(int j=0; j < i; j++){
    if(temp

  • @Bean-kw2xp
    @Bean-kw2xp Рік тому

    soo, bubble sort in reverse?

  • @MrLoser-ks2xn
    @MrLoser-ks2xn Рік тому

    Thanks!

  • @angelosyt5820
    @angelosyt5820 3 роки тому +1

    I know you're a better programmer than me and that it doesnt matter but why:
    int array[]
    Instead of
    int[] array

    • @BroCodez
      @BroCodez  3 роки тому

      whoops! You can write it either way. I should have made it consistent and wrote int[] array

    • @angelosyt5820
      @angelosyt5820 3 роки тому +1

      @@BroCodez it is the same its just more confusing since if you have like a 10k line program you can get confused thinking its an int, anyway though great tutorial
      EDIT: Just saw the other comments and wow you're like the only youtuber that replies to comments

    • @BroCodez
      @BroCodez  3 роки тому +1

      @@angelosyt5820 Thanks! I try my best to reply, but there's a lot of you guys 😅

  • @life_with_rauf
    @life_with_rauf Рік тому

    Please cover, Heap sort

  • @BatmanKingston
    @BatmanKingston 16 днів тому

    thank you

  • @taimoormanzoor7386
    @taimoormanzoor7386 3 роки тому

    Bro can you make video on recursion ?

  • @Instinct072
    @Instinct072 Рік тому

    THANK YOU

  • @jeromesimms
    @jeromesimms 2 роки тому

    Thanks 👍🏽

  • @BN-cr3el
    @BN-cr3el 3 роки тому

    Thank you for these epic educational videos. You explain it super clear 💯

    • @BroCodez
      @BroCodez  3 роки тому +1

      You're welcome B N! Thanks for watching!

  • @varnitraina4684
    @varnitraina4684 3 місяці тому

    Learnt in 7 minutes,THALA FOR A REASON

  • @newbie6449
    @newbie6449 10 місяців тому

    Damn this was so easy

  • @ryan2-518
    @ryan2-518 3 роки тому

    How do you rotate jlabels?

    • @BroCodez
      @BroCodez  3 роки тому

      something like this might work
      public void paintComponent(Graphics g) {
      Graphics2D gx = (Graphics2D) g;
      gx.rotate(0.6, getX() + getWidth()/2, getY() + getHeight()/2);
      super.paintComponent(g);
      }

    • @ryan2-518
      @ryan2-518 3 роки тому

      @@BroCodez oh, but what if I need my jlabels to have a mouse listener? Currently I have set an icon to my jlabels and can’t rotate them. If I instead use a graphic, will I even be able to use a jlabel, much less have it be clickable? Thanks for your time

  • @rdxdevil5083
    @rdxdevil5083 5 місяців тому

    but if you will understand code + animation then it will be better for us

  • @edhitimana7733
    @edhitimana7733 Рік тому

    Chef kiss