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.](https://content.bartleby.com/qna-images/question/2ae76079-938b-4b5d-8ceb-1dfbc10f88ce/d710f751-651b-4755-837b-5fb90711ce45/mgxm7u_thumbnail.png)
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 nowThis is a popular solution!
Step by stepSolved in 4 steps

Knowledge Booster
Similar questions
- // lab8ExB.cpp #include <iostream> using namespace std; void insertion_sort(int *int_array, int n); /* REQUIRES * n > 0. * Array elements int_array[0] ... int_array[n - 1] exist. * PROMISES * Element values are rearranged in non-decreasing order. */ void insertion_sort(const char** str_array, int n); /* REQUIRES * n > 0. * Array elements str_array[0] ... str_array[n - 1] exist. * PROMISES * pointers in str_array are rearranged so that strings: * str_array[0] points to a string with the smallest string (lexicographicall) , * str_array[1] points to the second smallest string, ..., str_array[n-2] * points to the second largest, and str_array[n-1] points to the largest string */ int main(void) { const char* s[] = { "AB", "XY", "EZ"}; const char** z = s; z += 1; cout << "The value of **z is: " << **z << endl; cout << "The value of *z is: " << *z << endl; cout << "The value of…arrow_forwardPlease run this code for n=6 for arrays 1,34,0,6,78,11 and n=5 for array 8,7,6,5,4 #include <stdio.h>void selection_sort(int arr[], int n) { if (n<=1) return ; int max_id=0; for(int i=0;i<n;i++) { if(arr[i]>arr[max_id]) max_id=i; } int temp=arr[max_id]; arr[max_id]=arr[n-1]; arr[n-1]=temp; selection_sort(arr,n-1); return;}arrow_forwardfast please c++arrow_forward
- Fill in the blanks to initialize a character array, a_char) with content code a_char[] Then fill in the blanks in such a way that the following for loop simply prints code to standard output. for (int i = 0; i < i++) { std::cout << a char } std::cout 1# 11 U (a char)/sizeof( } Now fill in the blanks in such a way that the following range based for loop simply prints code to standard output. ; << # ; ); [a_char]){arrow_forwardThe function 'countPrimes' is given below to find the count of prime numbers enclosed within double quotes in an array with 12. Which of the following is the missing code line? int countPrimes( char *a[]) { int i,j,num,nump 0,flag; for (j = 0; j< 12; ++j) { for(i=2; iarrow_forwardQuestion 11 mahuse c++ Full explain this question and text typing work only We should answer our question within 2 hours takes more time then we will reduce Rating Dont ignore this linearrow_forward
- 1. (100 points) Write a Java program that ● ● ● Define an integer array with length of 10. Populate the array with random integer numbers. Sort this array into non-decreasing/decreasing order based on three sorting algorithms. bubble sort selection sort O insertion sort Test these 3 sorting methods Give a big-Oh characterization, in terms of n, of the running time of these three algorithms. Improve the bubble sort so that the time complexity of them are O(N) for the best case (extraarrow_forward8arrow_forward
arrow_back_ios
arrow_forward_ios