Skip to content

Update InsertionSort.java #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Update InsertionSort.java #64

wants to merge 2 commits into from

Conversation

Vsareen0
Copy link

Comments have been added for the implementation of insertion sort algorithm

@Shayan-Asgari
Copy link

In the insertion sort when you put "arr[j+1] = key;", I believe it is unnecessary since you have arr[j+1] = arr[j] where arr[i] will always be key.

Regards, Shayan Asgari

@Shayan-Asgari
Copy link

Inside the while loop there is also a problem. You do not do any swapping of integers, you are simply overriding the original array! You must have:
while (j>=0 && arr[j] > key)
{
int temp = arr[j]; // save arr[j] into a temporary
arr[j] = arr[j+1] ; // [J+1] will always be key
arr[j+1] = temp;
j = j--;
}
//arr[j+1] = key; <-------There is no need for this line

Copy link
Author

@Vsareen0 Vsareen0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please have a look at modifications !

@Shayan-Asgari
Copy link

I believe the code is still incorrect. You are overriding some of the position values.

Regards,
Shayan Asgari

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants