Prove the loop invariant of the inner for loop by mathematical induction.

Topic: Algorithm Correctness and Efficiency

Question (9 marks)

1 public static void sort

(int [] arr) { 2 int len = arr . length; 3 for(int i = 0; i < len-1; i++){ 4 for(int j= len-1; j> i; j–){ 5 if ( arr [j] < arr [j -1] ){ 6 int temp = arr [j] ; 7 arr [j] = arr [j -1] ; 8 arr [j -1] = temp; 9 }//end if 10 } // end inner for loop 11 }//end outer for loop 12 }

1. The partial correctness of the Java program above can be proved by proving the loop invariant of the inner for loop and the loop invariant of the outer for loop.

What is the loop invariant of the inner for loop?

What is the loop invariant of the outer for loop? You may write your answer using either logical expressions or their equivalent English expressions. [4 marks]

2. Prove the loop invariant of the inner for loop by mathematical induction. [5 marks]

© 2020 EssayQuoll.com. All Rights Reserved. | Disclaimer: For assistance purposes only. These custom papers should be used with proper reference.