Java: Yellow highlighted columns are outer ‘for’ loop Blue highlighted columns are inner ‘for’ loop
Java:
Yellow highlighted columns are outer ‘for’ loop
Blue highlighted columns are inner ‘for’ loop
Insertion Sort:
i<list.length | CE=list[i] | k=i-1 | (list[k] > CE && (k>=0) |
list[k+1]= list[k]; k--; |
list[k+1]=CE; i++; |
Processing record in position 1 | CE= 8 | k=0 | Move the record to the left until it reaches the correct position. |
Swap list[1]=8; list[0]=150; ist[1]=list[0]; |
list[1]=150; 8,150,55, 78, 91, 10 |
Processing record in position 2 | CE=150 | k=1 | Move the record to the left until it reaches the correct position. |
Swap list[2]=55; list[1]=150; ist[2]=list[1]; |
list[2]=150 8 ,55,150, 78, 91, 10 |
Processing record in position 3 | CE=78 | k=2 | Move the record to the left until it reaches the correct position. |
Swap list[3]=78; list[2]=150; ist[3]=list[2]; |
list[3]=150 8 ,55, 78,150, 91, 10 |
Processing record in position 4 | CE=91 | k=3 | Move the record to the left until it reaches the correct position. |
Swap list[4]=91; list[3]=150; ist[4]=list[3]; |
list[4]=150 8 ,55, 78, 91,150, 10 |
Processing record in position 5 | CE=10 | k=4 | Move the record to the left until it reaches the correct position. |
Swap list[5]=10; list[3]=150; ist[5]=list[4]; |
ist[5]=150 8 ,55, 78, 91, 10,150 |
k=3 ,CE=10 Run Loop Till (list[k] > CE && (k>=0) |
Swap list[4]=10; list[3]=91; ist[3]=list[4]; |
8 ,55, 78, 10, 91,150 | |||
k=2 ,CE=10 Run Loop Till (list[k] > CE && (k>=0) |
Swap list[2]=10; list[1]=55; ist[5]=list[4]; |
8 ,55, 10,78,91,150 | |||
k=1 ,CE=10 Run Loop Till (list[k] > CE && (k>=0) |
Swap list[5]=10; list[3]=150; ist[5]=list[4]; |
8 ,10,55, 78, 91,150 | |||
condition false | condition false |
|
Step by step
Solved in 2 steps