Perhaps it may modify the actual algorithm's second half (but not how it works), I would recommend a brief explanation of the binary search and how that works. Then using the already sorted elements to find where to insert the number in question. Although in it's simplicity, there's nothing wrong with the explanation.
@sk8rboi7566 This is a slow sort. Think of all the comparisons you have to do to get each element sorted. Sure its fast for an array of small size, but imagine an array with 9001 elements. once you get up there the number of swaps you have to do to sort. Yes, to answer your question; it is teaching you sorting methods, but wikipedia it. This is a slow one. This can be made to be fast with a hash table or memorization though.
for all those confused abt the loop being till n-1 rather than till n, here's the explanation..
In the prototype, it mentions for i = 1 to n-1 now n should be interpreted as size of the array and n-1 the last index.. So the loop becomes for(int i = 1; i <= n-1; i++) Thus the loop will continue till the last index of the array
wrong algorithm see for i=1 to n not n-1 check this according to ur algo if i sort 24 13 9 2 then first is 13 24 9 2 for j=1 and for j=2 it will 13 9 24 2, + 9 13 24 2, now what about j=3 my elements in array are 4 and i can loop upto 3 times so algo fails so right programe is from i=1 to n then its gonna work and ignore my poor english
I think, a more efficient variant is that we swap only once (when we reach the place of insertion) but while searching for it, we just shift the elements to the right (swap is more expensive the shift).
The "j=j-1" is so the loop doesn't go on infinitely. If the condition is met, then you swap elements j and j-1, then go on with the next lower index, which is j-1 (so in the next loop iteration, you'll be comparing elements j-1 and j-2, and so on).
Array indices sometimes range from 0 to n-1 (for size n arrays; not from 1 to n), and this is the convention here. So the loop only goes till n-1. It does not start with i=0 but from i=1, because in the first loop there would be nothing to do (the first element is always in the right relative order with itself)
n will work if the array size is larger than the number of items in the array you are sorting. but if the array size is the same number as the number of items, n will cause you to go out of bounds.
It can't be n, because if it is n and not n-1, then j=i=n which means A[j]=A[n] which is not possible, because A[j] is equal from A[0] to A[n-1]. A[n] is not a value itself. A[n] is n times A, from A[0] to A[n-1]. And all A[0] to A[n-1] are values. I hope I was clear
but if you go to the very last one - n - what will you compare it to? there is no n+1 to do a comparison
that's why the last element to be processed is n-1, it will be compared to the next and final one - n - n is only processed and swapped if it is out of place
@fibrosport is it not because it's done on the example of an array and array has n number of slots but the first slot is always numbered 0 so the last slot would be number of slots minus one. if u have 3 slots they are numbered 0 1 2 so last slot is slot number 2. if the for loop would stop at slot n you'd have a runtime error because you'd fall off the edge of the array (move to the slot that doesnt exist)... I think so ?
@myjunno It is because the array is indexed from 0 up to n -1 where n is the number of elements. Treat the first element as index 0 and so on up until the final element which is n-1.
@myjunno the first element is already sorted, so you don't have to sort it, and can skip to the next element, because you skipped the first one, its n-1
I assume that n in this case is the length of the array, and the length of the array is 1 more than the index of the last item in the array. which means the last item in the array would be array[n-1]. If we were to do array[n] then we would get an OutOfBound Error
@myjunno Because in languages like C, arrays are indexed from 0 up, not 1 up. So the last element is indexed n-1, not n. If you used n, you would be accessing elements not in the array.
@fahadkhan2 Pseudocode is intended to be read by humans, and is not in any particular programming language. Hence the pseudocode is indexed from 1 up, since that is how humans count.
@myjunno Because the index of array is from 0 to n-1, so there are n elements in the array. Remember the last index is n-1, not n, or the system will occur IndexoOutOfArray exception.
I like the music :). Very relaxing and helps learning.
Listn2CKY 5 days ago
The animation helps so much!! It clears the idea so well , i bet in my exam i would be seeing a brown background with white spots dancing :P
DivjotBogas 2 weeks ago in playlist Algorithms
Perhaps it may modify the actual algorithm's second half (but not how it works), I would recommend a brief explanation of the binary search and how that works. Then using the already sorted elements to find where to insert the number in question. Although in it's simplicity, there's nothing wrong with the explanation.
rtcvb32 1 month ago
it helps me a lot! thanks man!
LibelCase 1 month ago
good !
TheRAVIKELAKAM 2 months ago
this concl-
316lga 2 months ago
blew my mind!
rampage241 2 months ago in playlist Algorithms
awesome!!
rhlkiller420 2 months ago
Thanks a lot!!
bhanwar04 2 months ago
I wanna make love to this. great music!
ShivMan007 3 months ago
I like the examples and it clearly illustrates the concept.
thaibui01 4 months ago
Im starting Computing soon..this was useful and was straightforward.
Bunfire123 5 months ago
can you post the exact code ?
musikajhen14 6 months ago
music i putting me to sleep
Cory123125 7 months ago
Great supplement to the C++ tutorials!
DigiTan000 7 months ago
SO CLEAR NOW. Wish I'd seen this before I started my assignment haha.
axeld93 9 months ago
This has been flagged as spam show
Get a good friendship with good girls naneedj.info
you are the only solution for bbw naneedj.info
malisha70 9 months ago
good example.
ArunPotti 10 months ago
thank u?
janidhyan 10 months ago
heres hoping it will help me with my test next week ha.
Alexplus20 10 months ago
so is this teaching you how to use faster algorithms that will help your scripts run faster?
sk8rboi7566 10 months ago
@sk8rboi7566 This is a slow sort. Think of all the comparisons you have to do to get each element sorted. Sure its fast for an array of small size, but imagine an array with 9001 elements. once you get up there the number of swaps you have to do to sort. Yes, to answer your question; it is teaching you sorting methods, but wikipedia it. This is a slow one. This can be made to be fast with a hash table or memorization though.
malakov5 10 months ago
is this teaching you faster ways to use algorithms so your programs will fun faster with programming script?
sk8rboi7566 10 months ago
This has been flagged as spam show
Jesus is king of kings and lord of lords
bass109 11 months ago
for all those confused abt the loop being till n-1 rather than till n, here's the explanation..
In the prototype, it mentions for i = 1 to n-1 now n should be interpreted as size of the array and n-1 the last index.. So the loop becomes for(int i = 1; i <= n-1; i++) Thus the loop will continue till the last index of the array
gnarula 11 months ago
wrong algorithm see for i=1 to n not n-1 check this according to ur algo if i sort 24 13 9 2 then first is 13 24 9 2 for j=1 and for j=2 it will 13 9 24 2, + 9 13 24 2, now what about j=3 my elements in array are 4 and i can loop upto 3 times so algo fails so right programe is from i=1 to n then its gonna work and ignore my poor english
2667931 11 months ago
@2667931 the implementation is actually correct check your logic you are doing it wrong
Jackpotur 11 months ago
You forgot to give the time complexity...
highvltg1122 1 year ago
you actually don't have to do all these swaps at all
bigphatkdawg 1 year ago
This has been flagged as spam show
My name is Mike from LA Although there busizz4me.info
consuelahuttonc 1 year ago
that's good ,thank you
cckate321 1 year ago
Its a great video and clearly explains the idea in discussion. A big thumb up for XoaX.net
Hey! I jumped ot xoax.com at first without noticing that .net. I liked the website, it has great potentials but you need to add some more content yet.
ictTrendsVideo 1 year ago
Just to explain a bit further:
i is the INDEX, and J is the actual value of that index.
for i=1 to n-1 not i=0, since i=0 is considered sorted. n-1 refers to array length
j=i focus on the value for that index, is that correct??
while j>0 and A[j]<A[j-1] while number at position 3(for example) is less than number at position 2, not sure why j is more than 0?
swap
and then j is now reduced? j=j-1, so if we were at j=1, j is now 0, algorithm jumps back up to i, i is now 2.
321noone 1 year ago
well done
gognaIT 1 year ago
please make any of your new or old video last for 5minutes or update which video are necessary to be updated
kairen5443 1 year ago
awsum. . .
sim0332 1 year ago
nicely done
ondrasubs 1 year ago
thanks a lot!
harashin0000 1 year ago
thanks dude ^^
jpabloariasa 1 year ago
really good explanation
dfom78 1 year ago
Realy it is great effort thanks very much
nados1977 1 year ago
awsome sweet n simple !!!
Lovel123 1 year ago
Great, I understood the algorithm perfectly with your video. By the way, the music helps. Thanks!
tetsuo8484 1 year ago
I think, a more efficient variant is that we swap only once (when we reach the place of insertion) but while searching for it, we just shift the elements to the right (swap is more expensive the shift).
exotic1406 1 year ago
i like bubble sort cuz i like the word buubbble
WannaBeiDev 1 year ago
@WannaBeiDev yeah but your computer may not like it :)
milohoff88 1 year ago
this really saved my day!
pigpigpigwwf 1 year ago
Hmm, I think I prefer this over bubblesort.
kertaspaper94 1 year ago
best video for Insertion Sort !
eksman187 2 years ago
thanks\m/
mohitsingh009 2 years ago
what does the j=j-1 do?
The algorithm already swaps them so whats the point in the j=j-1?
Need some help here =/
LiquidXTC 2 years ago
The "j=j-1" is so the loop doesn't go on infinitely. If the condition is met, then you swap elements j and j-1, then go on with the next lower index, which is j-1 (so in the next loop iteration, you'll be comparing elements j-1 and j-2, and so on).
AComputerScientist 2 years ago
Comment removed
070action 2 years ago
decrements j
psychofish25 1 year ago
hi ... it is really helpful but i wanna know why for loop is till n-1 only? and not n?
myjunno 2 years ago 15
Because the first element is already in the right relative order
Givicencio 2 years ago
Array indices sometimes range from 0 to n-1 (for size n arrays; not from 1 to n), and this is the convention here. So the loop only goes till n-1. It does not start with i=0 but from i=1, because in the first loop there would be nothing to do (the first element is always in the right relative order with itself)
AComputerScientist 2 years ago
n will work if the array size is larger than the number of items in the array you are sorting. but if the array size is the same number as the number of items, n will cause you to go out of bounds.
TafTaf4Life 1 year ago
@myjunno Because you don't compare an element to itself. That is why is it n-1 and not n
Conan4372 1 year ago
It can't be n, because if it is n and not n-1, then j=i=n which means A[j]=A[n] which is not possible, because A[j] is equal from A[0] to A[n-1]. A[n] is not a value itself. A[n] is n times A, from A[0] to A[n-1]. And all A[0] to A[n-1] are values. I hope I was clear
0lsi 1 year ago
@myjunno
think about it:
the reason it is n-1 rather than n
is that each element is compared to the next one
but if you go to the very last one - n - what will you compare it to? there is no n+1 to do a comparison
that's why the last element to be processed is n-1, it will be compared to the next and final one - n - n is only processed and swapped if it is out of place
fibrosport 1 year ago
@fibrosport is it not because it's done on the example of an array and array has n number of slots but the first slot is always numbered 0 so the last slot would be number of slots minus one. if u have 3 slots they are numbered 0 1 2 so last slot is slot number 2. if the for loop would stop at slot n you'd have a runtime error because you'd fall off the edge of the array (move to the slot that doesnt exist)... I think so ?
mila88323 1 year ago
@myjunno
because in array the first element is in the 0 space array[ ]={1,2,3,4,5,6,7}
array[ 0 ]=1
array [ 5] = 6
so if n=30
array[29]= the last number in that array
:)
bakkecske91 1 year ago 2
This has been flagged as spam show
@myjunno think of it as from 2 to n
MrThatstheguy 1 year ago
@myjunno n = size of array, n - 1 = last index
Jackpotur 11 months ago
@myjunno because the index usually starts at cero.
Mainelcc 11 months ago
@myjunno Because you should find a more suitable job for a moron like you..
ya6655 10 months ago
@myjunno It is because the array is indexed from 0 up to n -1 where n is the number of elements. Treat the first element as index 0 and so on up until the final element which is n-1.
jonathananorman 9 months ago 2
@myjunno the first element is already sorted, so you don't have to sort it, and can skip to the next element, because you skipped the first one, its n-1
claudinegladue 9 months ago 3
@myjunno
I assume that n in this case is the length of the array, and the length of the array is 1 more than the index of the last item in the array. which means the last item in the array would be array[n-1]. If we were to do array[n] then we would get an OutOfBound Error
Darkness2288 9 months ago
@myjunno Cause array which has n element has indexes between 0 and n-1, so the last element is "n-1".
First loop begins from one, but it's not because an array has only positive indexes (like in Pascal), but because element A[0] is always sorted.
youwouldbetterfuckme 9 months ago
@myjunno Because in languages like C, arrays are indexed from 0 up, not 1 up. So the last element is indexed n-1, not n. If you used n, you would be accessing elements not in the array.
axeld93 8 months ago 6
@axeld93 : Then why does the indexing in the pseudocode mention starts from 1 not 0?
fahadkhan2 2 months ago
@fahadkhan2 Pseudocode is intended to be read by humans, and is not in any particular programming language. Hence the pseudocode is indexed from 1 up, since that is how humans count.
axeld93 2 months ago
@myjunno Because the index of array is from 0 to n-1, so there are n elements in the array. Remember the last index is n-1, not n, or the system will occur IndexoOutOfArray exception.
Freedom1987ify 3 months ago
@myjunno Because in an array, the counting starts from zero.
goodmorning20986 3 months ago
Thank you! Much better than that idiot with the deck of cards!
Astrosisphere 2 years ago 77
lol
neolegionar 2 years ago
@Astrosisphere Haha no offence to that guy (he explains heap&quick sort pretty well) but true!
elobato 1 year ago
@Astrosisphere haha i watched that one too xD. You deserve a thumb up!
Edange 1 year ago
@Edange haha, it seems like quite a few people agree with my statement!
Astrosisphere 1 year ago
@Astrosisphere
28haricharan 10 months ago
@Astrosisphere
:P
28haricharan 10 months ago
@Astrosisphere lol that's a bit harsh.
SteveThePCGamer 6 months ago
@Astrosisphere it may be better but that so called idiot is also trying there to teach you and you should appreciate his efforts as well
fuzwire 3 months ago 4
@fuzwire I know, I know; that was written right before an assignment deadline!
Astrosisphere 3 months ago
Great explanation.
MasterAchilles 2 years ago
I like the way you explain it, it is helpful.
thank you
sikkin 2 years ago