Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Consider the implementation below for Insertion Sort, which sorts an array into descending order. public void InsertionSort(int[] array, int n) // PRE: n is the
Consider the implementation below for Insertion Sort, which sorts an array into descending order. public void InsertionSort(int[] array, int n) // PRE: n is the length of the array POST: array is sorted int i; int j; int key: for (j 1; j n; j++) { //Notice starting with 1, not ? // Invariant for the outer Loop: array[0..j-1] is sorted, descending order keyarrayj]: for (1-j - 1; (1 >-?) && (array[1] key) ; i-) { // Move smaller values up one position array[i1] -array[i] array[? + 1] key; // Insert key into proper position (++) return a) Trace the algorithm for the array A 12, 1, 5, 0, 6), and n 5; make sure you keep track of the array after each inner and outer iteration. In your trace, draw a rectangle around the array after each complete iteration of the OUTER loop b) Verify that the array portion A[0..j-1] is sorted after each complete iteration of the outer loop, i.e. underline that portion of the array and check that it is sorted. (Note that A[0.j- 1] means "the portion of the array between, and including, indices 0 and j-1.) Now check the invariant for the outer loop is established (made to be true) by the initialisation of the variable j; i.e. substitute the initial values of the relevant variables into the invariant statement and observer that it is true. c) d) Finally deduce that the postcondition is satisfied after the outer loop has terminated
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started