We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

staticvoidinsertionSort2(intn,int[]arr){// Complete this functionfor(intk=0;k<arr.length-1;k++){for(inti=0;i<arr.length-1;i++){intkey=arr[k+1];if(i==k+1)break;if(arr[i]>key){arr[k+1]=arr[i];arr[i]=key;}}for(inti=0;i<arr.length;i++){System.out.print(arr[i]+" ");}System.out.println();}}

i think the idea is that they can check whether you have implemented the algorithm right. that is more accurately tested when all iterations and exchanges are recorded.This is not about efficiency, its about learning.

## Insertion Sort - Part 2

You are viewing a single comment's thread. Return to all comments →

Pretty badly described algorithm, needs a better description of the steps.

You can sort of follow this GIFFY

https://upload.wikimedia.org/wikipedia/commons/0/0f/Insertion-sort-example-300px.gif

Thanks it helped me

thank u , it helped me.

Thank you very much!!

Thanks. Very helpful.

thanks bro. it helped me a lot

Thank you! It helped me!

Thank you , helped a lot!

Thanks, that helped a lot. Here's my Javascript implementation of that gif lol =P

The GIFY was great

Thank you. Helped alot!

thanks for the giffy

Discription for algo is not good. It wasted lots of my time. Got clarification from this giffy. Thank you so much for posting this.

https://github.com/shivasurya/cc150/blob/master/sorting/insertions%20sort/C%20lang/insertion.c

check this with algorithm description

Wow, thank you for sharing! It helped me!

Here's another, slightly condensed Python3 solution

Using an in-built sort function defeats the purpose of learning sorting algorithms...

coooooollll..... u are a true coder....

agreed, it's a shame that it assumes some iterations that print the array even if nothing was moved, i.e.:

And solutions like:

that are insertion sort, would print it only when it actually changed, i.e.:

thanks it helped me !

i think the idea is that they can check whether you have implemented the algorithm right. that is more accurately tested when all iterations and exchanges are recorded.This is not about efficiency, its about learning.

dude! thats heck of a complex solution!

I agreed, why do we have to print out in that weird way???

The problem seems to be designed in a very bad way.But after finally solving it you kindof get the idea what insertion sort does at every step.