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: Step-by-Step Explanation
#### Original Array:
- `{150, 8, 55, 78, 91, 10}`
#### Table Explanation for Insertion Sort Algorithm:
This table guides you through the process of the insertion sort algorithm by following specific steps illustrated in columns. Each step represents an iteration of the inner workings of the algorithm.
1. **Column 1: `i < list.length`**
- This condition checks if the current index `i` is less than the length of the list. If true, the algorithm proceeds to the next steps.
2. **Column 2: `CE = list[i]`**
- The current element (CE) is set to the value of the list at index `i`.
3. **Column 3: `k = i - 1`**
- `k` is initialized to the index one less than `i`.
4. **Column 4: `(list[k] > CE) && (k >= 0)`**
- This condition checks if the element at index `k` is greater than the current element `CE` and ensures `k` is not negative.
5. **Column 5: `list[k+1] = list[k]; k--`**
- If the above condition is true, shift the element at index `k` to the right (index `k+1`) and decrement `k`.
6. **Column 6: `list[k+1] = CE; i++`**
- Place the current element `CE` in its correct position (index `k+1`). Increment `i` to proceed to the next element in the array.
The table is structured to demonstrate each step iteratively as the insertion sort algorithm processes the original array. This allows an educational exploration of how elements are compared and shifted to achieve a sorted sequence.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fb64c1c6f-d4d7-4d8b-8ebe-5eadb49feddc%2F08bdfb16-2d0b-4c6c-a645-e68fb28fbb8a%2Fqsrasz_processed.png&w=3840&q=75)

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









