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

icon
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.
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
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps

Blurred answer