Java: Insertion Sort sorting algorithm


Video is ready, Click Here to View ×

Animated demo tutorial on Insertion Sort sorting algorithm with example implementation code in Java. Get the code from my github site here:

29 thoughts on “Java: Insertion Sort sorting algorithm

  1. My lecturer just went quickly through slides and then told us to watch a visualisation on a website if we like,that was it.I didn't get anything,and have seen multiple videos on this.This is the only one i understood,thank you so much!

  2. well , can't thank you enough for your videos !
    I was trying to code the Insertion sort Method on my own before seeing your version of the code and now they are different but it does work perfectly , I am not sure what is he difference

    public static void InsertionSort() {

    int temp;

    int j;

    for (int i = 1; i < A.length; i++) {

    j = i;

    while (j > 0 && A[j – 1] > A[j]) {

    temp = A[j];

    A[j] = A[j – 1];

    A[j – 1] = temp;





  3. It's a great video Joe, and it explains things well, but I see this all the time in videos like these. Folks will use single digit numbers as elements, which is okay for people like me and you, but put yourself in the mind of a beginner. They will confuse indexes with elements because they are single digit. May I propose using double digit numbers as elements, so the beginner isn't confused with "Was I suppose to switch index 3 or did he mean the value 3?" Do you see how that could be confusing in terms of a beginner who is mixing up elements with indexes?

  4. Insertion sort does not deal with swapping😕 ,we have to INSERT the values as required
    That's what the name suggests
    The video was good explained👍🏻 but instead of swapping there must be insertion

  5. Hi there, I am dealing with this mathematically, thanks for saying 5 is already sorted.

    This sorting algorithm is mathematically, an arithmetic series, such that , after the first sort, where there are n numbers in this sort , we have n sorts to begin with, and so there are (n-1) sorts after the first sort, after two sorts we have (n-2) sorts etc.,

    The Arithmetic series is then : (n-1) + (n-2)+ (n-3)+(n-4)+(n-5)+(n-6)

  6. Could you please tell me is this insertion sort?
    public static int[] insertionSort(int[] nums)
    for(int i = 1;i<nums.length;i++)
    for(int j = 0;j<i;j++)
    swap(nums, i, j);
    return nums;

Leave a Reply

Your email address will not be published. Required fields are marked *