3. Sorting 3.1. Understand the soring processes/steps for Bubble/ Selection/Insertion sort 3.2. Please fill in each blank with a proper code 234567x923 8 10 11 12 13 /** Insertion-sort of an array of characters into nondecreasing order */ public static void insertion Sort(char[] data) { int n data.length; } for (int k = 1; k
3. Sorting 3.1. Understand the soring processes/steps for Bubble/ Selection/Insertion sort 3.2. Please fill in each blank with a proper code 234567x923 8 10 11 12 13 /** Insertion-sort of an array of characters into nondecreasing order */ public static void insertion Sort(char[] data) { int n data.length; } for (int k = 1; k
Related questions
Question
![```plaintext
3. Sorting
3.1. Understand the sorting processes/steps for Bubble/Selection/Insertion sort
3.2. Please fill in each blank with the proper code
/** Insertion-sort of an array of characters into nondecreasing order */
public static void insertionSort(char[] data) {
int n = data.length;
for (int k = 1; k < n; k++) { // begin with the second character
char cur = data[k]; // time to insert cur=data[k]
int j = k; // find correct index j for cur
while (__________1__________) { // thus, data[j-1] must go after cur
__________2__________; // slide data[j-1] rightward
j--; // and consider previous j for cur
}
__________3__________; // this is the proper place for cur
}
}
```
### Explanation:
The code provided is an outline of the insertion sort algorithm in Java, which sorts an array of characters into nondecreasing order. It is structured as a template where specific logic needs to be filled in at the indicated blanks.
- **Line 1**: Declaration of the insertion sort method.
- **Line 2**: Initialize the variable `n` to store the length of the array.
- **Lines 3-4**: Iterate over the array starting from the second element (index 1) using a for loop. `cur` is assigned the current value to be sorted.
- **Line 5**: Set `j` as `k`, representing the index to place `cur`.
- **Line 6**: A while loop is indicated, where condition 1 needs to be defined for shifting elements to the right to make space for `cur`.
- **Line 7**: Action 2 involves sliding elements rightward to find the correct position for `cur`.
- **Line 8**: Decrement `j` for further consideration of elements leftward.
- **Line 10**: Action 3 places `cur` in its correct position within the sorted portion of the array.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F2ae76079-938b-4b5d-8ceb-1dfbc10f88ce%2Fd710f751-651b-4755-837b-5fb90711ce45%2Fmgxm7u_processed.png&w=3840&q=75)
Transcribed Image Text:```plaintext
3. Sorting
3.1. Understand the sorting processes/steps for Bubble/Selection/Insertion sort
3.2. Please fill in each blank with the proper code
/** Insertion-sort of an array of characters into nondecreasing order */
public static void insertionSort(char[] data) {
int n = data.length;
for (int k = 1; k < n; k++) { // begin with the second character
char cur = data[k]; // time to insert cur=data[k]
int j = k; // find correct index j for cur
while (__________1__________) { // thus, data[j-1] must go after cur
__________2__________; // slide data[j-1] rightward
j--; // and consider previous j for cur
}
__________3__________; // this is the proper place for cur
}
}
```
### Explanation:
The code provided is an outline of the insertion sort algorithm in Java, which sorts an array of characters into nondecreasing order. It is structured as a template where specific logic needs to be filled in at the indicated blanks.
- **Line 1**: Declaration of the insertion sort method.
- **Line 2**: Initialize the variable `n` to store the length of the array.
- **Lines 3-4**: Iterate over the array starting from the second element (index 1) using a for loop. `cur` is assigned the current value to be sorted.
- **Line 5**: Set `j` as `k`, representing the index to place `cur`.
- **Line 6**: A while loop is indicated, where condition 1 needs to be defined for shifting elements to the right to make space for `cur`.
- **Line 7**: Action 2 involves sliding elements rightward to find the correct position for `cur`.
- **Line 8**: Decrement `j` for further consideration of elements leftward.
- **Line 10**: Action 3 places `cur` in its correct position within the sorted portion of the array.
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 4 steps
