0
I have got problem in insertino sort code. Can someone guide me what I'm doing wrong?
I am having problem it should display from 1 to 5 unless it's displaying 5 to 1. Plz guide me..... https://code.sololearn.com/c9P0rpBJS5pS
3 Answers
+ 1
for(int i=0;i<5;i++){ //start from i = 0
for(int j=i;j<5;j++) //j=i
{
if(a[i] > a[j]) //use > instead of <
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
0
Jayakrishnađźđł can you please explain it???
0
It's a bubble sort, not insertion sort. But main idea in both is when a[j-1] > a[j] then we need swap.. That in 5,4 3,2,1 there 1st 5 > 4 then we need to swap..
In bubble sort we do 1 swap for adjacent elements in one iteration but in insertion sort the element swapped until it placed in correct position a[j-1]<a[j] like 4,5 for 4 in list. 3 to before 4,...so on......