Learn Insertion Sort in 7 minutes 🧩

preview_player
Показать описание
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
===========================================================
Рекомендации по теме
Комментарии
Автор

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;
}
}
}

BroCodez
Автор

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

krzychhoo
Автор

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

sameerizaj
Автор

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

bh
Автор

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.

Amy-moki
Автор

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! ;)

matyasneilinger
Автор

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!

mmetdiev
Автор

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

aditya_asundi
Автор

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.

polinakornilova
Автор

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

hassankushkush
Автор

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

Saucevideos
Автор

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!

ethan
Автор

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.

rebootlinux
Автор

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 !!

vslqmul
Автор

This is the best explanation I found in YouTube. Thanks!

FabricioRWitt
Автор

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

rdxdevil
Автор

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

kettenbach
Автор

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

xzoceiv
Автор

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

DamiansCraft
Автор

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 😎

raufsaleem